【问题标题】:display selected datagridview item to textbox on another form c#将选定的datagridview项目显示到另一个表单c#上的文本框
【发布时间】:2015-05-20 10:26:34
【问题描述】:

我正在设计一个要求用户从位于另一个表单的列表中选择产品的 pos 系统,该列表是在 datagridview 中生成的。我现在需要的是,当我单击 datagridview 列表上的项目时,它应该显示在 pos 表单的文本框中。我有下面的代码,但它没有将值传递给表单

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    POS pos = new POS();
    pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
    pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
    this.Hide();
}

提前致谢。

【问题讨论】:

  • 你没有在你发布的代码中传递任何东西
  • 那我该如何通过呢?
  • 您正在此处创建一个新的“POS”表单实例。那是你要的吗?每次用户点击时显示一个新的表单实例?

标签: c# winforms


【解决方案1】:

如果 POS 是您的表单,您应该出示它。

改变'this.Hide();'到'POS.Show()'

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    POS pos = new POS();
    pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
    pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
    pos.Show();
}

应该可以了!

编辑:

此外,如果您的 POS 表单已经可见并且带有 dataGridView 的表单已从该表单打开,那么您应该使用对所有者表单的引用。

POS 形式:

private void buttonOpenProductList_Click(object sender, EventArgs e)
{
    var productListForm = new ProductListForm(); // It is form with DataGridView
    productListForm.Show(this); // Set owner form
}

ProductList 表单(您的表单带有 DataGridView):

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    POS pos = (POS)this.Owner;
    pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
    pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
    this.Close();
}

ctor 示例:

POS 形式:

private void buttonOpenProductList_Click(object sender, EventArgs e)
{
    var productListForm = new ProductListForm(this); // It is form with DataGridView
    productListForm.Show();
}

ProductList 表单(您的表单带有 DataGridView):

private POS pos;
// Constructor of ProductListForm
public ProductListForm(POS pos)
{
    this.pos = pos;
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
    pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
    this.Close();
}

【讨论】:

  • 我试过这个它抛出一个异常(在商店管理 System.exe 中发生了“System.InvalidCastException”类型的未处理异常)
  • @UmarE.Shaibu 显示您的堆栈跟踪和完整的异常消息。什么是问题线?这个“POS pos = (POS)this.Owner;”还是其他?
  • POS pos = (POS)this.Owner;这就是抛出异常的地方
  • 检查您使用 DataGridView 打开子窗体的位置。您应该将所有者作为“显示”方法的参数,这是必然的!看我的代码:productListForm.Show(this); // 设置所有者表单
  • 我看到代码'private void btnSlect_Click(object sender, EventArgs e) { var frmProductList = new frmProductList(); // 它是带有 DataGridView 的表单 frmProductList.Show(this); // 设置所有者表单 }'
【解决方案2】:

您必须创建要发布到的表单的实例,例如:

Form f2 = new Form();

f2.TextBox1.Text = somevalue;

现在您也可以访问您要发布的表单上的控件,例如 TextBox 等。您可以为它们分配值。

请注意本指南。

【讨论】:

    【解决方案3】:
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        POS pos = new POS();
        pos.txtProductCode.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        pos.txtProductName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        pos.Show();
    }
    

    使文本框公开

    其他明智的添加一个方法来填充POS中的数据。

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        POS POS = null;
        FormCollection fc = Application.OpenForms;
        foreach (Form frm in fc)
        {
            if (frm.GetType().Name.Equals("POS"))
            {
                POS = frm as POS;
            }
        }
        if (POS == null)
        {
            POS = new POS();
        }
        string ProductCode = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        string ProductName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
    
        POS.FillTextBoxes(ProductCode, ProductName);
        POS.Show();
    }
    

    在 POS 中:

    internal  void FillTextBoxes(string productCode, string productName)
    {
         txtProductCode.Text = productCode;
         txtProductName.Text = productName;
    }
    

    CellContentClick 在单击文本时起作用。最好将其更改为 dataGridView1_CellClick 或 CellDoubleClick。

    POS.Show() 还会在每次点击时创建一个新表单。因此,如果适合您的情况,请应用 POS.ShowDialog() 。

    【讨论】:

    • 我试过了,但它没有将值添加到当前打开的表单中,而是用数据打开了一个新表单。我希望它将数据应用于当前打开的 POS 表单。谢谢。
    • 你尝试使用 POS.ShowDialog() 吗?
    • 是的,我做到了。它打开了一个新的 POS 表格.. 里面有值.. 我希望它把值放在已经打开的 POS 表格中.. 谢谢
    • 我尝试将您发送的示例转换为用于 datagridview 仍然没有工作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多