C# RowIndex在ContextMenuStrip中返回错误
我一直在想办法。使用contextmenustrip编辑、更新和删除我的数据 基本上,我希望我的contextmenustrip能够与Datagridview交互,问题是每当我为click事件设置代码时。它显示了一个错误,我对c#还是新手,所以我可能没有弄明白什么。这是我的密码C# RowIndex在ContextMenuStrip中返回错误,c#,datagridview,contextmenustrip,C#,Datagridview,Contextmenustrip,我一直在想办法。使用contextmenustrip编辑、更新和删除我的数据 基本上,我希望我的contextmenustrip能够与Datagridview交互,问题是每当我为click事件设置代码时。它显示了一个错误,我对c#还是新手,所以我可能没有弄明白什么。这是我的密码 private void DgAddnew_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { MessageBox.Show("You c
private void DgAddnew_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show("You can now Edit this data, Choose Update Button To populate, Thank you!", "Edit Data,", MessageBoxButtons.OK, MessageBoxIcon.Information);
int index = e.RowIndex;// get the Row Index
DataGridViewRow selectedRow = dgAddnew.Rows[index];
txtNAME.Text = selectedRow.Cells[1].Value.ToString();
txtADDRESS.Text = selectedRow.Cells[2].Value.ToString();
txtCELLPHONE.Text = selectedRow.Cells[3].Value.ToString();
txtCODE.Text = selectedRow.Cells[4].Value.ToString();
cmbTYPE.Text = selectedRow.Cells[5].Value.ToString();
cmbCOLOR.Text = selectedRow.Cells[6].Value.ToString();
cmbHEELS.Text = selectedRow.Cells[7].Value.ToString();
cmbPAYMENT.Text = selectedRow.Cells[8].Value.ToString();
cmbPRICE.Text = selectedRow.Cells[9].Value.ToString();
cmbRESERVE.Text = selectedRow.Cells[10].Value.ToString();
cmbBONDFEE.Text = selectedRow.Cells[11].Value.ToString();
dpDATEOUT.Text = selectedRow.Cells[12].Value.ToString();
dpDATERETURN.Text = selectedRow.Cells[13].Value.ToString();
}
这项工作在datagridview中进行。但当我在contextmenu中添加它时,所有这些项都不会填充,并且我得到一个行索引错误
private void EDITToolStripMenuItem_Click_1(object sender, EventArgs e)
{
MessageBox.Show("You can now Edit this data, Choose Update Button To populate, Thank you!", "Edit Data,", MessageBoxButtons.OK, MessageBoxIcon.Information);
int index = e.RowIndex;// get the Row Index
DataGridViewRow selectedRow = dgAddnew.Rows[index];
txtNAME.Text = selectedRow.Cells[1].Value.ToString();
txtADDRESS.Text = selectedRow.Cells[2].Value.ToString();
txtCELLPHONE.Text = selectedRow.Cells[3].Value.ToString();
txtCODE.Text = selectedRow.Cells[4].Value.ToString();
cmbTYPE.Text = selectedRow.Cells[5].Value.ToString();
cmbCOLOR.Text = selectedRow.Cells[6].Value.ToString();
cmbHEELS.Text = selectedRow.Cells[7].Value.ToString();
cmbPAYMENT.Text = selectedRow.Cells[8].Value.ToString();
cmbPRICE.Text = selectedRow.Cells[9].Value.ToString();
cmbRESERVE.Text = selectedRow.Cells[10].Value.ToString();
cmbBONDFEE.Text = selectedRow.Cells[11].Value.ToString();
dpDATEOUT.Text = selectedRow.Cells[12].Value.ToString();
dpDATERETURN.Text = selectedRow.Cells[13].Value.ToString();
}
它的“e.RowIndex”部分给出了错误。是否有一种方法可以声明全局声明或将行索引存储到容器,并能够与contextmenustrip交互
我已经想了好几天了,但还是不起作用。
这是我学习c#作为第一语言的12天,我仍然很糟糕,这让我很沮丧。
请帮帮我。谢谢。
EventArgs
没有名为RowIndex
的属性。相反,我只是使用var selectedRow=dgAddnew.SelectedRows[0]
获取所选行。此代码假定您在选择多行时没有尝试执行操作。为了安全起见,我会先检查SelectedRows.Count>0
。谢谢你给我的建议,很抱歉,我的编程逻辑还不是很好,因为我还是刚开始,不知道如何处理它。你给了我很多关于这条评论的想法,我将尝试分析它,以便能够处理我想要的功能。非常感谢你!