473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Iterate through a DataGrid (not a DataGridView)

Hi

I need to iterate through a winforms visible datagrid, in a legacy
application, to update it a little bit to change the background
colours of the grid.

This seems to be more dificult than i expected as it does not appear
the DataGridRows[] collection is exposed with DataGrid.

Is there a way to/ or best way to iterate through the rows of a
datagrid to change the background colour.

Note that i cannot update the winforms app to use a DataGridView
Any advice or examples appreciated

Peted
Oct 20 '08 #1
4 5570

"Peted" wrote:
Hi

I need to iterate through a winforms visible datagrid, in a legacy
application, to update it a little bit to change the background
colours of the grid.

This seems to be more dificult than i expected as it does not appear
the DataGridRows[] collection is exposed with DataGrid.

Is there a way to/ or best way to iterate through the rows of a
datagrid to change the background colour.

Note that i cannot update the winforms app to use a DataGridView
Any advice or examples appreciated

Peted
Hi Peter,

I'm afraid you can't change the background color if an individual row, even
if you can extract the DataGridRows collection using reflection. The
DataGridRow does not contain any color properties. If you want a different
background color on the DataGridRows you need to use the color properties on
the DataGrid. BackColor, AlternatingBackColor, SelectionBackColor etc.

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 20 '08 #2


Hi thanks for that.
Suppose however i do want to just iterate through a DataGrid , lets
say through the rows using a "foreach" statement. Is this possible
with a datagrid, seeing as it does not expose a datagridrows
collection.

Is there anyway to do this at all ?

thanks

Peter
>
Hi Peter,

I'm afraid you can't change the background color if an individual row, even
if you can extract the DataGridRows collection using reflection. The
DataGridRow does not contain any color properties. If you want a different
background color on the DataGridRows you need to use the color properties on
the DataGrid. BackColor, AlternatingBackColor, SelectionBackColor etc.
Oct 21 '08 #3

"Peted" wrote:
>

Hi thanks for that.
Suppose however i do want to just iterate through a DataGrid , lets
say through the rows using a "foreach" statement. Is this possible
with a datagrid, seeing as it does not expose a datagridrows
collection.

Is there anyway to do this at all ?

thanks

Peter
Well, using reflection you can get to the DataGridRow using the following:
Type gridType = Type.GetType(@"
System.Windows.Forms.DataGrid,
System.Windows.Forms,
Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089");

PropertyInfo piRows = gridType.GetProperty("DataGridRows",
BindingFlags.Instance | BindingFlags.NonPublic);

Type rowType = piRows.PropertyType.GetElementType();
PropertyInfo piValue = rowType.GetProperty("RowNumber");

object[] rows = piRows.GetValue(myDataGrid, null) as object[];
foreach (object row in rows)
{
int rowNumber = (int)piValue.GetValue(row, null);
}

However, there are no Cells property or Value so the DataGridRow is only
meant for display and only contains the row index to the actual row, which
would explain why DataGridRows is null before display time, even though
DataSource is set.

You can instead use the indexer on the DataGrid itself to obtain the values

int numRows = dg.BindingContext[myDataGrid.DataSource].Count;
object[] data = new object[numRows];
for (int i = 0; i < numRows; i++)
{
data[i] = myDataGrid[i, 0];
}

However, the number of columns is a bit trickier as this is the number of
displayed columns, not the number of potential columns from the DataSource.

A third method is parsing through the DataSource itself, which according to
the documentations can be a DataTable, DataView, DataSet, DataViewManager,
IListSource or IList.

Once you determine which type the DataGrid.DataSource contains you can use
properties and methods on each of the six types to parse the content.
if (dg.DataSource is DataTable)
ParseTable(dg.DataSource as DataTable);
else if (dg.DataSource is IList)
ParseList(dg.DataSource as IList);
....
etc
--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 21 '08 #4

thanks for that dude
i will see how it goes

cheers

Peter
>>

Well, using reflection you can get to the DataGridRow using the following:
Type gridType = Type.GetType(@"
System.Windows.Forms.DataGrid,
System.Windows.Forms,
Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089");

PropertyInfo piRows = gridType.GetProperty("DataGridRows",
BindingFlags.Instance | BindingFlags.NonPublic);

Type rowType = piRows.PropertyType.GetElementType();
PropertyInfo piValue = rowType.GetProperty("RowNumber");

object[] rows = piRows.GetValue(myDataGrid, null) as object[];
foreach (object row in rows)
{
int rowNumber = (int)piValue.GetValue(row, null);
}

However, there are no Cells property or Value so the DataGridRow is only
meant for display and only contains the row index to the actual row, which
would explain why DataGridRows is null before display time, even though
DataSource is set.

You can instead use the indexer on the DataGrid itself to obtain the values

int numRows = dg.BindingContext[myDataGrid.DataSource].Count;
object[] data = new object[numRows];
for (int i = 0; i < numRows; i++)
{
data[i] = myDataGrid[i, 0];
}

However, the number of columns is a bit trickier as this is the number of
displayed columns, not the number of potential columns from the DataSource.

A third method is parsing through the DataSource itself, which according to
the documentations can be a DataTable, DataView, DataSet, DataViewManager,
IListSource or IList.

Once you determine which type the DataGrid.DataSource contains you can use
properties and methods on each of the six types to parse the content.
if (dg.DataSource is DataTable)
ParseTable(dg.DataSource as DataTable);
else if (dg.DataSource is IList)
ParseList(dg.DataSource as IList);
...
etc
Oct 22 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JochenZ | last post by:
Hello, I have a DataGrid(View) and a DataTable. The DataTable is displayed in the DataGridView: dataGridView.DataSource = theTable; The user is allowed to select some rows and then the...
1
by: Irfan | last post by:
hi, all In VB 2005 Beta i cant find dataGrid control in the toolbox, instead DataGridView is available. What is the differance between the two ? How can i get dataGrid Control? Thanks
5
by: ComputerStop | last post by:
I am attempting to print a datagridview. I have not found any method that works successfully. Has any one been successful with this?
0
by: Brett Romero | last post by:
I've written a DataGrid class that inhertis from DataGrid. There's a lot of code in this class specific to the DataGrid's functionality. I mainly want to use the DataGridView for drop down combo...
2
by: Warex | last post by:
Hello I am having a problem with using the datagrid. I am attaching my data through a dataset via code to a datagridview.datasource. All if fine except if I make any changes to the datagrid the...
1
by: seanmle | last post by:
I want to build a windows application that has a datagrid filled with data. When a person modifies information on a single cell, it updates the database without the user having to click on a save...
2
by: cj | last post by:
I was looking over some of my 2003 code today (see below) that loads a foxpro table via oledb connection. I used a sub "autosizecolumns" I found on the web but I never quite understood why they...
2
by: Rob | last post by:
After I type my DataGrid name in the code, I am expecting to have a couple options (methods ?) available... I see them in sample code, but they are not available for me to select. ...
3
by: frostbb | last post by:
Greetings, We're starting to transition our legacy C# apps from earlier versions to VS2008 versions. Many of our apps allow the user to select a row from a DataGrid into a 'Data Edit' area...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.