C# DataGridView表格控件

常用属性

不显示最后下空白行

dataGridView.AllowUserToAddRows = false;

不显示第一列前面的空白列

dataGridView.RowHeadersVisible = false;

禁止改变行高

dataGridView.AllowUserToResizeRows = false;

选择模式

属性:SelectionMode,默认是选中单元格

  • CellSelect: 只支持单元格选取, 不能整行或者整列选取

  • FullRowSelect: 只能整行选取

  • FullColumnSelect: 只能整列选取

  • RowHeaderSelect: 同时支持整行选取和单元格选取 (等于 CellSelect + FullRowSelect)

  • ColumnHeaderSelect: 同时支持整列选取和单元格选取 (等于Cell Select + FullColumnSelect)

设置背景色

设置为白色:

dataGridView.BackgroundColor = System.Drawing.Color.White;

设置为系统色:

dataGridView.BackgroundColor = System.Drawing.SystemColors.Window;

添加表头- 普通方式

// 设置有多少列
dataGridView.ColumnCount = heads.Length;

for (int i = 0; i< heads.Length; i++)
{
    // 循环方式设置表头
    dataGridView.Columns[i].HeaderText = heads[i];
}

添加表头- 高级方式

该列是文字

DataGridViewTextBoxColumn cell2 = new DataGridViewTextBoxColumn();
// 表头显示文字
cell2.HeaderText = "姓名";
// 表头宽度
cell2.Width = 200;
selectColDataGridView.Columns.Add(cell2);

该列是下拉框

DataGridViewComboBoxColumn cell3 = new DataGridViewComboBoxColumn();
cell3.HeaderText = "性别";
cell3.Width = 80;
selectColDataGridView.Columns.Add(cell3);

绑定简单数据源:

List<string> ListData = new List<string> { "男", "女" };
// 设置combox的数据源,显示下拉框的内容
cell3.DataSource = ListData;

绑定key、value数据源,list里面一般是对象,这里为了简单,使用匿名对象

List<object> list = new List<object> {
    new { display = "男", value = "m" },
    new { display = "女",value = "f" }
};

comboBox1.DataSource = list;
// 指定value值
comboBox1.ValueMember = "value";
// 指定显示值
comboBox1.DisplayMember = "display";

设置列隐藏

设置第一列隐藏

resultTable.Columns[0].Visible = false

没有显示值和隐藏值

本控件不像 combobox 控件 或 HTML的 select 控件,可以设置显示值和隐藏值,可通过隐藏列实现类似功能

添加数据

普通方式

// 添加一行
int index = resultTable.Rows.Add();
// 给该行的第一个单元格设置值
resultTable.Rows[index].Cells[0].Value = result.Item2;
// 给该行的第二个单元格设置值
resultTable.Rows[index].Cells[1].Value = result.Item3;

获取操作

获取总共行数

dataGridView.Rows.Count

获得某个(指定的)单元格的值

dataGridView1.Row[i].Cells[j].Value;

获得选中的总行数

dataGridView1.SelectedRows.Count;

获得当前选中行的索引:

dataGridView1.CurrentRow.Index;

获得当前选中单元格的值:

dataGridView1.CurrentCell.Value;

取选中行的数据

设置选中模式:将 SelectionMode 设置为 FullRowSelect,否则取不到值

// 遍历选中的行
foreach (DataGridViewRow item in resultTable.SelectedRows)
{
    // 获取选中行的第一列
    string file = item.Cells[0].Value.ToString();

}

获取选中行的某个数据

int a = dataGridView1.SelectedRows.Index;
dataGridView1.Rows[a].Cells["你想要的某一列的索引,想要几就写几"].Value;

获得某个(指定的)单元格的值

dataGridView1.Rows[i].Cells[j].Value;

int a=dataGridView1.CurrentRow.Index;
string str=dataGridView1.Row[a].Cells["strName"].Value.Tostring();

selectedRows[0]当前选中的行
.cell[列索引].values 就是当前选中行的某个单元格的值 

DataGridView1.SelectedCells(0).Value.ToString 取当前选择单元内容
DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString 当前选择单元第N列内容

删除

根据索引删除行

dataGridView.Rows.RemoveAt(index);

根据Row删除行

dataGridView.Rows.Remove(row对象);

删除选中的行

dataGridView.Rows.Remove(dataGridView.CurrentRow);

清空所有表头

selectColDataGridView.Columns.Clear();

清空所有数据

resultTable.Rows.Clear();

遍历

IList<string[]> list = new List<string[]>();
foreach(DataGridViewRow row in dataGridView.Rows){
    string[] arr = new string[] 
    {
        // 取出当前行第一个单元格
        row.Cells[0].Value as string,
        row.Cells[1].Value as string,
        row.Cells[2].Value as string,
        row.Cells[3].Value as string,
        row.Cells[4].Value as string,
        row.Cells[5].Value as string
    };
    list.Add(arr);
}

其他操作

判断是否选中行

if (resultTable.CurrentRow == null)
{
    MessageBox.Show("请选中要打开的文件!", "提示",
        MessageBoxButtons.OK,
        MessageBoxIcon.Information);
    return;
}

设置点击表头不排序

设置表头时,通过 DataGridViewTextBoxColumn 的 SortMode 设置排序方式:

DataGridViewTextBoxColumn cell2 = new DataGridViewTextBoxColumn();
cell2.HeaderText = "列";
cell2.Width = 200;
selectColDataGridView.Columns.Add(cell2);
cell2.SortMode= DataGridViewColumnSortMode.NotSortable;

事件

  • CellDoubleClick:双击行事件

参考:

https://blog.csdn.net/hejisan/article/details/52688972
https://blog.csdn.net/alai7150/article/details/89514165