sir, i'm using ASP.Net 2.0,
i'm using a gridview in a form and i want to create a check boxes dynamically in each row of my grid view so that i can select multiple values from that grid, i want to use those selected values for some manipulation in the same form.

{
     foreach (GridViewRow row in GridView1.Rows) {
        if (((CheckBox)row.FindControl("CheckBox1")).Checked == true) {
             ((CheckBox)rows.FindControl("CheckBox1")).Text();
         }
    }
 }

try this code ... hope it will work for you

Edited 3 Years Ago by Dani: Formatting fixed

thanks for ur reply but this is not working, can u tell me where to put this code, and it is giving error when i write it there.

First add this code on the .aspx file to add checkboxes to GridView

<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemTemplate>
<asp:CheckBox ID="chkDeleteRows" runat="server"/>
</ItemTemplate>
<FooterTemplate>                        
<asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>                                               
</FooterTemplate> 
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

Then locate

protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e) for your GridView and place the code below.

            // Looping through all the rows in the GridView
            foreach (GridViewRow row in gv.Rows)
            {
                CheckBox checkbox = (CheckBox)row.FindControl("chkDeleteRows");
                //Check if the checkbox is checked. 
                if (checkbox.Checked)
                {
                    // Pass the Row ID to the DeleteList function
                    DeleteList(Convert.ToInt32(gv.DataKeys[row.RowIndex].Values[0]));
// DataKeys should be defined in the GridView property on the aspx page along with the delete button handler like this (OnRowDeleting="gv_RowDeleting")
// DataKeyNames="ROWID" (this should be the Primary Key in the table.)
                }
            }

Edited 3 Years Ago by Dani: Formatting fixed

sir this thing is not working, basically i'm facing a prob because of Direct Cast and Find control. when i'm writing a cond for checking checkbox is checked or not then it is always coming false. i don't know how to use find control or direct cast for this condition. please help me. i'm in very much prob

Dim i, k As Integer
        Dim j As Integer = 0
        Dim q As Integer = 0
        Dim isChecked As Boolean
        Dim MoreThanOneTime As Boolean = False

        For i = 0 To FormGV.Rows.Count - 1
            isChecked = DirectCast(FormGV.Rows(i).FindControl("CheckBox1"), CheckBox).Checked
            If isChecked Then
                j += 1
            End If
        Next

        Dim arrCpyNo As String() = New String(j - 1) {}
        Dim arrCpyName As String() = New String(j - 1) {}
        Dim arrDate As String() = New String(j - 1) {}

        For i = 0 To FormGV.Rows.Count - 1
            isChecked = DirectCast(FormGV.Rows(i).FindControl("CheckBox1"), CheckBox).Checked
            If MoreThanOneTime Then
                q += 1
            End If

            For k = q To j - 1
                If isChecked Then
                    arrCpyNo(k) = FormGV.Rows(i).Cells(1).Text
                    arrCpyName(k) = FormGV.Rows(i).Cells(2).Text
                    arrDate(k) = FormGV.Rows(i).Cells(3).Text
                    MoreThanOneTime = True
                    Exit For
                Else
                    MoreThanOneTime = False
                End If
            Next
        Next

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

This article has been dead for over six months. Start a new discussion instead.