473,499 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datagrid Question

I would like to read the value in a certain column of my datagrid. Can
someone please help me with the syntax?

I found one way to do this was:
string myString = dgListPersonnel.SelectedItem.Cells[1].Text

But it must be old because I get the following error:
error: 'dgListPersonnel.SelectedItem.Cells' does not exist
Mar 27 '06 #1
8 1806

You could cast the DataSource to a DataTable and access it with x and y
coordinates. This works with .NET 1.0, 1.1 and 2.0.

Please remember to start on row and column 0 and to test for selected values
larger than -1 and smaller than
this.dataGridView1.Columns.Count and this.dataGridView1.Rows.Count, or you
could access a pointer to NULL.

System.Data.DataTable DataTable =
(System.Data.DataTable)this.dataGridView1.DataSour ce;
string data = DataTable.Rows[1][2].ToString();
Regards,
Lars-Inge Tønnessen
Mar 27 '06 #2
Thanks for the reply, but it sounds like you are telling me to iterate
through the datagrid (as a datatable). I would like to use the selectedItem
property...if I can. I will have the user select a row and I want to get the
values out of the row they selected. Thanks again.

"Lars-Inge Tønnessen (VJ# MVP)" wrote:

You could cast the DataSource to a DataTable and access it with x and y
coordinates. This works with .NET 1.0, 1.1 and 2.0.

Please remember to start on row and column 0 and to test for selected values
larger than -1 and smaller than
this.dataGridView1.Columns.Count and this.dataGridView1.Rows.Count, or you
could access a pointer to NULL.

System.Data.DataTable DataTable =
(System.Data.DataTable)this.dataGridView1.DataSour ce;
string data = DataTable.Rows[1][2].ToString();
Regards,
Lars-Inge Tønnessen

Mar 27 '06 #3
On Mon, 27 Mar 2006 12:59:03 -0800, Mike Collins
<Mi*********@discussions.microsoft.com> wrote:
I would like to read the value in a certain column of my datagrid. Can
someone please help me with the syntax?

I found one way to do this was:
string myString = dgListPersonnel.SelectedItem.Cells[1].Text

But it must be old because I get the following error:
error: 'dgListPersonnel.SelectedItem.Cells' does not exist


From the help files:

private void myDataGrid_CurrentCellChanged(object sender,
System.EventArgs e)
{
MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber
+ ", Row is " + myDataGrid.CurrentCell.RowNumber
+ ", Value is " + myDataGrid[myDataGrid.CurrentCell] );
}

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 28 '06 #4
Thanks for the reply. I will not be able to check this until Tuesday morning,
but this will give me what I want by a user just selecting a row?

"Otis Mukinfus" wrote:
On Mon, 27 Mar 2006 12:59:03 -0800, Mike Collins
<Mi*********@discussions.microsoft.com> wrote:
I would like to read the value in a certain column of my datagrid. Can
someone please help me with the syntax?

I found one way to do this was:
string myString = dgListPersonnel.SelectedItem.Cells[1].Text

But it must be old because I get the following error:
error: 'dgListPersonnel.SelectedItem.Cells' does not exist


From the help files:

private void myDataGrid_CurrentCellChanged(object sender,
System.EventArgs e)
{
MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber
+ ", Row is " + myDataGrid.CurrentCell.RowNumber
+ ", Value is " + myDataGrid[myDataGrid.CurrentCell] );
}

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Mar 28 '06 #5
On Mon, 27 Mar 2006 18:59:02 -0800, Mike Collins
<Mi*********@discussions.microsoft.com> wrote:
Thanks for the reply. I will not be able to check this until Tuesday morning,
but this will give me what I want by a user just selecting a row?

"Otis Mukinfus" wrote:
On Mon, 27 Mar 2006 12:59:03 -0800, Mike Collins
<Mi*********@discussions.microsoft.com> wrote:
>I would like to read the value in a certain column of my datagrid. Can
>someone please help me with the syntax?
>
>I found one way to do this was:
>string myString = dgListPersonnel.SelectedItem.Cells[1].Text
>
>But it must be old because I get the following error:
>error: 'dgListPersonnel.SelectedItem.Cells' does not exist


From the help files:

private void myDataGrid_CurrentCellChanged(object sender,
System.EventArgs e)
{
MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber
+ ", Row is " + myDataGrid.CurrentCell.RowNumber
+ ", Value is " + myDataGrid[myDataGrid.CurrentCell] );
}

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Well, I don't know if it will be as easy as you would like, but in the event,
you should be able to tell which row the user is on and with a private field in
the form compare the row number to the last one the user was on. After that you
can see if the cell is the one you want and if it is do your processing of the
info you wanted from the cell.

Kind of a round about way of doing it, but that's what I found when I looked at
the DataGrid help. There may be other ways, that I didn't find.

There are a lot of Mouse events you might look into. One or more of them might
be able to tell you the row and cell locations of the click.

