Datagridview的使用方法

本文详细介绍如何使用DataGridView控件自动生成行列、修改列名、添加行数据及获取单元格内容等关键操作,帮助开发者快速掌握DataGridView的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自动添加行列

	private void Form1_Load(object sender, EventArgs e)
	        {
	            DataGridViewColumnCollection SS = this.dataGridView1.Columns;
	            SS.Add("第一列", "姓名");
	            SS.Add("第二列", "地址");
	            SS.Add("第三列", "男女");
	            SS.Add("第四列", "电话");
	     	  }

在这里插入图片描述
窗体加载的时候,自动生成几列
在这里插入图片描述
这个不是我的图片是网上的案例的结果(方便理解用的,第一列 第二次 就是name headertext是姓名 地址 )
在这里插入图片描述
2 如果需要修改表格名字
如下:

 private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewColumnCollection SS = this.dataGridView1.Columns;
            SS.Add("第一列", "姓名");
            SS.Add("第二列", "地址");
            SS.Add("第三列", "男女");
            SS.Add("第四列", "电话");

        this.dataGridView1.Columns[0].HeaderCell.Value = "姓名";
        this.dataGridView1.Columns[1].HeaderCell.Value = "字段3";
        this.dataGridView1.Columns[2].HeaderCell.Value = "字段4";
        this.dataGridView1.Columns[3].HeaderCell.Value = "字段5";
    }

在这里插入图片描述
代码自动生成行数

 DataGridViewColumnCollection SS = this.dataGridView1.Columns;
            SS.Add("第一列", "姓名");
            SS.Add("第二列", "地址");
            SS.Add("第三列", "男女");
            SS.Add("第四列", "电话");
            this.dataGridView1.Rows.Add(5)

在这里插入图片描述

 DataGridViewColumnCollection SS = this.dataGridView1.Columns;
            SS.Add("第一列", "姓名");
            SS.Add("第二列", "地址");
            SS.Add("第三列", "男女");
            SS.Add("第四列", "电话");
       
        int index = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index].Cells[0].Value = "1";

        this.dataGridView1.Rows[index].Cells[1].Value = "2";

        this.dataGridView1.Rows[index].Cells[2].Value = "监听";
        this.dataGridView1.Rows[index].Cells[3].Value = "151234567";

利用dataGridView1.Rows.Add()事件为DataGridView控件增加新的行,该函数返回添加新行的索引号,即新行的行号,然后可以通过该索引号操作该行的各个单元格,如dataGridView1.Rows[index].Cells[0].Value = “1”。这是很常用也是很简单的方法。
在这里插入图片描述

 DataGridViewColumnCollection SS = this.dataGridView1.Columns;
            SS.Add("第一列", "姓名");
            SS.Add("第二列", "地址");
            SS.Add("第三列", "男女");
            SS.Add("第四列", "电话");
            DataGridViewRow row = new DataGridViewRow();
            DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
            textboxcell.Value = "aaa";
            row.Cells.Add(textboxcell);
            DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
            row.Cells.Add(comboxcell);
            DataGridViewTextBoxCell textboxcel22 = new DataGridViewTextBoxCell();
            textboxcel22.Value = "aa1a";
            row.Cells.Add(textboxcel22);
            dataGridView1.Rows.Add(row);

在这里插入图片描述

DataGridView 取得或者修改当前单元格的内容:

当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 CurrentCell 属性取得。如果当前单元格不存在的时候,返回null

  private void Form1_Load(object sender, EventArgs e)
        {

        DataGridViewColumnCollection SS = this.dataGridView1.Columns;
        SS.Add("第一列", "姓名");
        SS.Add("第二列", "地址");
        SS.Add("第三列", "男女");
        SS.Add("第四列", "电话");
        int index = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index].Cells[0].Value = "1";

        this.dataGridView1.Rows[index].Cells[1].Value = "2";

        this.dataGridView1.Rows[index].Cells[2].Value = "nan";
        this.dataGridView1.Rows[index].Cells[3].Value = "151234567";
        int index1 = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index1].Cells[0].Value = "1";

        this.dataGridView1.Rows[index1].Cells[1].Value = "2";

        this.dataGridView1.Rows[index1].Cells[2].Value = "nv";
        this.dataGridView1.Rows[index1].Cells[3].Value = "151234567";
    }

    private void button3_Click(object sender, EventArgs e)
    {
    

MessageBox.Show(this.dataGridView1.CurrentCell.Value.ToString());
    }

在这里插入图片描述

// 取得当前单元格的列 Index
Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex);
// 取得当前单元格的行 Index
Console.WriteLine(DataGridView1.CurrentCell.RowIndex);

 private void Form1_Load(object sender, EventArgs e)
        {

        DataGridViewColumnCollection SS = this.dataGridView1.Columns;
        SS.Add("第一列", "姓名");
        SS.Add("第二列", "地址");
        SS.Add("第三列", "男女");
        SS.Add("第四列", "电话");
        int index = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index].Cells[0].Value = "1";

        this.dataGridView1.Rows[index].Cells[1].Value = "2";

        this.dataGridView1.Rows[index].Cells[2].Value = "nan";
        this.dataGridView1.Rows[index].Cells[3].Value = "151234567";
        int index1 = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index1].Cells[0].Value = "1";

        this.dataGridView1.Rows[index1].Cells[1].Value = "2";

        this.dataGridView1.Rows[index1].Cells[2].Value = "nv";
        this.dataGridView1.Rows[index1].Cells[3].Value = "151234567";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        // 取得当前单元格的列 Index 
        //Console.WriteLine(dataGridView1.CurrentCell.ColumnIndex);
        MessageBox.Show("列"+dataGridView1.CurrentCell.ColumnIndex.ToString()+"行"+dataGridView1.CurrentCell.RowIndex.ToString());
    }
}

在这里插入图片描述

private void Form1_Load(object sender, EventArgs e)
{

        DataGridViewColumnCollection SS = this.dataGridView1.Columns;
        SS.Add("第一列", "数1字");
        SS.Add("第二列", "数2字");
        SS.Add("第三列", "数3字");
        SS.Add("第四列", "数4字");
        int index = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index].Cells[0].Value = "1";

        this.dataGridView1.Rows[index].Cells[1].Value = "2";

        this.dataGridView1.Rows[index].Cells[2].Value = "5";
        this.dataGridView1.Rows[index].Cells[3].Value = "0";
        int index1 = this.dataGridView1.Rows.Add();
        this.dataGridView1.Rows[index1].Cells[0].Value = "1";

        this.dataGridView1.Rows[index1].Cells[1].Value = "2";

        this.dataGridView1.Rows[index1].Cells[2].Value = "7";
        this.dataGridView1.Rows[index1].Cells[3].Value = "1";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        int cc = this.dataGridView1.CurrentCell.RowIndex;
        
      int bb=    dataGridView1.Rows[cc].Cells.Count;
        int sum = 0;
        for (int i = 0; i < bb; i++)
        {
            var tt = dataGridView1.Rows[cc].Cells[i].Value;
            int yy = Convert.ToInt32(tt );

            if (yy!=null)
            {
                sum = sum + yy;
               
            }
            else
            {
                MessageBox.Show("vvvvvv");

            }

            
        }
        MessageBox.Show(sum.ToString());

在这里插入图片描述

例如获得第一行第二列的值
dataGridView1.Rows[0].Cells[1].value

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值