Hi,
How do I add a CheckBox Column to my DataGrid and set the Check box in a way that Depending on the value returned in my row, it is either Checked or not checked?
Reaction
Printable View
Hi,
How do I add a CheckBox Column to my DataGrid and set the Check box in a way that Depending on the value returned in my row, it is either Checked or not checked?
Reaction
Do you mean DataGrid or DataGridView? If you mean DataGrid then I suggest that you change to using a DataGridView unless you have a good reason not to. If you mean DataGridView then please be careful with your naming because they are two different things and the answer will be different for each one.
i need this too.
please help.
thanks
Hi,
To add a Checkboxcolumn in a datagridview you have to add a Column and change the type into the one you need, so for this problem you set it to DatagridViewCheckBoxColumn.
That's it.
Wkr,
sparrow1
Not just that. I am actually working on a Gridview, what I want is based on the returned Bit 1 or 0, the Check box is checked or unchecked. I would also like to basically when User checks or Unchecks any of the checkBoxes, right before the page reloads, the program saves the information back to the database. Basically, the GridView is filtered by values in a DropDownList, If User selects a New Index, I want method to check to see if any changes were made with the previous SelectedIndex, save the data and then change to the new selected Index and then filter the Datagrid with that new value.
Reaction
Quote:
Originally Posted by Reaction
1) Add a DataGridView control to your form 2) Choose Datasource from the small arrow on the Top right of the control. 3) Add Project Data Source 4) select Database 4) Select Connections 5) Select the Table 6) Finish
Make sure you have a Boolean Yes/No Column in your Table that you can show as a Checkbox.
Also make sure to enable editing for that column. To save the changes back to the database once checked/unchecked.
Hope this helps. Else i have done similar using List view :)
Thanks
in2minds
Here is the thing .... I am not using a DataGridView but a GridView control ( web Project), The Column contains 1's and 0's, supposed to be bits. How do I go about setting this please?? Another issue is how do I catch the OnCheckedChanged event of the CheckBox so I can know to save this to the Database? Right now what I have is this
vb Code:
<asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="ChangeRowColor" /> </ItemTemplate> </asp:TemplateField>
But this does not seem to call that method CHANGEROWCOLOR when I check a box or make changes to it . How do I capture this please??
Reaction
Quote:
Originally Posted by Reaction
Hi Reaction,
For the checkbox state here is the code i use in my program: clbTypeTrans is a checklistbox, you can use this method for checking the states.
'---Update List of Transactions for List box
For i As Integer = 0 To clbTypeTrans.Items.Count - 1
Dim CheckStatus As String
If clbTypeTrans.GetItemCheckState(i).ToString() = "Unchecked" Then
CheckStatus = "No"
Else
CheckStatus = "Yes"
End If
SetTransactionTypes(clbTypeTrans.Items(i).ToString(), CheckStatus)
Next
I already have a way to loop through the DataGrid and check each Check box, but say I want to change the color of the row when user selects or deselects a check box, or I want to set the value of a CHECKCHANGED boolean variable when a user Checks or Unchecks a checkbox in my Datagrid, how do I capture this event ??
Public Sub ChangeRowColor(ByVal sender As Object, ByVal e As EventArgs)
ChangeMade = True
'since one of the two scenarios when this will be needed is IF a change has been made
RoleID = DDLRoles.SelectedIndex
End Sub
Public Sub SubmitChanges(ByVal sender As Object, ByVal e As EventArgs)
Dim MySiteAdmin As New WHC.SiteAdmin("DbUserAuth")
Dim row As GridViewRow
Dim isSelected As Boolean
Dim UserId As Integer
'run the code below ONLY if a change has been made, else simply do not bother doing the heavy work
'If ChangeMade Then
'loop through the Grid rows to find the changed rows and send any change to DB
For Each row In MyGridUser.Rows
If (CType(row.FindControl("ChkSelect"), CheckBox).Checked = False And _
CInt(CType(row.FindControl("RoleManagers"), Label).Text) = 1) Then
' UserId = CInt(CType(row.FindControl("UserID"), Label).Text)
' UserId = CInt(row.DataItem("UserID").ToString)
isSelected = CType(row.FindControl("ChkSelect"), CheckBox).Checked
If isSelected Then
MySiteAdmin.AdminUserRoleAssociationAddNew(UserId, RoleID, 1)
Else
MySiteAdmin.AdminUserRoleAssociationAddNew(UserId, RoleID, 0)
End If
End If
Next
'End If 'end of changemade run
End Sub
Reaction
Hi, although this post is old, the answer I think you are looking for is:
For the CheckBox in your Column of the dataGrid,
1)ensure the AUTOPOSTBACK property of the CheckBox is set to TRUE.
2) In the HTML as you have done enter the
OnCheckedChanged="SaveChanges" or "ChangeRowColor" as you wanted
3) Then in your VB Code window create your Subroutine making sure it is a PUBLIC subroutine.HTML Code:<asp:TemplateColumn HeaderText="AMPS Updated">
<ItemTemplate>
<asp:CheckBox id=CheckBox1 runat="server" AutoPostBack="True" Checked='<%# DataBinder.Eval(Container, "DataItem.AMPSUPDATED") %>' OnCheckedChanged="SaveChanges"> </asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox id=CheckBox2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AMPSUPDATED") %>' Checked='<%# DataBinder.Eval(Container, "DataItem.AMPSUPDATED") %>'>
</asp:CheckBox>
</EditItemTemplate>
</asp:TemplateColumn>
Just in case others are working through this as I was currently.Code:Public Sub saveChanges(ByVal sender As Object, ByVal e As System.EventArgs)
'DataGrid1.SelectedIndex = 2
DataGrid1.SelectedIndex = sender.parent.parent.itemindex
'could have the item change saved to the DB here
End Sub
Thanks for all the info on these Forums!! It has been a great resource for me!