这是我在RowStateChangedEvent中的代码
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if (dataGridView1.Rows[e.Row.Index].Selected)
{
//dosomething
}
}
这是form_load事件(在此填充datagridview)
DataTable dt = new DataTable();
dt.Columns.Add("c1", typeof(string));
dt.Columns.Add("c2", typeof(string));
dt.Rows.Add("somedata", "somedata");
dt.Rows.Add("somedata", "somedata");
dataGridView1.ColumnHeadersVisible = false;
dataGridView1.RowHeadersVisible = false;
dataGridView1.DataSource = dt;
答案 0 :(得分:0)
您将要检查索引> =0。某些事件也会触发具有-1的标题行:
if (e.RowIndex >= 0 && dataGridView1.Rows[e.Row.Index].Selected)
{
//dosomething
}