DatagridView数据传输

时间:2015-08-11 10:40:39

标签: c# winforms datagridview

我试图将DGV中的数据从一种形式传输到另一种形式的Texbox 这就是我所做的:

int o = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows[o].Cells["AccountName"].Value.ToString()
obj.txtTranType.Text = dataGridView1.Rows[o].Cells["TransactionType"].Value.ToString();
obj.txtDateTime.Text = dataGridView1.Rows[o].Cells["TransactionDate"].Value.ToString();
obj.txtTranAmt.Text = dataGridView1.Rows[o].Cells["TransactionAmount"].Value.ToString();
obj.txtCurrAmt.Text = dataGridView1.Rows[o].Cells["CurrentAmount"].Value.ToString();
obj.txtAvailAmt.Text = dataGridView1.Rows[o].Cells["AvailableAmount"].Value.ToString();
obj.txtClientID.Text = dataGridView1.Rows[o].Cells["ClientID"].Value.ToString();

当我回到带有文本框的表单时,没有任何内容显示在其中,我不知道为什么。

2 个答案:

答案 0 :(得分:0)

Not sure if this is async or on button click, or whatever.

But for async add a method to the event of the datagrid, that sets the textbox of the form to the value of the datagrow row by referencing the index value.

Missing quite a bit of the requirements, so it's hard to provide you with complete code examples.

答案 1 :(得分:0)

如果您对评论中提到的obj有疑问,请尝试使用此示例将对象传递给表单obj

public partial class Form1 : Form
{
    private Form2 obj;
    public Form1(Form2 form2)
    {
        InitializeComponent();
        obj = form2;
    }
    //declare your method here as you show in code to get values from gridview using obj
}


public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        LaunchForm1();
    }
    private void LaunchForm1()
    {
        var form1 = new Form1(this);
        form1.ShowDialog();
    }
}