https://blog.csdn.net/alisa525/article/details/7350471
dataGridView1.ReadOnly = true; // disable editing functions
Method One: Add Datatable manually, and then bind dataGridView
DataTable dt = new DataTable (); // build data tables
dt.Columns.Add (new DataColumn ( "id", typeof (int))); // add a column of type int table
dt.Columns.Add (new DataColumn ( "Name", typeof (string))); // add string type of the Name column in the table
DataRow dr; // row
for (int I = 0; I <. 3; I ++)
{
DR = dt.NewRow ();
DR [ "ID"] = I;
DR [ "the Name"] = "the Name" + I;
dt.Rows.Add (dr); // add this line of the table's row object
}
dataGridView1.DataSource = dt;
If you want to add a column textbox effect, do the following processing
dt.Columns.Add(new DataColumn("选中", typeof(bool));
Method two: directly inserted in the dataGridView
dataGridView1.ColumnCount = 4;
dataGridView1.ColumnHeadersVisible = true;
// Set the column header style.
DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
columnHeaderStyle.BackColor = Color.Beige;
columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
// Set the column header names.
dataGridView1.Columns[0].Name = "Recipe";
dataGridView1.Columns[1].Name = "Category";
dataGridView1.Columns[2].Name = "Main Ingredients";
dataGridView1.Columns[3].Name = "Rating";
// Populate the rows.
string[] row1 = new string[] { "Meatloaf", "Main Dish", "ground beef",
"**" };
string[] row2 = new string[] { "Key Lime Pie", "Dessert",
"lime juice, evaporated milk", "****" };
string[] row3 = new string[] { "Orange-Salsa Pork Chops", "Main Dish",
"pork chops, salsa, orange juice", "****" };
string[] row4 = new string[] { "Black Bean and Rice Salad", "Salad",
"black beans, brown rice", "****" };
string[] row5 = new string[] { "Chocolate Cheesecake", "Dessert",
"cream cheese", "***" };
string[] row6 = new string[] { "Black Bean Dip", "Appetizer",
"black beans, sour cream", "***" };
object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };
foreach (string[] rowArray in rows)
{
dataGridView1.Rows.Add(rowArray);
}
Column inserted DataGridViewCheckBoxColumn
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
{
column.HeaderText = "选中";
column.Name = isSelected;
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
column.FlatStyle = FlatStyle.Standard;
column.ThreeState = true;
column.CellTemplate = new DataGridViewCheckBoxCell();
column.CellTemplate.Style.BackColor = Color.Beige;
}
DataGridView1.Columns.Insert(0, column);