Hi As you mensioned above;
I create a demo and seems works well, hope it help you.
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Text;
System.Windows.Forms;
WindowsApplication1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable TB = new DataTable();
TB.Columns.Add("checked", typeof(bool));
TB.Columns.Add("name", typeof(String));
for (int i = 0; i <= 5; i++)
{
DataRow row = TB.NewRow();
if (i % 2==1)
row["checked"] = false;
else
row["checked"] = true;
row["name"] = "Name" + " " + i.ToString();
TB.Rows.Add(row);
}
this.dataGridView1.DataSource = TB.DefaultView;
dataGridView1.Columns.Clear();
DataGridViewCheckBoxColumn CC = new DataGridViewCheckBoxColumn();
CC.DataPropertyName = "checked";
DataGridViewTextBoxColumn TC = new DataGridViewTextBoxColumn();
TC.DataPropertyName = "name";
this.dataGridView1.Columns.Add(CC);
this.dataGridView1.Columns.Add(TC);
dataGridView1.AllowUserToAddRows = false;
}
}
Your code works, i have successfully passed the data to DataTable but i can't bound the data to the DataGridColumn, it seems quite messy because the Check value displayed as "True" or "False" in my Name DataGridTextColumn and my Check Column is empty without any data. May i know why? I have gone through the code for several times, the codes is shown as the following and there is no error for the bound. Thank you.
I realized that the problem might be caused by the checkbox, if i remove the DataProperty of the CheckBox then it works fine. Can i know how to tick on and off the CheckBox based on the DataTable check data? How to set the dataproperty of the checkColumn equals to a set of true of false values stored in database or datatable and tick on and off according to those values? Thank you.
Dim i As Integer
Dim DT As New DataTable
Dim DRow As DataRow
DT.Columns.Add(
"name", GetType(String))
DT.Columns.Add(
"check", GetType(Boolean))
For i = 0 To 5
DRow = DT.NewRow
DRow(
"name") = "name" + i.ToString
If i = 1 Then
DRow(
"check") = False
Else
DRow(
"check") = True
End If
DT.Rows.Add(DRow)
Next i
DataGridView1.DataSource = DT.DefaultView
DataGridView1.Columns.Clear()
Dim DGVTextColumn1 As New DataGridViewTextBoxColumn
DGVTextColumn1.Name =
"Name"
DGVTextColumn1.DataPropertyName =
"name"
DGVTextColumn1.Width =
"100"
DataGridView1.Columns.Insert(0, DGVTextColumn1)
Dim DGVLinkColumn As New DataGridViewTextBoxColumn
DGVLinkColumn.Name =
"Check"
DGVTextColumn1.DataPropertyName =
"check"
DGVLinkColumn.Width =
"80"
DGVLinkColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Columns.Insert(1, DGVLinkColumn)