473,511 Members | 16,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading rows and columns from a DataGrid

I find it amazing that I am quickly able to find answers to obscure questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in each
column.

I can't figure out how to do that. It has to be simple, can somebody please
help me?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the audience.

Animadverto est verus
Nov 17 '05 #1
15 9168
Hi,

That;s not a very good idea to do, you see the datagrid is meant only to
make the data visible, not to retrieve it. for that use the binded source.

The reason is that the "data" itself may be transformed, maybe you have a
column that in the datasource was a double or a decimal and in the grid is
displayed as a currency formatted using the locate in place ( $XX,XXX.YY or
$XX.XXX,YY ) convert that back to the original value can be difficult

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Rik Brooks" <rb*****@hhcc.com.donotspam> wrote in message
news:B0**********************************@microsof t.com...
I find it amazing that I am quickly able to find answers to obscure
questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in each
column.

I can't figure out how to do that. It has to be simple, can somebody
please
help me?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the audience.

Animadverto est verus

Nov 17 '05 #2
>I find it amazing that I am quickly able to find answers to obscure
questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in each
column.

I can't figure out how to do that. It has to be simple, can somebody
please
help me?


I think you'd do it this way:

for (int rowIndex = 0; rowIndex < dataGrid.VisibleRowCount; rowIndex++)
for (int colIndex = 0; rowIndex < dataGrid.VisibleColCount; colIndex++)
{
object cellValue = dataGrid[rowIndex, colIndex];
... do something with cellValue here ...
}


Nov 17 '05 #3
I would agree with Igancio. It would be preferable to go through the actual
datasource as opposed to the grid. For one thing, performance-wise, it will
be much faster.

That said, I believe the indexer will return the underlying value and not
the transformed text, so that shouldn't be a problem.

Pete
Hi,

That;s not a very good idea to do, you see the datagrid is meant only to
make the data visible, not to retrieve it. for that use the binded
source.

The reason is that the "data" itself may be transformed, maybe you have a
column that in the datasource was a double or a decimal and in the grid is
displayed as a currency formatted using the locate in place ( $XX,XXX.YY
or $XX.XXX,YY ) convert that back to the original value can be difficult

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Rik Brooks" <rb*****@hhcc.com.donotspam> wrote in message
news:B0**********************************@microsof t.com...
I find it amazing that I am quickly able to find answers to obscure
questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in each
column.

I can't figure out how to do that. It has to be simple, can somebody
please
help me?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the
audience.

Animadverto est verus


Nov 17 '05 #4
Rik,

Why will you do that if your data is already in your datasource?

Cor
"Rik Brooks" <rb*****@hhcc.com.donotspam> schreef in bericht
news:B0**********************************@microsof t.com...
I find it amazing that I am quickly able to find answers to obscure
questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in each
column.

I can't figure out how to do that. It has to be simple, can somebody
please
help me?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the audience.

Animadverto est verus

Nov 17 '05 #5
Actually, I can think of several reasons you might want to: First of all, he
may be implementing code that works with a datagrid for use by other people.
He may not know what the data source is. A datagrid can be bound to any
object that IList or IListSource. If he doesn't know the nature of the data
source, then he would probably have to use the CurrencyManager to iterate
through the data. This would be unpleasant given the lack of documentation
of the CurrencyManager.

The easy solution would simply be to iterate through the grid cells and get
the data.

I'm not saying that's what he's doing, and iterating through the original
datasource may be the better solution for him. I'm simply saying, this is a
reason why that may not be preferable.

Pete

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ob*************@tk2msftngp13.phx.gbl...
Rik,

Why will you do that if your data is already in your datasource?

Cor
"Rik Brooks" <rb*****@hhcc.com.donotspam> schreef in bericht
news:B0**********************************@microsof t.com...
I find it amazing that I am quickly able to find answers to obscure
questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in each
column.

I can't figure out how to do that. It has to be simple, can somebody
please
help me?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the
audience.

Animadverto est verus


Nov 17 '05 #6
Pete,

