0

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>
0

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);
  • Thanks. But this is for a button action. I would like to trigger a click event for each item (in a column) in the SPGridView. Column data consist of folder and file names. – Thomas Mathew Sep 22 '14 at 3:39
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.