我的程序中有一个搜索表单。当用户双击搜索表单上的(dgv 的)单元格时,我希望程序关闭该表单并跳转到主表单上的项目。
我通过使用唯一的 ID 来标识每个项目来做到这一点。
我正在尝试将行 id 的值传递给其他表单。问题是,它说我每次都传递零值。但是当我在搜索表单上插入一些消息框时,它表示整数“id”已成功分配给主表单上的变量:public int CellDoubleClickValue { get; set; }
这是我的代码:
搜索表格:
private int id;
private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.rowIndex1 = e.RowIndex;
this.id = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
invmain inv = new invmain();
inv.CellDoubleClickValue = this.id;
this.DialogResult = DialogResult.OK;
this.Close();
//MessageBox.Show(inv.CellDoubleClickValue.ToString());
//Above, it shows it got assigned successfully.
}
主要形式:
public int CellDoubleClickValue { get; set; }
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
search form1 = new search();
form1.ShowDialog();
if (form1.DialogResult == DialogResult.OK)
{
MessageBox.Show(CellDoubleClickValue.ToString());
}//Here it shows that the value is: 0
我建议您按以下步骤操作:
搜索表格:
public int SelectedId { get; set; }
private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.rowIndex1 = e.RowIndex;
this.SelectedId = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
this.DialogResult = DialogResult.OK;
this.Close();
}
主要形式:
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
search form1 = new search();
form1.ShowDialog();
if (form1.DialogResult == DialogResult.OK)
{
int selectedId = form1.SelectedId;
// Do whatever with selectedId...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)