Checkbox within gridview template field

27 views
Skip to first unread message

donk888888

unread,
Mar 30, 2007, 10:30:32 AM3/30/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I have a gridview that I have created a template field with a checkbox
within it. I have figured out how to update the database when the box
is checkbox is checked using a sqldatasource. Now when the grid
repopulates I want to look into the database to see if the checkbox
has been checked and if so then have the checkbox come up checked. I
am trying to do this in a rowcreated event and have also tried a
rowdatabound event and I keep getting a index out of range error. Here
is the code thanks for any help.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowCreated
Dim asset As String
Dim flag As String
Dim index As Integer


If e.Row.RowType = DataControlRowType.DataRow Then

asset = e.Row.Cells.Item(3).Text
index = e.Row.RowIndex

newcommand = "SELECT IT_FLAG_1 FROM IT_PC_INVENTORY WHERE
ASSET_TAG = '" & asset & "'"

Dim command As New SqlCommand(newcommand, myconnection)

myconnection.Open()

Dim Reader As SqlDataReader = command.ExecuteReader()
Do While Reader.Read
flag = Reader("IT_FLAG_1")

Loop
myconnection.Close()
If flag = "T" Then


CType(GridView1.Rows(index).FindControl("chk1"),
CheckBox).Checked = True
Else

CType(GridView1.Rows(index).FindControl("chk1"),
CheckBox).Checked = False

End If
ElseIf e.Row.RowType = DataControlRowType.Footer Then

End If

End Sub

MeenakshiSundaresa

unread,
Mar 30, 2007, 3:00:30 PM3/30/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
hi,

In which line you are getting the index out of range error.

if it is with
asset = e.Row.Cells.Item(3).Text

then you have to make sure that your grid has 4 cells.

I would say you can search for the asset control (hope that is label)
like below to retrieve the text value
asset =
((Label)GridView1.Rows(e.Row.index).FindControl("yourLabelBoxControlID")).Text

Thanks
Meens.

donk888888

unread,
Apr 2, 2007, 9:02:02 AM4/2/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Meens,

Thanks for the reply, but no it is not with the
asset=e.rows.cells(3).text that is returning correctly. The out of
range is when I using index = e.row.rowindex in the


CType(GridView1.Rows(index).FindControl("chk1"), CheckBox).Checked =

False portion of the code.
I find this weird because if it knows and returns correctly
asset=e.rows.cells(3) and when I debug the index=e.row.rowindex it
gives me a value of 0 so it seems to know that it is on the first row
but
it still errors out.

On Mar 30, 3:00 pm, "MeenakshiSundaresa" <vmeenaks...@gmail.com>
wrote:


> hi,
>
> In which line you are getting the index out of range error.
>
> if it is with
> asset = e.Row.Cells.Item(3).Text
>
> then you have to make sure that your grid has 4 cells.
>
> I would say you can search for the asset control (hope that is label)
> like below to retrieve the text value
> asset =

> ((Label)GridView1.Rows(e.Row.index).FindControl("yourLabelBoxControlID")).T­ext
>
> Thanks
> Meens.
>

Vinod Giri

unread,
Apr 3, 2007, 1:16:32 AM4/3/07
to DotNetDe...@googlegroups.com
For the purpose of upating the database, if checbox is checked, use following code:
 foreach(DataGridItem itm in DataGrid1.items)
{
    Checkbox chk=(CheckBox) itm.findControl("CheckBox1") ;
   if(chk.checked==true)
  {
              // Your code to update the database
  }
}

You also asked same status when page gets load so refer to following code :
 Page_Load()
{
     // Read the data from database & store it in dataset object (dsTemp)
  for(int i=0;i<dsTemp.tables[0].rows.Count;i++)
{
  CheckBox chk1=null;
   if(dsTemp.tables[0].rows[0]["YourColumnName"]=="1")
  {
     chk1 = DataGrid1.Items[i].findControl("CheckBox1")    ;
     chk1.checked=true;
  }
 }
}
 
 
On 3/30/07, donk888888 <kdon...@yahoo.com> wrote:

Hi,

I have a gridview that I have created a template field with a checkbox
within it. I have figured out how to update the database when the box
is checkbox is checked using a sqldatasource. Now when the grid
repopulates I want to look into the database to see if the checkbox
has been checked and if so then have the checkbox come up checked. I
am trying to do this in a rowcreated event and have also tried a
rowdatabound event and I keep getting a index out of range error. Here
is the code thanks for any help.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewRowEventArgs ) Handles

GridView1.RowCreated
       Dim asset As String
       Dim flag As String
       Dim index As Integer


       If e.Row.RowType = DataControlRowType.DataRow Then

           asset = e.Row.Cells.Item (3).Text
           index = e.Row.RowIndex

           newcommand = "SELECT IT_FLAG_1 FROM IT_PC_INVENTORY WHERE
ASSET_TAG = '" & asset & "'"

           Dim command As New SqlCommand(newcommand, myconnection)

           myconnection.Open()

           Dim Reader As SqlDataReader = command.ExecuteReader()
           Do While Reader.Read
               flag = Reader("IT_FLAG_1")

           Loop
           myconnection.Close()
           If flag = "T" Then


               CType(GridView1.Rows(index).FindControl("chk1"),
CheckBox).Checked = True
           Else

               CType( GridView1.Rows(index).FindControl("chk1"),

donk888888

unread,
Apr 3, 2007, 9:04:41 AM4/3/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Vinod,

Updating the database with the checkbox has not been a problem. And
now I also haved figured out how to
get the boxes chekced on the page_load But I am still having a problem
with holding the check box checked throughout
the different events fired within a gridview. For example on page load
the correct box is checked then when i hit one of the
update buttons to update another row all checks from the page_load are
gone. I tried putting the code to check the boxes
which is pretty similiar to what you have below into a sub and calling
the sub in the different events but that has not worked.
I am looking into viewstate stuff now but have not figured out nething
yet

Thanks
Kevin

> > As System.Web.UI.WebControls.GridViewRowEventArgs) Handles


> > GridView1.RowCreated
> > Dim asset As String
> > Dim flag As String
> > Dim index As Integer
>
> > If e.Row.RowType = DataControlRowType.DataRow Then
>
> > asset = e.Row.Cells.Item(3).Text
> > index = e.Row.RowIndex
>
> > newcommand = "SELECT IT_FLAG_1 FROM IT_PC_INVENTORY WHERE
> > ASSET_TAG = '" & asset & "'"
>
> > Dim command As New SqlCommand(newcommand, myconnection)
>
> > myconnection.Open()
>
> > Dim Reader As SqlDataReader = command.ExecuteReader()
> > Do While Reader.Read
> > flag = Reader("IT_FLAG_1")
>
> > Loop
> > myconnection.Close()
> > If flag = "T" Then
>
> > CType(GridView1.Rows(index).FindControl("chk1"),
> > CheckBox).Checked = True
> > Else
>
> > CType(GridView1.Rows(index).FindControl("chk1"),

> > CheckBox).Checked = False
>
> > End If
> > ElseIf e.Row.RowType = DataControlRowType.Footer Then
>
> > End If
>

> > End Sub- Hide quoted text -
>
> - Show quoted text -

Reply all
Reply to author
Forward
0 new messages