Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 463
kashif Avatar asked Oct 06 '12 18:10

kashif


People also ask

How do I get the selected row cell value in DevExpress GridView?

Answers approved by DevExpress Support MultiSelect option, use the GridView. GetSelectedRows method to get handles of the selected rows. To get a value of these rows, use the GridView. GetRowCellValue method.

How do I get row count in DevExpress GridControl?

To get the row number, you can use the GridView. RowCount property. To sort the grid against a column, you can use end-user capabilities (click or right-click a column header).

How do I get summary value from DevExpress GridView?

To obtain a total summary value, use the GridColumn. SummaryItem. SummaryValue property as described in the Obtain Summary Values help article. To access a cell value, use the GridView.

How do I select multiple rows in DevExpress GridView?

Answers approved by DevExpress Support AllowSelectByRowClick to True. By default, the grid supports multiple selection, so you can select several rows holding down the 'Ctrl' key.


2 Answers

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.

like image 59
mili Avatar answered Oct 14 '22 06:10

mili


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();
like image 38
Stig Avatar answered Oct 14 '22 07:10

Stig