checkbox on gridview

M

mike7510uk

Hi, I am using a gridview with a templatefield containing a checkbox.
I want to update the database with a 1 or 0 depending on if a checkbox
is checked or unchecked (then use the 1 or 0 later on another page)..i
have been google around and found this....

If e.Row.RowType = DataControlRowType.DataRow Then

Dim Chk As CheckBox = e.Row.FindControl("CheckBox1")

just dont know what to do next...Any ideas?
 
G

Goofy

Well, how you do it depends on when you want to do the update. Basically you
need to indeed find the control in the row/column. When data is bound to the
grid, this event fires for each row bound to the control. The event args 'e'
is passed to the function and you can use this to check if the checkbox is
checked or not.

(Not tested but something like this)

'//e contains the Item which is the current row.
dim cb as checkbox

cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)

if cb.selected = true then

'//do domething

end if

HTH
 
M

mike7510uk

Hi
I have expanded on what i had originally but still cant get it to
update DB...here is the new code i have....

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



Dim MyConnection As OdbcConnection

MyConnection = New OdbcConnection() 'declare new connection
object

MyConnection.ConnectionString = "" 'database connect string

MyConnection.Open() 'open connection to database

Dim MyCommand As New OdbcCommand() 'declare new command object

MyCommand.Connection = MyConnection



If e.Row.RowType = DataControlRowType.DataRow Then

Dim Chk As CheckBox = e.Row.Cells(5).FindControl("save")



If Chk.Checked = True Then
MyCommand.CommandText = "update support.support set
save = 1"



End If

If Chk.Checked = False Then
MyCommand.CommandText = "update support.support set
save = 0"



End If


MyCommand.ExecuteNonQuery()

MyConnection.Close()

MyCommand = Nothing



'MsgBox(Chk.Checked)



End If

End Sub
 
G

Goofy

breakpoint the line as shown below and see if you get an exception, if you
do look at the exception message and see what its telling you and go from
there.

Try

MyCommand.ExecuteNonQuery() '// breakpoint it here


Catch ex as Exception



End Try
 
M

mike7510uk

Tried that and nothing happened, it loaded page up ok and let me click
the checkboxes but no DB update
 
M

mike7510uk

with the code you posted earlier ('//e contains the Item which is the
current row.
dim cb as checkbox


cb= Ctype(e.item(indexColumnNumber).controls(1),checkbox)


if cb.selected = true then


'//do domething


end if )

i get the error saying "item is not a member of system.web etc etc
 
J

Jim in Arizona

mike7510uk said:
Tried that and nothing happened, it loaded page up ok and let me click
the checkboxes but no DB update

How are you specifying where to do the update within your support table?
MyCommand.CommandText = "update support.support set save = 0"

I'm not an expert, but I couldn't find anything in your code (update sql
statement) that would tell the database where in the table to do the update.
 
M

mike7510uk

Ok, im confused now..

Basically, i want a gridview with a templatefield (or checkboxfield)
with a checkbox on it that when the user clicks the checkbox it sends a
1 or 0 value back to DB (1 for checked and 0 for unchecked)
 
M

mike7510uk

I have now managed to do this (at last)
here is the code...

Protected Sub save_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim MyConnection As OdbcConnection

MyConnection = New OdbcConnection() 'declare new connection
object

MyConnection.ConnectionString = " " 'database connect string

MyConnection.Open() 'open connection to database

Dim MyCommand As New OdbcCommand() 'declare new command object

MyCommand.Connection = MyConnection

Dim cID As Integer = 0
' Dim str As StringBuilder = New StringBuilder
Dim i As Integer = 0
Dim state As Integer
For i = 0 To GridView1.Rows.Count - 1
'Dim row As GridViewRow = GridView1.Rows(i)
Dim isChecked As Boolean =
CType(GridView1.Rows(i).FindControl("save"), CheckBox).Checked
If isChecked Then
state = 1
Else
state = 0
End If

MyCommand.CommandText = "update support.support set save =?
where id=?"
MyCommand.Parameters.Clear()
MyCommand.Parameters.Add(New OdbcParameter("@state",
state))
MyCommand.Parameters.Add(New OdbcParameter("@id",
GridView1.Rows(i).Cells(0).Text))
MyCommand.ExecuteNonQuery()
Next

MyConnection.Close()
MyCommand = Nothing
End Sub

All i need to do now is if a user checks a particular checkbox, it
remains checked the next time they visit the page..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top