110,533
社区成员
发帖
与我相关
我的任务
分享
//dataGridView1.Rows[0].Selected = false; //放在Form1这里不起效果 可放在下面的Form1_Load中
this.button1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows[0].Selected = false;
//this.dataGridView1.TabStop = false;
//dataGridView1.Rows[0].Selected = false;
//this.dataGridView1.CurrentCell = null;
//dataGridView1.ClearSelection();
}
表格不是用绑数据库来显示的,是自己一条条插入的!
表格不是用绑数据库来显示的,是自己一条条插入的!
private DataGridViewCell clickedCell;
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// If the user right-clicks a cell, store it for use by the shortcut menu.
if (e.Button == MouseButtons.Right)
{
DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell)
{
clickedCell =
dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
}
}
}
[quote=引用 19 楼 youzelin 的回复:] dataGridView1.Rows[(dataGridView1.DataSource as DataTable).Rows.Count - 1].Selected = true; 这个试过吗?反正我是试过了,okay 的。
难道不是GridView.FocusedRow = -1???
dataGridView1.Rows[(dataGridView1.DataSource as DataTable).Rows.Count - 1].Selected = true; 这个试过吗?反正我是试过了,okay 的。
[quote=引用 7 楼 happy09li 的回复:] [quote=引用 5 楼 zxvyio 的回复:] [quote=引用 3 楼 guwei4037 的回复:] this.dataGridView1.DataSource = dt.DefaultView; this.dataGridView1.ClearSelection();//在绑定数据源之后加上这么一句
if (DINdataGridView.CurrentCell == null) { DINdataGridView.Rows[2].Selected = true; MessageBox.Show("没有选中行"); }
if (DINdataGridView.CurrentCell == null) { DINdataGridView.Rows[2].Selected = true; MessageBox.Show("没有选中行2"); }
但是第二条的IF语句,还是运行了,显示“没有选中行2”,那说明第一条IF的Selected = true 没有起作用,怎么会这样呢?[/quote]
不要用DataGridView自带selected 判断,在mousedown时间里面写判断,[/quote]
这是表格加载完自动去除默认选中第一行,不能用mousedown来做![/quote]
你现在要的是判断,又不是要第一行不选中的效果,在mosedown中把当前选中的行记录起来[quote=引用 11 楼 zxvyio 的回复:] [quote=引用 7 楼 happy09li 的回复:] [quote=引用 5 楼 zxvyio 的回复:] [quote=引用 3 楼 guwei4037 的回复:] this.dataGridView1.DataSource = dt.DefaultView; this.dataGridView1.ClearSelection();//在绑定数据源之后加上这么一句
if (DINdataGridView.CurrentCell == null) { DINdataGridView.Rows[2].Selected = true; MessageBox.Show("没有选中行"); }
if (DINdataGridView.CurrentCell == null) { DINdataGridView.Rows[2].Selected = true; MessageBox.Show("没有选中行2"); }
但是第二条的IF语句,还是运行了,显示“没有选中行2”,那说明第一条IF的Selected = true 没有起作用,怎么会这样呢?[/quote]
不要用DataGridView自带selected 判断,在mousedown时间里面写判断,[/quote]
这是表格加载完自动去除默认选中第一行,不能用mousedown来做![/quote]
你现在要的是判断,又不是要第一行不选中的效果,在mosedown中把当前选中的行记录起来[/quote]
刚好相反,是要第一行不选中的效果,不是要判断!上面的加载完简单的方法都试过了,都不行,有高级的方法吗?加载完成后,加上 this.dataGridView1.ClearSelection();不就行了
[quote=引用 5 楼 zxvyio 的回复:] [quote=引用 3 楼 guwei4037 的回复:] this.dataGridView1.DataSource = dt.DefaultView; this.dataGridView1.ClearSelection();//在绑定数据源之后加上这么一句
if (DINdataGridView.CurrentCell == null) { DINdataGridView.Rows[2].Selected = true; MessageBox.Show("没有选中行"); }
if (DINdataGridView.CurrentCell == null) { DINdataGridView.Rows[2].Selected = true; MessageBox.Show("没有选中行2"); }
但是第二条的IF语句,还是运行了,显示“没有选中行2”,那说明第一条IF的Selected = true 没有起作用,怎么会这样呢?[/quote]
不要用DataGridView自带selected 判断,在mousedown时间里面写判断,[/quote]
这是表格加载完自动去除默认选中第一行,不能用mousedown来做!你应该知道表格可以选中多个单元格的吧 Selected = true只是让这个单元格被选中,不代表当前单元格就变成它了
if (DINdataGridView.SelectedCells != null) { DINdataGridView.ClearSelection(); DINdataGridView.CurrentCell = null; MessageBox.Show("没有选中行"); }
if (DINdataGridView.SelectedCells != null) { DINdataGridView.ClearSelection(); DINdataGridView.CurrentCell = null; MessageBox.Show("没有选中行2"); }
经过这种修改后,把这两条代码放到加载数据之后,还是出现第2个MessageBox.Show("没有选中行2"); 而且DataGridView 的第一行还是蓝色的选中状态!