从TextBox中的dataGridView显示数据?

时间:2013-03-14 10:22:05

标签: c#

当我点击dataGridView上的行时,我想在文本框中填充该行的数据? 我怎么能这样做?

exemple  ID, Name,Surname ... to show now in TextBox

dataGridView中的数据(例如ID=1, Name=s ...)显示在Textbox Up ??

3 个答案:

答案 0 :(得分:8)

您必须实施SelectionChanged的{​​{1}}事件,然后检查选择的行。

DataGridView

答案 1 :(得分:3)

注册网格的MouseClick事件并使用以下代码。

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    DataGridViewRow dr = dataGridView1.SelectedRows[0];
    textBox1.Text = dr.Cells[0].Value.ToString();
     // or simply use column name instead of index
    //dr.Cells["id"].Value.ToString();
    textBox2.Text = dr.Cells[1].Value.ToString();
    textBox3.Text = dr.Cells[2].Value.ToString();
    textBox4.Text = dr.Cells[3].Value.ToString();
}

在加载事件中添加以下行

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

答案 2 :(得分:0)

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //it checks if the row index of the cell is greater than or equal to zero
    if (e.RowIndex >= 0)
    {
        //gets a collection that contains all the rows
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        //populate the textbox from specific value of the coordinates of column and row.
        txtid.Text = row.Cells[0].Value.ToString();
        txtname.Text = row.Cells[1].Value.ToString();
        txtsurname.Text = row.Cells[2].Value.ToString();
        txtcity.Text = row.Cells[3].Value.ToString();
        txtmobile.Text = row.Cells[4].Value.ToString();

    }

}