Search code examples
c#winformsdevexpressxtragrid

How to get the selected row values of DevExpress XtraGrid?


Consider the following picture

enter image description here

I get the selected row values in the three textboxes shown in the figure when i click a cell using following code.

void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e) {
    TBGRNo.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
    TBSName.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
    TBFName.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
}

My Question is: how will I do the same thing in DevExpress XtraGrid control??


Solution

  • You can do this in a number of ways. You can use databinding (typical initialized after InitializeComponent();)

    textBox1.DataBindings.Add(new Binding("Text", yourBindingSource, 
                        "TableName.ColumnName", true, DataSourceUpdateMode.OnPropertyChanged));
    

    or use a DataLayoutControl (if you are going to use textbox for editing, I really recommend spending some time to learn how to use this component.

    or in FocusedRowChanged by assigning from one of these methods:

    textBox1.Text = gridView1.GetDataRow(e.FocusedRowHandle)["Name"].ToString();
    textBox1.Text = gridView1.GetFocusedDataRow()["Name"].ToString();
    textBox1.Text = (gridView1.GetFocusedRow() as DataRowView).Row["Name"].ToString();
    textBox1.Text = gridView1.GetFocusedRowCellValue("Name").ToString();