How can he use the bindingmanager as he does not know the datasource, as
well how can he even get any data in a datagrid without a datasource?

I am really curious as you can show me how?

The documentation for the currencymanager is easy to find by the way and I
will not call this bad.

http://msdn.microsoft.com/library/de...classtopic.asp

Cor
Nov 17 '05 #7
> Pete,

How can he use the bindingmanager as he does not know the datasource, as
well how can he even get any data in a datagrid without a datasource?

I am really curious as you can show me how?

The documentation for the currencymanager is easy to find by the way and I
will not call this bad.

http://msdn.microsoft.com/library/de...classtopic.asp

Cor


Having implemented a hierarchical databound grid using the CurrencyManager,
I can promise you that the existing documentation was far from sufficient.
Were it not for tools like Reflector, it simply would have been impossible.

I'm sorry I wasn't more clear in my response. He may not know the specifics
of what the datasource is. That is to say, it could be a DataSet, a
DataTable, or any other IList or IListSource provider. Certainly he can get
the DataSource from the grid and he could then try to determine its type,
but from a generic point of view, he'd probably need to use the
CurrencyManager to then iterate through the data.

Now, one of the problems with doing this is that by iterating through the
CurrencyManager, he then going to implicitly force all bound controls to
iterate through the data as the Position changes. He could get around this
by creating a new BindingContext object and getting the CurrencyManager
from that, but the easiest thing would simply be to go through the grid.

As for the documentation issues, here's an example: The documentation says,
the CurrentChanged event "Occurs when the bound value changes." Well, that's
not very accurate at all. Changes how? If a value in a row in a bound
DataTable is changed? If the current row index (or CurrencyManager.Position)
changes?

The fact is, the CurrentChanged is sent for a variety of operations. If the
source implements IBindingList, then any ListChanged events will trigger a
CurrentChanged, regardless of whether the current item is the item affected
or not. Several other operations trigger a CurrentChanged whether or not the
record at the current CurrencyManager.Position is involved or not.

So the documentation for that one event is seriously misleading.

The MetaDataChanged event says that it "Occurs when the metadata of the List
has changed." Again, misleading in that it doesn't go on to say that this
only applies to objects that implement IBindingList. Any object implementing
IList but not IBindingList will not trigger a MetaDataChanged event if the
metadata of the list changes.

So yes, technically, it's documented, but the documentation is sorely
lacking in my opinion. I could go on and on all day about things about the
CurrencyManager that don't agree with the documentation. I'm intimately
familiar with both.

Pete
Nov 17 '05 #8
Hi,

No really, when each of the collection item is binded it's transformed
depending of the columns, for example, a bool column could be transformed in
a checkbox, the indexer ( at least in the case of the web grid ) is a
DataGridItem collection. It's difficult , if not impossible, to get from
the displayed grid back to the data itself.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:J-********************@giganews.com...
I would agree with Igancio. It would be preferable to go through the actual
datasource as opposed to the grid. For one thing, performance-wise, it will
be much faster.

That said, I believe the indexer will return the underlying value and not
the transformed text, so that shouldn't be a problem.

Pete
Hi,

That;s not a very good idea to do, you see the datagrid is meant only to
make the data visible, not to retrieve it. for that use the binded
source.

The reason is that the "data" itself may be transformed, maybe you have a
column that in the datasource was a double or a decimal and in the grid
is displayed as a currency formatted using the locate in place (
$XX,XXX.YY or $XX.XXX,YY ) convert that back to the original value can be
difficult

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Rik Brooks" <rb*****@hhcc.com.donotspam> wrote in message
news:B0**********************************@microsof t.com...
I find it amazing that I am quickly able to find answers to obscure
questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in
each
column.

I can't figure out how to do that. It has to be simple, can somebody
please
help me?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the
audience.

Animadverto est verus



Nov 17 '05 #9
Hi,

First of all, this only apply to a win environment, the OP did not especify
if he is developing a win or web app ( nor he has posted back until now ).

If he is using a win app then you could keep a reference to the binded item
in that case, this could be done I think ,I do not have much experience
using the win datagrid but I'm pretty sure it can be done.

