How to get data in a selected row of your Xtragrid

There are many alternative ways to get values of cells in a row in your devexpress xtragrid. This tutorial will describe three simple ways to achieve this task by using the following methods:
  • GetRowCellValue()
  • GetDataRow() 
  • GetRow()
In the example, 
  • gv is your gridview's name 
  • colName is your gridview’s column name 

1. GetRowCellValue method
The GridView class has the GetRowCellValue(), which returns a cell value by a given GridView row handle
and the name of the column.

// is there any row selected?

if( gv.SelectedRowsCount > 0) {
  MessageBox.Show(gv.GetRowCellValue(gv.FocusedRowHandle, colName).ToString());
}

2. GetDataRow method
If you do not use LinQ to populate your xtragrid’s datasource, use can use GetDataRow method.

// is there any row selected?

if( gv.SelectedRowsCount > 0) {
  System.Data.DataRow row = gv.GetDataRow(gv.FocusedRowHandle);
  MessageBox.Show(row[number].ToString());
}

The row variable in the above code is always null if you use LinQ binding for your xtragrid's datasource. In case of LinQ binding, use the GetRow() method as the following.

3. GetRow method
If you use LinQ to populate your xtragrid’s datasource, you can use GetRow method.

// is there any row selected?

if( gvQuanLyMayChamCong.SelectedRowsCount > 0) {
  LinQEntityName row =(LinQEntityName)gv.GetRow(gv.FocusedRowHandle);
  MessageBox.Show(row.Field_Name);
}

No comments:

Post a Comment

Blog Archive

About Me

My photo
I am a software developer with roughly 5 years of experience in developing end-to-end solutions in C#, Java, C/C++, PHP and HTML/CSS/Javascript. At the moment, I am joining the Professional Doctorate in Engineering degree program in Software Technology at Eindhoven University of Technology. My areas of particular interest include software design, data structures and algorithms, problem solving, software security, embedded system, machine learning, and data science.