简体   繁体   English

使用datagridview中的数据以不同的形式填充文本框

[英]Populate textbox on different form from data in a datagridview

I'm trying to populate data from a DataGridView into TextBoxes which are on a different form . 我试图将数据从DataGridView填充到具有不同form TextBoxes中。 I can populate TextBoxes that are on the same form as the DataGridView . 我可以填充TextBoxes是相同的形式为上DataGridView I'm not sure on how to reference the other form? 我不确定如何引用其他表格? My code : 我的代码:

var value = dgvInfo.Rows[e.RowIndex].Cells["Description"].Value;

if (value != null)
{
    txtDescription.Text = value.ToString();
}

EDIT 1: The possible duplicate question deals with passing data from one TextBox to another. 编辑1:可能的重复问题处理将数据从一个TextBox传递到另一个。 In my question it is passing data from a DataGridView to a TextBox . 在我的问题是它将数据从DataGridView传递到TextBox

Pass Value as constructor parameter to destination form (for example Form4 ): Value作为构造函数参数传递给目标表单(例如Form4 ):

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    Form4 fr = new Form4(dgvInfo.Rows[e.RowIndex].Cells["Description"].Value.ToString());
    fr.ShowDialog();
}       

And in the destination form: 并以目标形式:

public Form4(string p)
{
    InitializeComponent();
    txtDescription.Text = p;
}

To pass more than one value to another form you should use a List like this: 要将多个值传递给另一种形式,您应该使用类似以下的List

private void button1_Click(object sender, EventArgs e)
{
    List<string> lst = new List<string>();

    foreach (DataGridViewRow row in dgvInfo.SelectedRows)
    {
        var Value = row.Cells["Description"].Value.ToString();
        if (!string.IsNullOrWhiteSpace(Value))
        {
            lst.Add(Value);
        }
    }
    Form4 fr = new Form4(lst);
    fr.ShowDialog();
}

And then in destination form: 然后以目标形式:

public Form4(List<string> p)
{
    InitializeComponent();
    txtDescription.Text = p[0];
    textBox1.Text = p[1];
}

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

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