C# 以另一种形式C将值从listview传递到文本框#
我有两种形式作为主要形式和产品形式。在主窗体中,我有一个包含产品详细信息的列表视图。我想通过单击“更新”按钮将listview中的产品id发送到产品表单。有人能帮我吗?这是在主窗体中绑定listview的代码。我有一个产品形式的文本框1C# 以另一种形式C将值从listview传递到文本框#,c#,sql,forms,listview,C#,Sql,Forms,Listview,我有两种形式作为主要形式和产品形式。在主窗体中,我有一个包含产品详细信息的列表视图。我想通过单击“更新”按钮将listview中的产品id发送到产品表单。有人能帮我吗?这是在主窗体中绑定listview的代码。我有一个产品形式的文本框1 public void BindGridProduct() { try { SqlConnection con = new SqlConnection(@"Data Source=DESKT
public void BindGridProduct()
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-U1OP1S9\SQLEXPRESS;Initial Catalog=PaintStores;Integrated Security=True");
SqlCommand command = con.CreateCommand();
command.CommandText = "sp_getAllProducts";
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
da.Fill(dataTable);
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow drow = dataTable.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(drow["ProductId"].ToString());
lvi.SubItems.Add(drow["ProductName"].ToString());
lvi.SubItems.Add(drow["TypeName"].ToString());
lvi.SubItems.Add(drow["Quantity"].ToString());
lvi.SubItems.Add(drow["Price"].ToString());
lvi.SubItems.Add(drow["Stock"].ToString());
// Add the list items to the ListView
listView2.Items.Add(lvi);
}
}
con.Close();
}
catch(Exception ex)
{
throw ex;
}
}
关于创建构造函数:
public partial class frmProduct : Form
{
public frmProduct()
{
InitializeComponent();
}
public frmProduct(int yourId)
{
InitializeComponent();
}
}
出示您的表格:
int id = 5;
frmProduct frm = new frmProduct(id);
frm.Show(this);
为表单创建一个新的构造函数,该构造函数不包含所需的值。您可以使用此构造函数来显示此表单。表单是一个与其他类类似的类:您可以添加一个接受参数的构造函数,或者使用getter和setter添加属性,或者添加一个用于设置和处理所需值的方法。关于这一点,表单没有什么特别的,只要像对待一个类那样做就行了。只要确保您的产品表单也有一个无参数构造函数(作为VisualStudio为您生成的默认构造函数):VisualStudio designer需要它。请接受答案并关闭线程,我只想让他标记这个问题已经得到了回答:)
int id = 5;
frmProduct frm = new frmProduct(id);
frm.Show(this);