By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,980 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,980 IT Pros & Developers. It's quick & easy.

Datagrid Question

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.