【问题标题】:I want to get the particular column value of the current row of the datagrid view into text box of another form. How can I do it. I am using c sharp我想将数据网格视图当前行的特定列值获取到另一个表单的文本框中。我该怎么做。我正在使用升c
【发布时间】:2010-03-17 01:59:22
【问题描述】:

我想传递datagridview当前行的列值。我在 datagridview 的一列上有一个按钮。当我单击按钮时,会打开另一个表单。我在另一个表单上有一个文本框。所以我想在另一个表单的文本框中获取当前行的列值。我正在使用 c sharp。

感谢任何帮助。

【问题讨论】:

  • 您是如何打开新表单的?

标签: c# datagridview


【解决方案1】:

下面是一个使用构造函数接收值的例子:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //test data
        this.dataGridView1.Rows.Add(1);
        this.dataGridView1[1, 0].Value = "testValue1";
        this.dataGridView1[1, 1].Value = "testValue2";
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        //Button is in column 0.
        if ((e.ColumnIndex != 0) || (e.RowIndex < 0)) { return; }


        string valueToPass = (dataGridView1[1, e.RowIndex].Value as string) ?? String.Empty;
        Form2 f2 = new Form2(valueToPass);
        f2.Show();
    }

}


public partial class Form2 : Form
{
    public Form2(string valueFromOtherForm)
    {
        InitializeComponent();
        this.textBox1.Text = valueFromOtherForm;
    }

}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2022-11-23
    • 2023-02-03
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多