473,725 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5583

"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, AlternatingBack Color, SelectionBackCo lor 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, AlternatingBack Color, SelectionBackCo lor 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= b77a5c561934e08 9");

PropertyInfo piRows = gridType.GetPro perty("DataGrid Rows",
BindingFlags.In stance | BindingFlags.No nPublic);

Type rowType = piRows.Property Type.GetElement Type();
PropertyInfo piValue = rowType.GetProp erty("RowNumber ");

object[] rows = piRows.GetValue (myDataGrid, null) as object[];
foreach (object row in rows)
{
int rowNumber = (int)piValue.Ge tValue(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.BindingConte xt[myDataGrid.Data Source].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.DataSo urce contains you can use
properties and methods on each of the six types to parse the content.
if (dg.DataSource is DataTable)
ParseTable(dg.D ataSource as DataTable);
else if (dg.DataSource is IList)
ParseList(dg.Da taSource 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= b77a5c561934e08 9");

PropertyInfo piRows = gridType.GetPro perty("DataGrid Rows",
BindingFlags.In stance | BindingFlags.No nPublic);

Type rowType = piRows.Property Type.GetElement Type();
PropertyInfo piValue = rowType.GetProp erty("RowNumber ");

object[] rows = piRows.GetValue (myDataGrid, null) as object[];
foreach (object row in rows)
{
int rowNumber = (int)piValue.Ge tValue(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.BindingConte xt[myDataGrid.Data Source].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.DataSo urce contains you can use
properties and methods on each of the six types to parse the content.
if (dg.DataSource is DataTable)
ParseTable(dg.D ataSource as DataTable);
else if (dg.DataSource is IList)
ParseList(dg.Da taSource 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
8508
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 following snippet updates some values in the selected rows:
1
1528
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
7102
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
1247
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 boxes. I thought about creating another custom type class for DataGridView and possibly use a composite class to bring in all of the CustomDataGrid's functionality. I was hoping to override the DataGridView's DataSource and assign to the...
2
1043
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 changes never reflect in the database. Does anybody have a sample/example of how to use the datagridview with code not the wizard? thanks
1
3803
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 button. Any ideas on how I can accomplish this? Thanks, Sean
2
1774
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 said the style name had to be the same as the table name. Perhaps the are refering to ts1.mappingname must be the same as the table name (datagrid1.datamember) because I've read that that is how the datagrid associates a style with what it's...
2
305
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. DataGridView1.SetDataBindings and DataGridView1.TableStyles
3
1833
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 composed of Text, Combo, etc boxes that contain the individual columns of the DataGrid row. To extract information from the user selected DataGrid row we use the
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.