在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);
注意:在添加列之前,應該確保DataGridView的數據源已經被設置。