将listview中的值传递给另一种形式的文本框C#

时间:2017-06-05 10:18:15

标签: c# sql forms listview

我有两种形式,主要形式和产品形式。在Main表单中,我有一个listview,其中包含产品的详细信息。我想通过“更新”按钮点击将列表视图中的产品ID发送到产品表单。谁能帮我?这是在主窗体中绑定listview的代码。我有一个产品形式的textbox1。

        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;
        }
    }

这是Main表单中的按钮,将值传递给其他表单

        private void button22_Click_1(object sender, EventArgs e)
    {
       //code

    }

此致

1 个答案:

答案 0 :(得分:3)

创建一个构造函数:

public partial class frmProduct : Form
    {
        public frmProduct()
        {
            InitializeComponent();
        }

        public frmProduct(int yourId)
        {
            InitializeComponent();
        }
    }

显示表单:

int id = 5;
frmProduct frm = new frmProduct(id);
frm.Show(this);