Account Unknown
Account Unknown

Reputation: 119

DataGridView does not contain a definition for 'Index'

I am trying to run this code to get values from a DataGridView to a string array.

string[,] invoice = new string[dataGridView1.Rows.Count,dataGridView1.Columns.Count];

foreach (DataGridView row in dataGridView1.Rows)
{
    foreach (DataGridView col in dataGridView1.Columns)
    {
        invoice[row.Index, col.Index] = dataGridView1.Rows[row.Index].Cells[col.Index].Value.ToString();
    }
}

I am getting this Error:

'System.Windows.Forms.DataGridView' does not contain a definition for 'Index'

I don't own this code, I got this from here.

Any help is appreciated.

Upvotes: 0

Views: 1590

Answers (2)

W0lfw00ds
W0lfw00ds

Reputation: 2086

You have a wrong types inside the loop, try:

foreach (DataGridViewRow row in dataGridView1.Rows)

and:

foreach (DataGridViewColumn col in dataGridView1.Columns)

Upvotes: 2

Jigneshk
Jigneshk

Reputation: 348

Check this.

string[,] invoice = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewColumn col in dataGridView1.Columns)
                {
                    invoice[row.Index, col.Index] = dataGridView1.Rows[row.Index].Cells[col.Index].Value.ToString();
                }
            }

Upvotes: 1

Related Questions