get row of DataGrid by index

Oct 8, 2009 at 3:27 PM


I have a problem concerning the DataGrid.


This is the Code of what I want to do - but in an ordinary SourceGrid.Grid:



SourceGrid.Cells.Views.Cell yellowView = new SourceGrid.Cells.Views.Cell();
yellowView.BackColor = Color.Yellow;

int[] selectedRowsIndexArray = grid1.Selection.GetSelectionRegion().GetRowsIndex();
for (int i = 0; i < selectedRowsIndexArray.Length; i++)
    grid1[selectedRowsIndexArray[i], 0].View = yellowView;

Works fine.

But now I want do exact the same thing in my DataGrid, but the problem is, that it is not possible to address the Cells directly like



Is there any method I can reach the rows / cells of my DataGrid with their index and change the View of the Cells in DataGrid?


My other question is not as critical for me as the one above: Is it possible to change the background color of a whole row without touching every Cell.View Attribute of the Cells in this row? Like in Microsofts DataGridView it is myRow.DefaultCellStyle.SelectionBackColor = myColor;


Thank you.



Oct 13, 2009 at 1:16 PM

In datagrid, a single view is shared for all cells.  Look at examples which use datagrid, there you can find how to change the background.

Jan 20, 2011 at 2:57 AM

Hi I want to bring back this question. specifically the unanswered part.


grid1[0,1].View = yellowView;

How Can this be done using a DataGrid cause i have been up and down all the examples and tried everything i can to get
a similar thing to work. An example pasted here would be greatly appreciated.

P.S I thought even the DataGrid1.Rows[0]. would work but was surprised that it didn't. Please help.

Jan 20, 2011 at 5:40 AM

grid1[0,1].View = yellowView;

What is exactly the problem?  Is the problem that you do not know how to get a row/ column index of selected cell, or you do not know how to attach a view, or... ?


tell us what you want first in what effect you want to actually achieve

Jan 20, 2011 at 5:44 PM

The problem is that it is clear that for Grid to change properties of a specific row you use... grid1[0,1].Veiw etc... but I am trying to do the same thing in DataGrid.  I managed to find out how to get and manipulate the columns through ..  dg.Columns[c].HeaderCell.View = viewColumnHeader;   dg being my DataGrid.  but I dan't figure out how to access a specific row as per the above example.

Basically I have created a DataGrid bound to a datatable and I have added the rows to it.  All works wonderful... except there are not row headers which I would like to add after the fact. So would like to loop through the Rows and add the rowheaders to the Datagrid.


Any Help would be be appreciated.


Also I would like to be able to say Row[x].Datacell.text = "newText";  (Sorry Not at home so this is pseudo code only Hope it shows what i am trying to do though.)



Jan 28, 2011 at 8:00 AM

Did you go through examples and examined how they are programmed?  There are a couple of examples with DataGrid, and they have row headers, and some other stuff

Jan 29, 2011 at 6:44 PM

I have been through the examples actually and I did double check just in case, and while i was able to get the row headers added (dg.Columns.Insert(0, SourceGrid.DataGridColumn.CreateRowHeader(dg));)


there not click-able mind you and while this was a side goal the main goal is to still be able to reference a row directly... just like above.. dg.Rows doesn't seem to have the ability to return that row so you can manipulate it directly.


Any thoughts?

Jan 29, 2011 at 6:48 PM

the code is relatively still unfamiliar to me but it looks like the columns inherits in such a way that it provides access to the list of columns for the grid, but it looks like the rows doesn't inherit in a similar method so the row list is not exposed.

Jan 30, 2011 at 12:41 PM

>but it looks like the rows doesn't inherit in a similar method so the row list is not exposed.

That's because of inherent difference between how DataGrid and Grid works. DataGrid does not have a list of SourceGridRow objects. DataGrid is designed for speed and scalability, which means that it's not a viable solution to create 1 million DataGridRows objects.  


DataGrid exposes a method to get an index to your original data source from the currently selected row in the grid.

See these methods: