关于DataGridView控件的用法

DataGridView控件的应用和问题总结

1.在DataGridView 中通过遍历每一行的指定单元格

foreach (DataGridViewRow row in this.dgvBookList.Rows)
        {
            if (row.Cells["BarCode"].Value.ToString() == this.txtBarCode.Text.Trim())
            {
                row.Cells["BookCount"].Value = Convert.ToInt32(row.Cells["BookCount"].Value) + Convert.ToInt32(this.txtAddCount.Text.Trim());
                this.dgvBookList.Refresh();
            }
        }

2.DataGridView SelectedRow 和 CurrentRow属性区别

用SelectedRows时必须 单击该行最前面的的方格而不能点击表格里有数据的单元格。
用CurrentRow时点哪都行。
SelectedRows表示的是你在DataGridView中所选择的行,其中Count属性是表示的选择的行数
CurrentRow表示的就是你所选择的当前行,只有一行
Rows表示的是DataGridView中所有的行数

3.DateGridView SelectionChanged事件

(1)注意 每当加载DataGridView列表时都会触发多次SelectionChanged事件,导致无故发生异常
(2)解决方案
在加载DataGridView前,先将事件与事件方法断开
this.dgvBookList.SelectionChanged -= new EventHander(this.dgvBookList_SelectionChanged);
加载DataGridView
在加载晚DataGridView后将SelectionChanged事件与方法开启关联
this.dgvBookList.SelectedChanged += new EventHander(this.dgvBookList_SelectionChanged);//只开启关联会发现加载后DataGridView不会刷新
调用一次SelectionChanged方法
dgvBookList_SelectionChanged(null,null);
(3)注意,当删除dgv的某一行时也会触发SelectionChanged事件

4.当对数据库修改信息后,DataGridView同步刷新

//(objStu与List集合里目标对象是同一个地址,所有objStu就是目标对象)
int objStudentId = Convert.ToInt32(this.lbStudentId.Text.Trim());
foreach(Student objStu in this.studentList)
{
	if(objStu.StudentId == objStudentId)
	{
		objStu.StudentName = this.txtStudentName.Text.Trim();
		objStu.StudentAddress = this.txtStudentAddress.Text.Trim();
	}
}
this.dgvStudentInfo.Reflush();//修改可以通过Reflush方法刷新

5.加粗显示相关的借书数据

this.dgvNonReturnList.Columns["BorrowCount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgvNonReturnList.Columns["BorrowCount"].DefaultCellStyle.Font = new Font("微软雅黑",14);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

枫杨NET

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

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

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

打赏作者

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

抵扣说明:

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

余额充值