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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add and Populate Checkbox Column of Datagridview

Status
Not open for further replies.

icodian

IS-IT--Management
Aug 28, 2001
74
US
I have no idea why I'm having so much trouble finding this but I cannot find the code I'm looking for. It seems as though it should be simple.

Anyway, all I want to do is take a boolean field from my database that stores either a 1 or a 0, and display this in my datagridview with a checkbox column. Checked for 1, Unchecked for 0.

Currently, I have a datagrid and am just populating it with my dataset. It currently displays all the columns correctly except the last one "Confirmed" that just shows the 1 or the 0 that is stored in it.

How can I change the type of column for the "Confirmed" column to show checkboxes, and then populate these checkboxes based on the 1 or 0 in the database?

Thank you!

Code:
Dim sqlSx As String
sqlSx = "SELECT sxBookID, AviClientID, AviPetId, Date, Type, LastName, FirstName, PetName, Breed, Notes, VaccStatus, Phone1, Phone2, Confirmed FROM sxBook"

Dim daSx As New SqlDataAdapter(sqlSx, SqlConnPetVillage)
Dim dsSx As New DataSet

SqlConnPetVillage.Open()

daSx.Fill(dsSx, "sxBook")
grdSx.DataSource = dsSx.Tables("sxBook")

VB.NET 2010
 
In the Edit Columns dialog, click on the "Confirmed" column. Then change the ColumnType to DataGridViewCheckBoxColumn. Also, make sure your DataPropertyName is set properly.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Is it a datagrid or a datagridview?
latter one shows checkboxes by default

Zameer Abdulla

 
It is a DataGridView and I'm creating everything programatically so I don't have the columns setup in the control. I actually did figure this out, I just forgot to come back and post how I did it.

Don't know if this is the best way, but here's how I'm doing it.

1. I have Function that creates and returns a table. I set the type to Boolean for the 'Confirmed' column that's created.
Code:
    Function GetDOTable() As DataTable
        'Dropoffs
        ' Create new DataTable instance.
        Dim table As New DataTable
        ' Create four typed columns in the DataTable.
        table.Columns.Add("sxBookID", GetType(Integer))
        table.Columns.Add("AviClientID", GetType(String))
        ........
        table.Columns.Add("Confirmed", GetType(Boolean))

        Return table
    End Function 'GetDOTable

2. And then here's the code that calls it and populates the grid. (I've removed all the extraneous stuff)
Code:
   Public Sub DropOffGridRefill(newDate As Date)
        Dim sqlDO As String = "SELECT sxBookID, ... , Confirmed FROM sxBook"
        daDO = New SqlDataAdapter(sqlDO, SqlConnPetVillage)
        dtDO = GetDOTable()
        daDO.Fill(dtDO)

        If dtDO.Rows.Count > 0 Then
            grdDO.DataSource = dtDO     

            With grdDO
                .Columns(0).Visible = False
                ....
                .Columns(17).Visible = False

                .Columns(5).HeaderText = "Last"
                .....                
                .Columns(14).HeaderText = "Confirmed"

                .Columns(5).Width = 0.08 * .Width
                .....
                .Columns(14).Width = 0.07 * .Width              
            End With
        Else
            grdDO.ClearSelection()
        End If

        SqlConnPetVillage.Close()
End Sub 'DropOffGridRefill
 
If I may suggest, when referencing a column, use its Name or ColumnObject.Index.
Code:
.Columns("ColumnName").Visible = False
or
.ColumnName.Visible = False
Having had to follow and fix a previous coder's form that used numbers, it was a pain to follow and figure out exactly what was going where.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Thanks for the tip. I'm new to VB.NET and definitely appreciate anything like this. It will make it a lot easier when I look at it a few months down the road. Appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top