4

I want to do it with Devexpress extension (gridview) :

string dataInCell = dataGridView1.Rows[i].Cells[j].Value.ToString();

Like :

gridView1.Rows[i].Cells[j]
SidAhmed
  • 2,332
  • 2
  • 25
  • 46
AhmetSem
  • 41
  • 1
  • 1
  • 3

11 Answers11

7

If you are trying to get the value of a cell in a specefic row, here is how :

a. If you want the value of a cell of the focused row :

view.GetFocusedRowCellValue("fieldName");

b. If you want the cell value of a row knowing his handle :

view.GetRowCellValue(rowHandle, "fieldName");

Good luck

SidAhmed
  • 2,332
  • 2
  • 25
  • 46
5

try this

 for (int i = 0; i < gridView.RowCount; i++)
        {
            dataInCell = Convert.ToString(gridView.GetRowCellValue(i, "FieldName"));
        }
Snippet
  • 1,522
  • 9
  • 31
  • 66
1

To get a spescific row you can use these commands.

GridView.GetDataRow(rowHandle)

or

GridView.GetRow(rowHandle)

but if you want to modify a range of cells, it is usually better to go directly at the datasource

Stig
  • 1,323
  • 16
  • 22
1

You presumably have set a datasource on the grid? If so, use the datasource and access it via its datasource index.

Using row handles could causes issues when the grid is sorted. I recommend...

int dataIndex = gridView.GetDataSourceRowIndex(rowHandle);
var myData = myDataSource[dataIndex];

Provided you're using a generic collection there is no casting involved and this handles grouping and sorting. Of course what is displayed and what is the data may not be the same thing. E.g. If the data is an enumeration. You would display the displayname for this but the value in the datasource is the enum. Normally I need the underlying value instead of the displayed text.

Thomas
  • 1,445
  • 14
  • 30
Darren G
  • 11
  • 3
0

you can use below code:

dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name","column2-name",...);

with this you can get value with row index that focused on it and select with field`s name,return value type of object and you cast to int,string and ... such :

string id=(string)dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name");

but it depends to type column1-name

Chakavak Behzad
  • 807
  • 1
  • 9
  • 13
0
string dataInCell = ((DataRowView)gridControl1.MainView.GetRow(i)).Row.ItemArray[j].ToString();
Mehmet
  • 1
0

I think you are looking for this:

string str = gridView1.GetRowCellValue(Convert.ToInt32("ROW_NUMBER"), "COLUMN_NAME").ToString();
demonplus
  • 5,613
  • 12
  • 49
  • 68
0

you should use GetRowCellValue

string cellValue;
cellValue = gridView1.GetRowCellValue(2, "ID").ToString();
Rayan Albouni
  • 169
  • 1
  • 5
0

All above step you can, but keep in mind that null values conversation will be thrown an error, so before access it do Convert.IsDBNull().

0

I think it's the best code for get row column field.

string name= gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "name").ToString(),
Md.Rezwan
  • 51
  • 3
0

You can get the value of grid cell using

string cellValue = gvGrid.GetRowValues(visibleIndex, "FieldName").ToString();

where visibleIndex is the row's index. You can just loop like this

if (gvGrid.VisibleRowCount > 0)
{
   for (int index = 0; index < gvGrid.VisibleRowCount; index++)
   {
      string cellValue = gvGrid.GetRowValues(index, "FieldName").ToString();
   }
}