1

I am trying to develop an asp c# gridview that has select so that I can display additional details of the record. I also need a check box to allow the user the check the row for further processing. I can achieve each separately but not together. Is it even possible?

3 Answers3

2

Of course it is possible. For the column of check boxes, use TemplateField column, https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield%28v=vs.110%29.aspx

In data grid, aspx:

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="CheckBoxProcess" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

In code behind:

protected void ButtonProcess_Click(object sender, EventArgs e)
{
    foreach (GridViewRow item in GridView1.Rows)
    {
        CheckBox chk = (CheckBox)item.FindControl("CheckBoxProcess");
        if (chk != null)
        {
            if (chk.Checked)
            {
                // This record should be processed
            }
        }
    }
}

The GridView has build in row select functionality, you can enable it by setting AutoGenerateSelectButton property to true:

<asp:GridView ... AutoGenerateSelectButton="true" />

However, it is more user friendly to select the row when user clicks on it (rather than clicking on the link). To do this, you need to attach a bit of java-script to the row:

void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
        e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString())
}

Here is also, much better explained solution for row click select:

https://stackoverflow.com/a/6250846/461810

Community
  • 1
  • 1
Dusan
  • 5,000
  • 6
  • 41
  • 58
0

You can use template fields and just add the controls into them.

<Columns>                             
    <asp:TemplateField>
        <ItemTemplate> 
            <select />
        </ItemTemplate>
    </asp:TemplateField>             
    <asp:TemplateField>
        <ItemTemplate> 
            <checkbox />
        </ItemTemplate>
    </asp:TemplateField>                  
</Columns> 
Jon
  • 626
  • 7
  • 19
  • Sorry i wasn't clear. I just want the row select-able with a click. And that click to fire the select method in code behind. As well as having a checkbox that when clicked fires its own method in code behind. – Phillip Andrews Apr 08 '15 at 19:10
  • Apologies for the misunderstanding. I haven't ever met this as a requirement, so I'm not familiar enough with the code to give you a great answer. I did, however, find a web page which I believe covers what you're trying to accomplish here. Hopefully you find it helpful! [Selecting GridView Row by clicking anywhere on the Row](http://www.aspsnippets.com/Articles/Selecting-GridView-Row-by-clicking-anywhere-on-the-Row.aspx) – Jon Apr 08 '15 at 19:29
0
<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="SelectChk" OnCheckedChanged="chk_Click1" runat="server" AutoPostBack="true" />
    </ItemTemplate>
</asp:TemplateField>

And in the CS code:

protected void chk_Click1(object sender, EventArgs e)
{
    CheckBox MChk = sender as CheckBox;
    GridViewRow MyGridR = MChk.NamingContainer as GridViewRow;
    GridView1.SelectRow(MyGridR.RowIndex);
}                    
Nrzonline
  • 1,600
  • 2
  • 18
  • 37