C# – How to get the selected row values of DevExpress XtraGrid

cdevexpresswinformsxtragrid

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??

Best Answer

Here is the way that I've followed,

int[] selRows = ((GridView)gridControl1.MainView).GetSelectedRows();
DataRowView selRow = (DataRowView)(((GridView)gridControl1.MainView).GetRow(selRows[0]));
txtName.Text = selRow["name"].ToString();

Also you can iterate through selected rows using the selRows array. Here the code describes how to get data only from first selected row. You can insert these code lines to click event of the grid.