DataGridView操作小记(1)

时间:2022-06-10 14:46:51

1、获取总列数

int Column_num = DataGridView1.ColumnCount;

2、获取总行数

int Column_num = DataGridView1.RowCount;

3、获取选中行索引

int selectRows_num = DataGridView1.CurrentRow.Index;

4、获取选中行的全部数据

list<string> Data = new list<string>();

for(int i =0;i<Column_num ;i++)

{

        Data.Add(DataGridView1.Rows[selectRoes_num].Cells[i].Value.Tostring());

}

5、获取某一列的所有的值

List<string> Data = new List<string>();

for(int i =0;i<DataGridView1.Rows.Count;i++)

{

        Data.Add(DataGridView1.Rows[i].Cells[1].Value.Tostring())

}

6、获取表头

List<string> Head = new List<string>();

int Column_num = Tablebase_ADD.ColumnCount;//获取数据表列数
for (int i = 0; i < Column_num; i++)
{
Head.Add(DaTaGridView1.Columns[i].HeaderCell.Value.ToString());
}