string[] header = new[] { "Column1", "Column2", "Column3" }; MyTable = new DataTable(); foreach (string str in header) MyTable.Columns.Add(str, typeof(string)); MyTable.Rows.Add({"abc","abc1","abc2"}); f_DatagridInfo.DataSource = MyTable; this.f_DatagridInfo.Columns.Add(new GridCheckBoxColumn() { MappingName = "Active", HeaderText = "Active", CheckBoxSize = new Size(12, 12) });
Dim header() As String = {"Column1", "Column2", "Column3"} Mytable = New DataTable Dim i As SByte For Each str As String In header MyTable.Columns.Add(str, GetType(String)) Next MyTable.Columns.Add("Active", GetType(Boolean)) f_DatagridInfo.DataSource = MyTable Dim addNewRow As System.Data.DataRow = TryCast(f_DatagridInfo.View.AddNew(), System.Data.DataRow) f_DatagridInfo.View.CommitNew() addNewRow.SetField(0, "abc1") addNewRow.SetField(1, "abc2") addNewRow.SetField(2, "abc3") addNewRow.SetField(3, false)Edit:question 2 has been solved, but I want to use question 1 because the checkbox size here is quite large (It does not automatically resize with the office2019colorful theme)
|
public partial class Form1 : Form
{
Random r = new Random();
public DataTable dt;
public Form1()
{
InitializeComponent();
dt = new DataTable();
dt.Columns.Add("EmployeeID", typeof(int));
dt.Columns.Add("EmployeeName", typeof(string));
dt.Columns.Add("Destination", typeof(string));
dt.Columns.Add("IsChecked", typeof(bool));
for (int i = 0; i < 10; i++)
{
System.Data.DataRow row = dt.NewRow();
var name = employeeName[r.Next(employeeName.Count() - 1)];
var destinaton = title[r.Next(title.Count() - 1)];
row[0] = 1000 + i;
row[1] = name;
row[2] = destinaton;
row[3] = i % 2 == 0 ? true : false;
dt.Rows.Add(row);
}
this.sfDataGrid1.AutoGenerateColumns = true;
this.sfDataGrid1.AutoGeneratingColumn += SfDataGrid1_AutoGeneratingColumn;
this.sfDataGrid1.DataSource = dt;
}
private void SfDataGrid1_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
{
if (e.Column is GridCheckBoxColumn)
(e.Column as GridCheckBoxColumn).CheckBoxSize = new Size(12, 12);
}
|
f_DatagridInfo.DataSource = MyTable
TryCast(f_DatagridInfo.Columns(3), GridCheckBoxColumn).CheckBoxSize = New Size(12, 12)