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 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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> 

2 Comments

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.
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
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);
}                    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.