2

In my application i am trying to get datagridview (5rows and 5columns)data to a 2d array,can anybody help to get data in to array...

        int countColumn = dataGridView1.ColumnCount -1;
        int j=0,k=0;
        string dataFromGrid = "";
        foreach (DataGridViewRow i in dataGridView1.Rows)
        {
            if (!i.IsNewRow)
            {


                //dataFromGrid = r.Cells[0].Value.ToString();

                for ( j = 0; j <= countColumn; j++)
                {
                    dataFromGrid = dataFromGrid + i.Cells[j].Value.ToString();


                }

            }
           var[k,j], Convert.ToInt32(dataFromGrid)
           k=k+1;
1
  • Inner loop condition should be j < countColumn Commented Sep 9, 2014 at 14:07

3 Answers 3

4

If you fancy the foreach loop, you could do this:

var array = new object[dataGridView1.RowCount,dataGridView1.ColumnCount];
foreach (DataGridViewRow i in dataGridView1.Rows)
{
    if (i.IsNewRow) continue;
    foreach (DataGridViewCell j in i.Cells)
    {
        array[j.RowIndex, j.ColumnIndex] = j.Value;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure I understood correctly, but, your array 'value' will actually be a 2x2 array containing all your values from your grid. In order to "get the value" from the array, you'll need to construct a string containing all the items from the array, that is parsing again the array, and creating the string. If you'd like, you could create this string when you're populating the array, thus eliminating an unnecessary loop. But again, I don't fully understand what you mean by "print the array value in textbox".
Well, you have to consider that there are multiple values in your array. If you have only one textbox, then you'll have to decide yourself what value from the array to put into it. If you already have your indexes, you just have to grab the value from your array: myTextBox.Text = array[iIndex, jIndex].ToString();
2
object[,] arr2d = new object[dataGridView1.Rows.Count,dataGridView1.Columns.Count];

for (int x = 0; x < arr2d.GetLength(0); x++)
     for (int i = 0; i < arr2d.GetLength(1); i++)
            arr2d[x, i] = dataGridView1.Rows[x].Cells[i].Value;

This should work

1 Comment

@user3413736 just cycle it again and add the values to .Text
0
//first of all create a 2D array varialble and count the rows and //column of data grid view
string[,] dataValue = new string[gridViewBillingItems.Rows.Count, gridViewBillingItems.Columns.Count];

//loop through the rows of data grid view 
foreach (DataGridViewRow row in gridViewBillingItems.Rows)
{
    //getting index of row
    int i = row.Index;
    //loop through the column of data grid view

    foreach (DataGridViewColumn col in gridViewBillingItems.Columns)
    {
        //getting index of column
        int j = col.Index;
        //add all the rows and column to 2D Array
        dataValue[row.Index, col.Index] = gridViewBillingItems.Rows[i].Cells[j].Value.ToString();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.