记一些C#窗体应用编程中的小问题。
this.dataGridView1.Rows[i].Selected = true;
后,在实际显示的时候,确实第i
被选中,然而通过CurrentRow
取值都是第一行,而且查看选中行标确实0this.dataGridView1.Rows[i].Selected = true;
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[i].Cells[0]; // 关键
DataBindingComplete
事件this.dataGridView1.ClearSelection();
,也可以在其他地方调用清空选择行。KeyPress
事件,其中实现private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)
}
关于带有小数点的数字校验可查看winForm控制输入框只接受数字输入
Sorted
事件,在该事件中重新设置显示效果。DataTable dt = this.dataGridView.DataSource as DataTable
, 这句话可以将DataGridView的的数据转到dt中,其实dt所持有的引用就是DataGridView的数据源,也即dt如果发生了改变,DataGridView也会发生改变。DataTable dt = (this.dataGridView.DataSource as DataTable).copy()
dt.DefaultView.Sort = "BH DESC";
按照编号降序排序(控制默认最小), 注意此时的dt并没有按照意愿排序,因为还差一步
dt = dt.DefaultView.ToTable();
需要重新ToTable();
DataRow[] drs = dt.Select(" BH is not null"); // 查询BH不为空的记录
返回的是DataRow 数组
未完,待续。。。
原文:https://www.cnblogs.com/numen-fan/p/11395713.html