ITOH, you know for a fact that the datasource is iterable, so you could use
that fact along with reflection to try to access the original values.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:Sc********************@giganews.com...
Actually, I can think of several reasons you might want to: First of all,
he may be implementing code that works with a datagrid for use by other
people. He may not know what the data source is. A datagrid can be bound
to any object that IList or IListSource. If he doesn't know the nature of
the data source, then he would probably have to use the CurrencyManager to
iterate through the data. This would be unpleasant given the lack of
documentation of the CurrencyManager.

The easy solution would simply be to iterate through the grid cells and
get the data.

I'm not saying that's what he's doing, and iterating through the original
datasource may be the better solution for him. I'm simply saying, this is
a reason why that may not be preferable.

Pete

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ob*************@tk2msftngp13.phx.gbl...
Rik,

Why will you do that if your data is already in your datasource?

Cor
"Rik Brooks" <rb*****@hhcc.com.donotspam> schreef in bericht
news:B0**********************************@microsof t.com...
I find it amazing that I am quickly able to find answers to obscure
questions
but simple ones elude me.

I have a datagrid that I've bound, no problem at all. Now I want to -
programatically - loop through each row, extracting the data held in
each
column.

I can't figure out how to do that. It has to be simple, can somebody
please
help me?
--
----------------------------------------
Magic is not in the hands of the magician but in the mind of the
audience.

Animadverto est verus



Nov 17 '05 #10
> Hi,

First of all, this only apply to a win environment, the OP did not
especify if he is developing a win or web app ( nor he has posted back
until now ).

If he is using a win app then you could keep a reference to the binded
item in that case, this could be done I think ,I do not have much
experience using the win datagrid but I'm pretty sure it can be done.

ITOH, you know for a fact that the datasource is iterable, so you could
use that fact along with reflection to try to access the original values.

cheers,


Ignacio,

You're correct. You know that it implements IList or IListSource (I am
assuming the WinForm DataGrid, I don't do much web stuff. The web grid
obviously is completely different). But based on that, you could iterate
through the data and use reflection. Generally this will work. The problem
you can run into there is with reflection permissions. Generally not a
problem, but it can be in some cases.

That said, iterating over the indexer DOES work, regardless of the displayed
cell type. I just did a test with TextBox and CheckBox columns. My code was
slightly wrong. It should be (assuming that the add a new row is active):

for (int rowIndex = 0; rowIndex < dataGrid1.VisibleRowCount - 1; rowIndex++)
for (int colIndex = 0; colIndex < dataGrid1.VisibleColumnCount;
colIndex++)
{
object cellValue = dataGrid1[rowIndex, colIndex];
Debug.WriteLine(cellValue.ToString());
}

For example, in my grid, dataGrid1[0, 0] is a boolean column in the
datatable but a checkbox in the grid. dataGrid1[0, 0], returns a boxed
System.Boolean with the value of the cell.

Pete
Nov 17 '05 #11
Hi Pete,

I'm in the opposite side , I know a lot about the web grid and almost
nothing from the win version :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:D6********************@giganews.com...
Hi,

First of all, this only apply to a win environment, the OP did not
especify if he is developing a win or web app ( nor he has posted back
until now ).

If he is using a win app then you could keep a reference to the binded
item in that case, this could be done I think ,I do not have much
experience using the win datagrid but I'm pretty sure it can be done.

ITOH, you know for a fact that the datasource is iterable, so you could
use that fact along with reflection to try to access the original values.

cheers,


Ignacio,

You're correct. You know that it implements IList or IListSource (I am
assuming the WinForm DataGrid, I don't do much web stuff. The web grid
obviously is completely different). But based on that, you could iterate
through the data and use reflection. Generally this will work. The problem
you can run into there is with reflection permissions. Generally not a
problem, but it can be in some cases.

That said, iterating over the indexer DOES work, regardless of the
displayed cell type. I just did a test with TextBox and CheckBox columns.
My code was slightly wrong. It should be (assuming that the add a new row
is active):

