-
Jan 24th, 2018, 12:38 PM
#1
Thread Starter
Addicted Member
Struggeling with DataGridView Check Box Column.
Afternoon all,
I have a datagridview that is populated from a SQL server via a datatable. I would like to add a Check Box Column with the idea to use the check boxes to do a multiple row selection. So far i've managed to add the column but I seem unable to check any of the check boxes. Any idea what I'm doing wrong?
Thanks
Dave
Code:
Dim EmployeeDT As New DataTable
Dim EmployeeList As New EmployeeDetails
V_EmployeeDGV.DataSource = Nothing
V_EmployeeDGV.Rows.Clear()
EmployeeDT = EmployeeList.GetEmployee_List(V_SIDTB.Text)
V_EmployeeDGV.DataSource = EmployeeDT
V_EmployeeDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
V_EmployeeDGV.Columns("T_TID").Visible = False
Dim ChkBoxCol As New DataGridViewCheckBoxColumn
With ChkBoxCol
.FlatStyle = FlatStyle.Standard
.HeaderText = "Select"
.Width = 30
End With
V_EmployeeDGV.Columns.Insert(0, ChkBoxCol)
V_EmployeeDGV.ClearSelection()
-
Jan 24th, 2018, 01:40 PM
#2
Re: Struggeling with DataGridView Check Box Column.
Hi,
this might help...
Code:
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''add checkbox
checkBoxColumn.HeaderText = "Select"
checkBoxColumn.Width = 30
checkBoxColumn.Name = "Select"
DataGridView1.Columns.Insert(3, checkBoxColumn)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim message As String = String.Empty
For Each row As DataGridViewRow In DataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("Select").Value)
If isSelected Then
message &= Environment.NewLine
'I have a ContactName in Cell 2
message &= row.Cells("ContactName").Value.ToString()
End If
Next
'return all the selected Contacts
MessageBox.Show("Selected Contact :" & message)
End Sub
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jan 24th, 2018, 02:23 PM
#3
Re: Struggeling with DataGridView Check Box Column.
The DataGridViewCheckBoxColumn does have have a ReadOnly property, for some reason it may have defaulted to True or you might try setting it to False. Mine doesn't but it's worth testing.
-
Jan 25th, 2018, 06:45 AM
#4
Re: Struggeling with DataGridView Check Box Column.
Here is how I would do this, load a DataTable from the database table, add a new DataColumn to the DataTable.
Code:
Dim dt As DataTable = GetData()
Add the column, set default values for each row to false and place the column at position 0 (or change it to 3 as shown in your initial post).
Code:
dt.Columns.Add(New DataColumn With {
.ColumnName = "Process", .DataType = GetType(Boolean)})
dt.Columns(CheckBoxName).SetOrdinal(0)
For Each row As DataRow In dt.Rows
row.SetField(Of Boolean)(CheckBoxName, False)
Next
Assign the data table to the DataGridView DataSource
Code:
DataGridView1.DataSource = dt
Later
Code:
Dim Checked As List(Of DataRow) =
(
From Rows In CType(DataGridView1.DataSource, DataTable)
Where Rows.Field(Of Boolean)("Process")
Select Rows
).ToList
-
Jan 25th, 2018, 07:47 AM
#5
Thread Starter
Addicted Member
Re: Struggeling with DataGridView Check Box Column.
Thanks for your replies.
@Karen, I never thought of adding the column to the datatable...!
Anyhow, tried everything but still I can't check or un-check the checkbox. I though I'd dumb it right down:
Code:
V_EmployeeDGV.ReadOnly = False
Dim ChkBoxCol As New DataGridViewCheckBoxColumn With {.FlatStyle = FlatStyle.Standard, .HeaderText = "Select", .Width = 30}
V_EmployeeDGV.Columns.Insert(0, ChkBoxCol)
V_EmployeeDGV.Rows.Add()
A datagridview is created with 1 row and 1 column with 1 checkbox but I'm still unable to check or uncheck it. Something a bit weird is going on here.....
Thanks
Dave
-
Jan 25th, 2018, 08:26 AM
#6
Re: Struggeling with DataGridView Check Box Column.
Are you able to edit cells in other columns?
-
Jan 25th, 2018, 08:33 AM
#7
Re: Struggeling with DataGridView Check Box Column.
Dave,
Follow Karens example, why are you still adding the CBcolumn to the dgv?
Code:
Public Class DGV_CheckBox
Dim DT As New DataTable
Private Sub DGV_CheckBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DT.Columns.Add("Select", GetType(System.Boolean))
DT.Columns("Select").DefaultValue = False
DataGridView1.DataSource = DT
End Sub
End Class
-
Jan 25th, 2018, 09:15 AM
#8
Thread Starter
Addicted Member
Re: Struggeling with DataGridView Check Box Column.
Found it..... 'Editmode' was set to 'EditProgramaticaly'. Changed it to 'KeystrokeOrF2' so far it looks to be working as it should.....
-
Jan 25th, 2018, 10:51 AM
#9
Re: Struggeling with DataGridView Check Box Column.
Originally Posted by QuattroDave
Thanks for your replies.
@Karen, I never thought of adding the column to the datatable...!
Anyhow, tried everything but still I can't check or un-check the checkbox. I though I'd dumb it right down:
Code:
V_EmployeeDGV.ReadOnly = False
Dim ChkBoxCol As New DataGridViewCheckBoxColumn With {.FlatStyle = FlatStyle.Standard, .HeaderText = "Select", .Width = 30}
V_EmployeeDGV.Columns.Insert(0, ChkBoxCol)
V_EmployeeDGV.Rows.Add()
A datagridview is created with 1 row and 1 column with 1 checkbox but I'm still unable to check or uncheck it. Something a bit weird is going on here.....
Thanks
Dave
Hi Dave, I've always recommended working from the DataTable rather than the DataGridView e.g. give the DataGridView a Boolean column in a DataTable without defining a DataGridView columns and you will have a DataGridViewCheckBoxColumn. There is nothing wrong with setting display properties for the DataGridView and events that is about if.
Check out my MSDN code sample for more on the above.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|