Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to enable check box in DataGrid on Edit command?

Status
Not open for further replies.

Katya85S

Programmer
Jul 19, 2004
190
In asp.net project (vb.net), in dataGrid I use CheckBox:
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id=chk Enabled=false runat="server" checked='<%# DataBinder.Eval(Container.DataItem, "Display") %>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

It is disabled, if no events are hired. I want to enable the checkbox on DataGrid’s Edit command, thus users could check-uncheck record if needed. I thought this code (below) should do it. On Edit command DataGrid turns into its Edit stage, though checkbox of the selected record remains disabled. Any ideas?
Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

Dim chknew As CheckBox = e.Item.FindControl("chk")
chknew.Enabled = True
dg.EditItemIndex = e.Item.ItemIndex

BindData()

End Sub
I would appreciate any help.
Thank you all in advance.
 
hi,

what does BindData() do? does it rebind the data?

then try this:

Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

BindData()

Dim chknew As CheckBox = e.Item.FindControl("chk")
chknew.Enabled = True
dg.EditItemIndex = e.Item.ItemIndex
End Sub


Known is handfull, Unknown is worldfull
 
Hi vbkris. thank you for the replay.
Here is my BindData():
Sub BindData()
Dim strSQL As String = "SELECT Field1, Field2 FROM tbl1 "

Dim objCmd As New SqlCommand(strSQL, myConn)
'Set the datagrid's datasource to the datareader and databind
myConn.Open()
dg.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection)
dg.DataBind()
End Sub
I've tried your code, but having DataBind before
dg.EditItemIndex = e.Item.ItemIndex
selects wrong record (not teh clicked one) for teh Editing mode. Having chknew.enabled = true after BindData (I've tried it just in case [below]) didn't help either:

Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

dg.EditItemIndex = e.Item.ItemIndex
BindData()
Dim chknew As CheckBox = e.Item.FindControl("chk")
chknew.Enabled = True

End Sub
 
why do you want to rebind it every time?

i am guessing that Rebinding will cause the grid to loose its edit mode as rebinding will override it...

Known is handfull, Unknown is worldfull
 
Do you suggest to take off the BindData() of the dg_Edit subroutine?
I've tried to see how it will affect the event. You were right, it does enable the checkbox, but this way dg_Eit selects absolutely wrong record for editing and works very unstable, sometime, when edit button is clicked it opens up empty textbox within DataGrid, but most of the time it opens a record clicked for editing one step behind.
Is there any solution?
 
hmm,

thats strange behavior!

why dont you try editing the first record continuously for 5 times? is this problem occruing only to middle rows???

Known is handfull, Unknown is worldfull
 
it doesn't do any thing, the record remains in regular (not edit) mode, and only after i switched to the next record the firs record happen to switch to the edit mode. Then i hit the second record second time, the first record turned into it's normal stage, but the second record remained unchanged, except that they both lost its value at some point (I haven't cached when exactly it happened) in the column with ReadOnly=False, so clicking the same record many times does nothing to the datagrid, except that it deletes some values.
 
hmm,

i guess you have to set debug points and do this. it seems that to edit you have to click twice right???

Known is handfull, Unknown is worldfull
 
Kind of…
Here is a part of my datagrid:

<asp:datagrid id="dg" runat="server" DataKeyField="ID" AutoGenerateColumns="False" OnDeleteCommand="dg_Delete" OnEditCommand="dg_Edit" OnCancelCommand="dg_Cancel" onUpdateCommand="dg_Update">

There is an EditCommandColumn in the datagrid
<asp:EditCommandColumn HeaderStyle-HorizontalAlign="center" EditText="Edit" ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" />

This is represented by Edit button in front of each row. To edit a record user hits the Edit button that fires datagrid’s OnEditCommand (="dg_Edit")
Dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
dg.EditItemIndex = e.Item.ItemIndex
BindData()
End Sub
This turns columns with ReadOnly=False property into textboxes, so users can update the selected record. And the, yes, you have to hit the Update button, that fires datagrid’s OnUpdateCommand to run the Update query and write the updated record into the database.

Sub dg_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'Read in the values of the updated row

Dim strID As String = e.Item.Cells(1).Text
Dim iID As Integer = CInt(TipID)
Dim strType As String = CType(e.Item.FindControl("txtType"), TextBox).Text

Dim chknew As CheckBox = e.Item.FindControl("chk")
Dim chkVal As Integer
If chknew.Checked Then
chkVal = "1"
Else
chkVal = "0"
End If

Dim strSQL As String = _
"UPDATE [tbl1] SET [Type] = ‘“ & strType & _
& "', Display = '" & chkVal & "' " & _
"WHERE [ID] = iID"

'2. Create the command object, passing in the SQL string
Dim myCommand As New SqlCommand(strSQL, myConn)
myConn.Open()
myCommand.CommandType = CommandType.Text

myCommand.ExecuteNonQuery() 'Execute the UPDATE query
myCommand.Dispose()
myCommand = Nothing
myConn.Close() 'Close the connection

'''''''...
'Finally, set the EditItemIndex to -1 and rebind the DataGrid
dg.EditItemIndex = -1
BindData()
End Sub

Is there more efficient way to perform the same task?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top