Reputation: 4655
I am unable to correctly populate a DataGridView checkbox column from a boolean column from a database.
First form_load code:
Me.DataGridView1.DataSource = Me.bindingSource1
GetData("SELECT myInt, myBool, myString " & _
"FROM " & myFavTable & " " & _
"WHERE (myInt > 100) ORDER BY myString")
formatGrid()
In GetData I fill myTable with data:
Me.dataAdapter.Fill(myTable)
Me.bindingSource1.DataSource = myTable
And finally I format grid the before showing.
I format it manually because loading is much faster than with automatic formatting.
With DataGridView1
.AllowUserToAddRows = False
.AllowDrop = False
.AllowUserToOrderColumns = False
.AllowUserToResizeRows = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.Dock = DockStyle.Fill
.EditMode = DataGridViewEditMode.EditProgrammatically
With .Columns(0)
.Name = "postN"
.HeaderText = "Postal"
.Width = 55
End With
With .Columns(1) 'here should be a checkbox
.Width = 20
End With
With .Columns(2)
.Name = "colCity"
.HeaderText = "City"
.Width = 180
End With
End With
But with this code, in my column that should show checkboxes the string value 0
is displayed when in database is FALSE
.
How in this situation can I get checkboxes in the middle column instead of text?
I try with .Columns.Add... before and after binding but with no wanted results.
That way I can get checkboxes
, but in the new column.
Upvotes: 1
Views: 22564
Reputation: 131
I had the same problem. I had a DataSet that it was filling with this SQL:
"SELECT nombre, CASE WHEN fecha IS NULL THEN 0 ELSE 1 END AS baja"
Assignment
dtgEmpleado.DataSource = ds.Tables(0)
With dtgEmpleado
.Columns(0).HeaderText = "Nombre"
.Columns(0).DataPropertyName = "nombre"
.Columns(0).Name = "nombre"
.Columns(0).Width = 100
.Columns(1).HeaderText = "Baja"
.Columns(1).DataPropertyName = "baja"
.Columns(1).Name = "baja"
.Columns(1).Width = 70
End With
I wanted that the column "Baja" it was displaying as a "Checkbox".
I could do it with:
AutoGenerateColumns = False
But an easier way, changing the SQL sentence:
"SELECT nombre, CAST(CASE WHEN fecha IS NULL THEN 0 ELSE 1 END AS BIT) AS baja"
Upvotes: 2
Reputation: 65554
In design-time add the columns to the DataGridView and set the middle column as a CheckBoxColumn.
Then set:
With DataGridView1
.AutoGenerateColumns = False
Edit: I see the problem now. You need to set the DataPropertyName to be the same as the column.
When you add columns to the DataGridView, in that dialog set the DataPropertyName to match the DataTable (myTable) column Names. That's the magic behind the mapping.
Here is the code:
DataTable dt = new DataTable();
dt.Columns.Add("TextBoxCol");
dt.Columns.Add("CheckBoxCol");
DataRow dr = dt.NewRow();
dr[0] = "Hello";
dr[1] = false;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "World";
dr[1] = true;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
Upvotes: 2
Reputation: 7082
Dim cell As DataGridViewCell = New DataGridViewCheckBoxCell()
With DataGridView1
With .Columns(1)
.CellTemplate = cell
End With
End With
EDIT:
This a suggestion, don't try to add columns at design-time in your DataGridView
because you query itself it will generate a DataGridViewCheckBoxCell
GetData("SELECT myInt AS Id, myBool AS Bool, myString AS String " & _
"FROM " & myFavTable & " " & _
"WHERE (myInt > 100) ORDER BY myString")
Me.dataAdapter.Fill(myTable)
Me.bindingSource1.DataSource = myTable
DataGridView1.DataSource = bindingSource1;
Upvotes: 1