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
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.
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")).Text
>
> Thanks
> Meens.
>
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"),
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 -