private void 删除纪录ToolStripMenuItem_Click(object sender, EventArgs e)
{
string id = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
ClsCon con = new ClsCon();
con.CondDatabase();
SqlCommand cmd = new SqlCommand("delete from g_grade where gradeid ='" + id +"'", con.conn);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
con.conn.Close();
Archeievement_list(this.dataGridView1.CurrentRow.Cells[2].Value.ToString()); }
如上图.右键选择的时候点中的是第9行,可是蓝色图标显示的是第1行,也就是我上面的程序中的CurrentRow是第1行,请问,如何使右键选择的时候,同时交CurrentRow也变为第9行?
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int x = this.dataGridView1.HitTest(e.X,e.Y).RowIndex;
int y = this.dataGridView1.HitTest(e.X,e.Y).ColumnIndex;
if (x != -1 && y != -1)
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[x].Cells[y]; }
}点右键的时候选择右键行
试了不行,问题不在这里,肯定我是上面的
string id = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();这里出问题了,因为获得是蓝色选中的地方.
{
dataGridView1.ClearSelection();
Rectangle _Rect =dataGridView1.RectangleToClient(new Rectangle(Cursor.Position.X, Cursor.Position.Y, 0, 0)); int _RowIndex = dataGridView1.HitTest(_Rect.X, _Rect.Y).RowIndex;
if (_RowIndex != -1) dataGridView1.CurrentCell = dataGridView1.Rows[_RowIndex].Cells[0];
}给菜单加个事件... OPENING
1.添加右键菜单
2.添加右键菜单的opening时间(即打开右键菜单时所要发生的事);
3.在opening事件中添加关键代码(string id = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
),必须要在此时间中获取id值,否则你永远是id=0;