Results 1 to 10 of 10

Thread: [2005] CheckBox in DataGrid control

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    134

    Question [2005] CheckBox in DataGrid control

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] CheckBox in DataGrid control

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Lively Member
    Join Date
    Sep 2001
    Location
    manila, philippines
    Posts
    74

    Re: [2005] CheckBox in DataGrid control

    i need this too.
    please help.
    thanks

  4. #4
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] CheckBox in DataGrid control

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    134

    Re: [2005] CheckBox in DataGrid control

    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

  6. #6
    New Member
    Join Date
    Dec 2006
    Posts
    11

    Re: [2005] CheckBox in DataGrid control

    Quote Originally Posted by Reaction
    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

    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    134

    Re: [2005] CheckBox in DataGrid control

    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:
    1. <asp:TemplateField HeaderText="Select">
    2.                             <ItemTemplate>
    3.                                 <asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="ChangeRowColor" />
    4.                             </ItemTemplate>
    5.                         </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

  8. #8
    New Member
    Join Date
    Dec 2006
    Posts
    11

    Re: [2005] CheckBox in DataGrid control

    Quote Originally Posted by Reaction
    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:
    1. <asp:TemplateField HeaderText="Select">
    2.                             <ItemTemplate>
    3.                                 <asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="ChangeRowColor" />
    4.                             </ItemTemplate>
    5.                         </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

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    134

    Re: [2005] CheckBox in DataGrid control

    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
    Last edited by Reaction; May 23rd, 2007 at 11:18 AM.

  10. #10
    New Member
    Join Date
    Aug 2002
    Location
    Gillam , MB Canada
    Posts
    9

    Re: [2005] CheckBox in DataGrid control

    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

    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>
    3) Then in your VB Code window create your Subroutine making sure it is a PUBLIC subroutine.
    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
    Just in case others are working through this as I was currently.

    Thanks for all the info on these Forums!! It has been a great resource for me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width