在我的 form1 中,我在 datagridview 中有我的 tblContacts,用户可以单击下一个/上一个按钮来浏览数据库。form1 也有一个更新按钮,该按钮通向 form 2,它是来自 form1 的同一数据集的详细信息视图。有人可以告诉我如何使 form2 自动加载 form1 datagridview 已选择的特定联系人的联系方式。
谢谢。
在我的 form1 中,我在 datagridview 中有我的 tblContacts,用户可以单击下一个/上一个按钮来浏览数据库。form1 也有一个更新按钮,该按钮通向 form 2,它是来自 form1 的同一数据集的详细信息视图。有人可以告诉我如何使 form2 自动加载 form1 datagridview 已选择的特定联系人的联系方式。
谢谢。
首先:例如,在您的 form2 类中,创建包含来自数据网格的信息的构造函数;
public string variable1 {get;set;}
public int variable2 {get;set;}
// as long as they're public, it doesnt matter what you call them, or there type
//(just make sure that you're using the right type, for datagrid's, string is
//usually fine but you can always convert)
其次:当您创建表单的新实例时,您可以将变量分配给“传递”到您的新表单,就像这样
Form2 form2 = new Form2();
form2.variable1 = dataSource[rowIndex]["columnName"]
form2.variable2 = dataSource[rowIndex2]["columnName2"]
要获取选定的行索引,请执行;
dataTable.SelectedRows[0].Index;
而不是 [rowIndex]
从这里开始,在您的 form2 中,您可以使用这些值对它们做任何您想做的事情