Storing Celvalues from first row in Array

feurich

Centurion
Joined
Oct 21, 2003
Messages
170
Location
Holland
Hi there,

I need to store all the values of the first row of a datagridview in to an array.
I have tried a lot but havent managed to do this.

Can someone help me on the way.

Thanks,
 
I have this....!! :confused:

Code:
int i = 0;
            string[] cellValues = null;
            // Only take the first row for the search values 

            foreach (DataGridViewCell dataCell in DataGridView1.SelectedRows[0].Cells)
            {
                cellValues[i] = dataCell.Value.ToString();
                i++;
            }
 
You would need to make the array big enough to hold each item. Something like

Code:
string[] cellValues = new string[DataGridView1.SelectedRows[0].Cells.Count]

that is probably a bit wrong as I don't have VS handy to test it.
 
The problem here is that i get an error on the SelectedRows[0].Cells.Count part of the statement.
It says: System.ArgumentoutOfRangeException in the for loop.
Actual value was null. But I am in the grid and there are 3 cells filled with data in the first row...
 
Hi There,

It's not the DataGridView1.SelectedRows[0] property that was needed but the DataGridView1.Rows[0].Cells.Count.

That solved this problem on the the next one.

Thanks PlausiblyDamp for showing the way.. :)
 
Hi There,

It's not the DataGridView1.SelectedRows[0] property that was needed but the DataGridView1.Rows[0].Cells.Count.

That solved this problem on the the next one.

Thanks PlausiblyDamp for showing the way.. :)
Code:
// Only take the first row for the search values 

string[] cellValues = new string[DataGridView1.Rows[0].Cells.Count];
for (int i = 0; i < this.DataGridView1.Rows[0].Cells.Count; i++)
   {
        cellValues[i] = DataGridView1.Rows[0].Cells[i].Value.ToString();
    }
 
Back
Top