1

I have a GridView which show data retrived from database. I've made TemplateField (CheckBox) to GridView with this code:

<asp:GridView ID="dbRecordsContent" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="dbRecordsContent_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="myCheckBox" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
        <asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
        <asp:BoundField DataField="category" HeaderText="category" SortExpression="category" />
        <asp:BoundField DataField="isChecked" HeaderText="isChecked" SortExpression="isChecked" />
    </Columns>
</asp:GridView>

My grid view looks like this:

enter image description here

My question is: How do I know which checkbox ID is checked? For example:

I want to delete 2nd row when I press "Delete" button. Of course I will check second Checkbox, but how do I know which record to delete? How to reference second checkbox in a code?

2 Answers 2

2

In your delete button's click event handler you need to loop through all the rows in the grid and if a check box is checked, then you need to perform your delete logic, like this:

protected void DeleteButton_Click(object sender, EventArgs e)
{
    foreach(GridViewRow row in dbRecordsContent.Rows)
    {
        // Only look for check boxes in data rows, ignoring header 
        // and footer rows
        if (row.RowType == DataControlRowType.DataRow)
        {
            if (((CheckBox)row.FindControl("myCheckBox")).Checked)
            {
                // Do delete logic here
            }
        }
    }
}

UPDATE:

To get the row number use the GridViewRow.RowIndex property, like this:

int rowNumber = row.RowIndex;

Read GridViewRow.RowIndex Property for more information.

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

5 Comments

It works perfect. One more question. How do I get checked row number?
@rootpanthera - see UPDATE: in answer.
Hmm, actually I found out that it doesn't work as it should. It works only if last checkbox in a row is selected (true statement above). Else statement is always executed even if any other checkbox is checked. Any idea why?
What else statement, I don't have an else statement in the code I posted?
Yep it was my bad. I mixed up something. Your code is perfect. I will accept your answer.
0

try this,

<asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="myCheckBox" runat="server" AutoPostBack="true"
                       oncheckedchanged="CheckBox1_CheckedChanged" />
        </ItemTemplate>
</asp:TemplateField>

code side,

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
    int index = row.RowIndex;
    CheckBox cb1 = (CheckBox)Gridview.Rows[index].FindControl("myCheckBox");
    string checkboxstatus;
    if (cb1.Checked)
    {
      //write your code
    }        
    else
    {
      //write your code
    }
}

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.