简体   繁体   English

将值从列表视图传递到另一种形式的C#文本框

[英]Passing a value from listview to a textbox in another form C#

I have two forms as Main form and Product form. 我有两种形式,分别是主形式和产品形式。 In the Main form I have a listview which contains the details of product. 在主窗体中,我有一个包含产品详细信息的列表视图。 I want to sent the product id in the listview to the Product form through the 'Update' button click. 我想通过单击“更新”按钮将列表视图中的产品ID发送到“产品”表单。 Can any one help me? 谁能帮我? This is the code to bind the listview in the main form. 这是绑定主视图中的列表视图的代码。 And i have a textbox1 in Product form. 而且我在产品表单中有一个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;
        }
    }

And this is the button in the Main form, to pass the value to other form 这是Main表单中的按钮,用于将值传递给其他表单

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

    }

Regards 问候

Create a constructor: 创建一个构造函数:

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

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

Show your form: 显示表格:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM