954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem regarding check box in gridview

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.

Shubhib
Newbie Poster
11 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

{
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

raj416
Newbie Poster
14 posts since Jan 2008
Reputation Points: 12
Solved Threads: 2
 

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.

Shubhib
Newbie Poster
11 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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

greeny_1984
Posting Whiz
372 posts since Apr 2007
Reputation Points: 25
Solved Threads: 29
 

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

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

binoj_daniel
Practically a Master Poster
645 posts since Dec 2006
Reputation Points: 25
Solved Threads: 18
 

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

Shubhib
Newbie Poster
11 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 
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
hopel7758
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You