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.

Recommended Answers

All 6 Replies

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

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

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.

u have to use row command event to achieve this functionality.

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

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

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
// 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.)

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

        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
                    MoreThanOneTime = False
                End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.