winfrom DataGridview使用方法

DataGridview 添加列

参考网址

数据填充

			foreach (var item in data)
            {
            // 循环之前提前创建DataGridViewRow代表我要创建一行数据
                DataGridViewRow dr = new DataGridViewRow();
                int index = DataGridview1.Rows.Add(dr);
                //拿下面第一行举例 index 代表第几行,意思是第index行的第0列(初始位置0开始),赋ID值
                DataGridview1.Rows[index].Cells[0].Value = item.ID;
                DataGridview1.Rows[index].Cells[1].Value = item.Name;
                DataGridview1.Rows[index].Cells[2].Value = item.Age;
                DataGridview1.Rows[index].Cells[3].Value = item.Sex;
            }

获取选中行数

	this.DataGridview1.CurrentRow.Index

获取选中列数

this.DataGridview1.CurrentCell.ColumnIndex;

设置指定单元格背景颜色

DataGridview1.Rows[index].Cells[6].Style.BackColor = Color.Yellow;

效果
在这里插入图片描述

更多用法后续会更新…

在WinForm中,DataGridView是一个非常常用的控件,用于显示和编辑数据。要使用DataGridView控件,你需要遵循以下几个步骤: 1. 在窗体上添加一个DataGridView控件。 2. 通过编程的方式或者通过设计器,设置DataGridView的属性和样式,如列的数量和类型、行的数量、单元格的样式等。 3. 将数据绑定到DataGridView控件上。可以通过设置DataGridView的DataSource属性,将数据源对象绑定到控件上,并通过调用DataGridView的DataBind方法更新数据。 4. 处理DataGridView的事件,以实现你所需要的功能。例如,你可以使用DataGridView的CellClick事件来处理单元格的点击事件,使用CellValueChanged事件来处理单元格值的变化事件等。 引用中的代码 `dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());` 用于在DataGridView的第一列之前插入一个复选框列。 引用提到的DataGridViewCell、DataGridViewRow、DataGridViewColumn和它们的派生类是用于访问和操作DataGridView控件中的单元格、行和列的相关属性和方法的类。 引用中的代码 `if (dataGridView1[e.ColumnIndex, e.RowIndex].ErrorText != String.Empty)` 则是在处理DataGridView的某个特定事件时,判断某个单元格是否有错误信息。 综上所述,要在WinForm中使用DataGridView控件,你需要添加控件、设置属性和样式、绑定数据、处理事件,并可以使用相关类来访问和操作单元格、行和列的属性和方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [DataGridView控件使用大全](https://blog.csdn.net/weixin_34272308/article/details/85482365)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大蘋果

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值