for (int rowIndex = 0; rowIndex < dataGrid1.VisibleRowCount - 1;
rowIndex++)
for (int colIndex = 0; colIndex < dataGrid1.VisibleColumnCount;
colIndex++)
{
object cellValue = dataGrid1[rowIndex, colIndex];
Debug.WriteLine(cellValue.ToString());
}

For example, in my grid, dataGrid1[0, 0] is a boolean column in the
datatable but a checkbox in the grid. dataGrid1[0, 0], returns a boxed
System.Boolean with the value of the cell.

Pete

Nov 17 '05 #12
> Hi Pete,

I'm in the opposite side , I know a lot about the web grid and almost
nothing from the win version :)

cheers,


Ignacio,

Well, since the original poster didn't reply with which he's using, I figure
he has his answer whichever one it is :-)

Pete
Nov 17 '05 #13
Pete,

About what kind of DataGrid are you talking? When we are talking here about
a datagrid than it is a standard Microsoft WindowsForm datagrid or Webform
datagrid.

Both you can only populate using the datasource (or with winform its
databinding equivalent). For both you have to know what you are populating.
There is nothing to search. You know it.

Cor
Nov 17 '05 #14
> Pete,

About what kind of DataGrid are you talking? When we are talking here
about a datagrid than it is a standard Microsoft WindowsForm datagrid or
Webform datagrid.

Both you can only populate using the datasource (or with winform its
databinding equivalent). For both you have to know what you are
populating. There is nothing to search. You know it.

Cor

I'm thinking in terms of the WinForm datagrid. I don't know much about
WebForms datagrid.

Okay, let's say, for example, I write an library that adds functionality to
the datagrid. Then other people use this library. My library needs to access
data in the grid as part of its function. I do not, beforehand, know the
exact nature of the data being bound to the grid. All I know is that it must
implement either IList or IListSource. Beyond that, I know nothing else
about the data beforehand.

My point being, if I need find out the contents of the data as part of my
library, the easiest way is going to be directly through the grid indexer.
It's not necessarily the best way, but it's certainly the easiest way.
Pete
Nov 17 '05 #15
Pete,

Okay, let's say, for example, I write an library that adds functionality
to the datagrid.


Than it is not a standard windowsform datagrid. In addition from a bad
design if you can not use the datasource of that.

Cor
Nov 17 '05 #16

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

Similar topics

1
1358
by: Asha | last post by:
grettings, i have been searching the web for articles on how to add new rows to datagrid... most of it does postback. do anyone here know of a good article which uses javascript to add new rows...
5
2419
by: Jay Villa | last post by:
Is it possible to update only few columns in datagrid during OnUpdateCommand event ? If so, could you help me .... -thanks Jay
5
2721
by: Alfredo Barrientos | last post by:
Hi, Is it possible editing all rows in datagrid?, i mean all fields are enabled to edit?, and update all values in just one click? Thanks, Alfredo Barrientos
2
2371
by: Karl | last post by:
Hi there, Need help, thanks first. In a winform datagrid, if I find the data with some error, I want to hight the row. It could be multiple rows. But Datagrid.Select() only allows one row. How...
2
9751
by: Shailja | last post by:
Can anyone tell me how to count no of rows into DataGrid at runtime after entering data into it?
4
1219
by: selvamariappan | last post by:
Hi all , how to display various color for rows in datagrid in vb.net 2005, Not a Alternate colors, Thanks and regards selvamariappan.c
0
1220
by: mahendra dubey | last post by:
Hi There I am using Datagrid control provided by wpftoolkit in wpf.I have set ItemsSource property of datagrid.Now Problem is that rows of datagrid takes only the space needed to display,...
6
2897
by: viki1967 | last post by:
Hi all. I need your help. I realize this script and I do not know where to begin: 1) A simple grid rows / columns. 2) The first column contain an input type = "checkbox" 3) When select...
3
7025
by: ronakinuk | last post by:
how can i unhide rows/columns in excel 2007
0
7242
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,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
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...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
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...
1
5063
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
446
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...

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.