Question

I have created a SPGridView control in a visual web part project. This control displays all the folders inside a Document library. I have used SPBoundField to the display the column data. Is it possible to trigger a custom click event for an column item in the grid ?

<SharePoint:SPGridView ID="ShowNode" runat="server" AutoGenerateColumns="false" AllowSorting="true" >
<Columns>
<SharePoint:SPBoundField HeaderText="Name" DataField="Title" HeaderStyle-HorizontalAlign="Left" SortExpression="Name" />
<SharePoint:SPBoundField HeaderText="Type" DataField="Type" HeaderStyle-HorizontalAlign="Left" SortExpression="Type" />
</Columns>
</SharePoint:SPGridView>
Was it helpful?

Solution 2

This can be achieved by "OnRowDataBound" event along with "OnSelectedIndexChanged" event.

<SharePoint:SPGridView ID="ShowNode" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnRowDataBound="OnRowDataBound"
    OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<SharePoint:SPBoundField HeaderText="Name" DataField="Title" HeaderStyle-HorizontalAlign="Left" SortExpression="Name" />
<SharePoint:SPBoundField HeaderText="Type" DataField="Type" HeaderStyle-HorizontalAlign="Left" SortExpression="Type" />
</Columns>
</SharePoint:SPGridView>

CS file code

protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(ShowNode, "Select$" + e.Row.RowIndex);
        e.Row.Attributes["style"] = "cursor:pointer";
    }
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{

}

Reference URL - http://www.aspsnippets.com/Articles/Add-Row-Click-event-to-GridView-Rows-in-ASPNet.aspx

OTHER TIPS

You can use RowCommand event of the Button field like following.

 btnRemoveItem = new ButtonField();
           btnRemoveItem.ButtonType = ButtonType.Button;
           btnRemoveItem.Text = "Remove";
           gridList.RowCommand += new GridViewCommandEventHandler(gl_RowCommand);
           gridList.Columns.Add(btnRemoveItem);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top