Trying to get values of each column in a datagrid view

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am usign the code below to find the current row and cell. Which works OK.
Then hwat I want to do is get the value of each other cell in the row. So
below I am trying to get at least one values. The last line gives me an error
that I am not passing the correct arguments. But I am passing two integers,
one for the row and one for the cell. Any ideas how I can achieve this feat?

// gets the current row and cell being edited.
string msg = String.Format("Row: {0}, Column: {1}",
scriptDataGridView.CurrentCell.RowIndex,
scriptDataGridView.CurrentCell.ColumnIndex);
//MessageBox.Show(msg, "Current Cell");
rowNumber = msg.Substring(5, 1);
colNumber = msg.Substring(16, 1);
int.Parse(rowNumber);
int.Parse(colNumber);

command = scriptDataGridView[colNumber, rowNumber].FormattedValue.ToString();
 
Chris said:
I am usign the code below to find the current row and cell. Which works
OK. Then hwat I want to do is get the value of each other cell in the row.
So below I am trying to get at least one values. The last line gives me an
error that I am not passing the correct arguments. But I am passing two
integers, one for the row and one for the cell. Any ideas how I can
achieve this feat?

// gets the current row and cell being edited.
string msg = String.Format("Row: {0}, Column: {1}",
scriptDataGridView.CurrentCell.RowIndex,
scriptDataGridView.CurrentCell.ColumnIndex);
//MessageBox.Show(msg, "Current Cell");
rowNumber = msg.Substring(5, 1);
colNumber = msg.Substring(16, 1);
int.Parse(rowNumber);
int.Parse(colNumber);

command = scriptDataGridView[colNumber,
rowNumber].FormattedValue.ToString();
Well, it seems that you are parsing the numbers but not assigning to any
variable, you could so what u want in two ways:
1) command = scriptDataGridView[int.Parse(colNumber),
int.Parse(rowNumber)].FormattedValue.ToString();
Or the cleaner way...
2)
int rowNum = int.Parse(rowNumber);
int colNum = int.Parse(colNumber);
scriptDataGridView[colNum, rowNum].FormattedValue.ToString();
I hope it helps.
 
Thank you! Thank you! Thank you! Works great.


Jerónimo Milea said:
Chris said:
I am usign the code below to find the current row and cell. Which works
OK. Then hwat I want to do is get the value of each other cell in the row.
So below I am trying to get at least one values. The last line gives me an
error that I am not passing the correct arguments. But I am passing two
integers, one for the row and one for the cell. Any ideas how I can
achieve this feat?

// gets the current row and cell being edited.
string msg = String.Format("Row: {0}, Column: {1}",
scriptDataGridView.CurrentCell.RowIndex,
scriptDataGridView.CurrentCell.ColumnIndex);
//MessageBox.Show(msg, "Current Cell");
rowNumber = msg.Substring(5, 1);
colNumber = msg.Substring(16, 1);
int.Parse(rowNumber);
int.Parse(colNumber);

command = scriptDataGridView[colNumber,
rowNumber].FormattedValue.ToString();
Well, it seems that you are parsing the numbers but not assigning to any
variable, you could so what u want in two ways:
1) command = scriptDataGridView[int.Parse(colNumber),
int.Parse(rowNumber)].FormattedValue.ToString();
Or the cleaner way...
2)
int rowNum = int.Parse(rowNumber);
int colNum = int.Parse(colNumber);
scriptDataGridView[colNum, rowNum].FormattedValue.ToString();
I hope it helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top