Good luck with your project.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 28 '06 #6
I have found out how to get the value of an individual cell of a selected row.

protected void dgAccounts_FormatRow(object sender, DataGridItemEventArgs e)
{
string myString = e.Item.Cells[cellNumberIWant].Text;
}
__________________________________________________ __________________
"Otis Mukinfus" wrote:
On Mon, 27 Mar 2006 18:59:02 -0800, Mike Collins
<Mi*********@discussions.microsoft.com> wrote:
Thanks for the reply. I will not be able to check this until Tuesday morning,
but this will give me what I want by a user just selecting a row?

"Otis Mukinfus" wrote:
On Mon, 27 Mar 2006 12:59:03 -0800, Mike Collins
<Mi*********@discussions.microsoft.com> wrote:

>I would like to read the value in a certain column of my datagrid. Can
>someone please help me with the syntax?
>
>I found one way to do this was:
>string myString = dgListPersonnel.SelectedItem.Cells[1].Text
>
>But it must be old because I get the following error:
>error: 'dgListPersonnel.SelectedItem.Cells' does not exist

From the help files:

private void myDataGrid_CurrentCellChanged(object sender,
System.EventArgs e)
{
MessageBox.Show ("Col is " + myDataGrid.CurrentCell.ColumnNumber
+ ", Row is " + myDataGrid.CurrentCell.RowNumber
+ ", Value is " + myDataGrid[myDataGrid.CurrentCell] );
}

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Well, I don't know if it will be as easy as you would like, but in the event,
you should be able to tell which row the user is on and with a private field in
the form compare the row number to the last one the user was on. After that you
can see if the cell is the one you want and if it is do your processing of the
info you wanted from the cell.

Kind of a round about way of doing it, but that's what I found when I looked at
the DataGrid help. There may be other ways, that I didn't find.

There are a lot of Mouse events you might look into. One or more of them might
be able to tell you the row and cell locations of the click.

Good luck with your project.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Mar 28 '06 #7
I was thinking about adding a cell enter handler: (This works with .NET 1.0,
1.1 and 2.0).

private void CellEnter(object sender, DataGridViewCellEventArgs e)
{
System.Data.DataTable DataTable =
(System.Data.DataTable)this.dataGridView1.DataSour ce;
string value = DataTable.Rows[e.RowIndex][e.ColumnIndex].ToString();
System.Windows.Forms.MessageBox.Show(value);
}


Or with .NET 2.0 you could use this one:

for (counter = 0;counter < dataGridView1.SelectedCells.Count; counter++)
{
string value = dataGridView1.SelectedCells[counter].ToString();
System.Windows.Forms.MessageBox.Show(value);
}
Regards,
Lars-Inge Tønnessen
Mar 28 '06 #8
On Tue, 28 Mar 2006 12:50:03 -0800, Mike Collins
<Mi*********@discussions.microsoft.com> wrote:
I have found out how to get the value of an individual cell of a selected row.

protected void dgAccounts_FormatRow(object sender, DataGridItemEventArgs e)
{
string myString = e.Item.Cells[cellNumberIWant].Text;
}

Great. Glad you found a way.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 29 '06 #9

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

Similar topics

2
3247
by: magister | last post by:
Hello, I have xml like this.... <test> <question>sdfsa</question> <section><question>43ga</question> <question>asdf</question> </test>
0
1066
by: Randy | last post by:
Hello, I have two questions... I have a datagrid. I'm capturing the cell via HitTestInfo. The first question is fairly simple. I'm using an example of how to capture the row/column I found on the...
3
4418
by: BBFrost | last post by:
Ok, I know how to count the number of selected datagrid rows using the code below. What has me stumped is how to determine when the selected rows within a datagrid have been changed. The...
6
1697
by: BBFrost | last post by:
I'm using Net 1.1 (2003) SP1 & Windows 2000 Here's the issue ... Rows 12 thru 24 are selected in a datagrid. The user now unselects rows 12 thru 24 and selects rows 45 thru 70 ??? How can...
2
4315
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
2
16974
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
2
2314
by: Sky | last post by:
Hello: Another question about trying to wring functionality from a DataGrid... Have a DB table of "Contacts" -- 14 or more fields per record Show in datagrid -- but only 5 columns (First,Last,...
3
1556
by: Danky | last post by:
Hello Masters! Anyone can help me with the datagrid, well, the app load a lot of data from DB and it show on the datagrid, and well, I need to allow paging and.... the issue is with the event...
4
2258
by: Jan Nielsen | last post by:
Hi all I'm a former Access developer who would like to implement a many-to-many relation in about the same way you do in Access: With a subform and a combo box. Is it possible to use a...
13
2448
by: pmcguire | last post by:
I have a DataGrid control for which I have also created several new extended DataGridColumnStyles. They behave pretty nicely, but I can't figure out how to implement Selected Item formatting for...
0
7131
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
7007
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
7174
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,...
1
6894
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
5470
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,...
1
4919
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
3099
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...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
297
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.