[英]Row Index error in my delete button gridview
我显示的这段代码在单元格单击事件中完全正常。 但是我在 gridview 中插入了一些按钮,其中之一是“删除”按钮。 当我将代码从单元格单击事件传输到按钮删除时,我遇到了这个错误。 有人可以告诉我我需要在代码中更改什么才能修复删除功能吗?
private void button3_Click(object sender, EventArgs e)
{
string maincon = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(maincon);
string rfidno = dataGridView1.Rows[e.RowIndex].Cells["rfidno"].FormattedValue.ToString();
DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this information?", "Delete function triggered", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
try
{
SqlDataAdapter da = new SqlDataAdapter();
sqlcon.Open();
da.DeleteCommand = new SqlCommand("delete from tbl_registerStudent where rfidno = '" + rfidno + "'", sqlcon);
da.DeleteCommand.ExecuteNonQuery();
MessageBox.Show("" + rfidno);
MessageBox.Show("Delete Successfull");
sqlcon.Close();
bindGrid();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
else if (dialogResult == DialogResult.No)
{
CreateID objs = new CreateID();
if (objs == null)
{
objs.Parent = this;
}
objs.Show();
this.Hide();
}
}
您不能在 buttonclick 事件处理程序中使用 e.RowIndex,因为传入的e
没有该属性。
你应该像以前一样处理这个问题; 将处理程序附加到 datagridview 的 CellContentClick 并执行此代码,如果单击的列是按钮列,即不要这样做:
当我从单元格单击事件传输代码时
只需进行单元格单击事件测试“如果单击的列是按钮列,则......”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.