在C#中,可以通過以下代碼來添加列到DataGridView:
dataGridView1.Columns.Add("ColumnName", "Column Header Text");
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = "ColumnName";
column.HeaderText = "Column Header Text";
dataGridView1.Columns.Add(column);
DataGridViewColumn column = new DataGridViewColumn();
column.Name = "ColumnName";
column.HeaderText = "Column Header Text";
column.CellTemplate = new DataGridViewTextBoxCell();
dataGridView1.Columns.Add(column);
注意:在添加列之前,應(yīng)該確保DataGridView的數(shù)據(jù)源已經(jīng)被設(shè)置。