locked
datagrid text as string array RRS feed

  • Question

  • for my friends

    i have form in c#  contains a datagridview dgview1 contains data of three column (column1,coulumn2,column3)

    i have one button1 also

    int k=dgview1.rows.count
    and

    i need to get a array of string like  STRING A[1]=column1.text+column2.text+column3.text;  till A[i]  by

    pressing a command button ..  how i will get it


    regards


    sadique







    ---

    Friday, March 20, 2009 3:15 PM

Answers

  • Ok, You are still asking, do this way, and also try to understand how its looping

    Outer loop is row and inner loop is column. I hope you are concatenating the string not summing up values, if you are summing numeric value then use array of double or integer or decimal, whatever suits you. One more thing, if grid contains null values then following code might generate error, check for that as well


                 int Length = dataGridView1.Rows.Count;  
                string[] A = new string[Length];   
     
                int c = 0;  
                foreach (DataGridViewRow dvr in dataGridView1.Rows)  
                {  
                    string v = string.Empty;  
                    foreach (DataGridViewCell dvc in dvr.Cells)  
                    {  
                       v += dvc.Value.ToString();  
                          
                    }  
                    A[c] =v;  
                    c += 1;  
                } 

    Arjun Paudel
    • Marked as answer by hot n thought Saturday, March 21, 2009 4:20 PM
    Friday, March 20, 2009 6:37 PM

All replies

  • Something like :

    dataGridView.Rows[rowIndex].Cells[columnIndex].Value.ToString();

    http://blog.voidnish.com
    Friday, March 20, 2009 3:21 PM
  • pls

    i need my string  like A[1],A[2].....a[i]

    where i=dgview1.rows.count


    awaiting valuable replay from anybody


    Friday, March 20, 2009 5:10 PM
  • Have you tried out the code in my reply to you? Or do you not know how to concatenate strings?

    You can do :

    arr[i] = dataGridView.Rows[i].Cells[0].Value.ToString() + dataGridView.Rows[i].Cells[1].Value.ToString() + dataGridView.Rows[i].Cells[2].Value.ToString();
    http://blog.voidnish.com
    • Proposed as answer by Arjun Paudel Friday, March 20, 2009 6:37 PM
    Friday, March 20, 2009 5:14 PM
  • Ok, You are still asking, do this way, and also try to understand how its looping

    Outer loop is row and inner loop is column. I hope you are concatenating the string not summing up values, if you are summing numeric value then use array of double or integer or decimal, whatever suits you. One more thing, if grid contains null values then following code might generate error, check for that as well


                 int Length = dataGridView1.Rows.Count;  
                string[] A = new string[Length];   
     
                int c = 0;  
                foreach (DataGridViewRow dvr in dataGridView1.Rows)  
                {  
                    string v = string.Empty;  
                    foreach (DataGridViewCell dvc in dvr.Cells)  
                    {  
                       v += dvc.Value.ToString();  
                          
                    }  
                    A[c] =v;  
                    c += 1;  
                } 

    Arjun Paudel
    • Marked as answer by hot n thought Saturday, March 21, 2009 4:20 PM
    Friday, March 20, 2009 6:37 PM
  • thanks a lot for this nice forum .. and for arjun and for god also

    sadique
    Saturday, March 21, 2009 4:23 PM
  • thanks a lot

    my code is working properly


    regards

    sadique
    Saturday, March 21, 2009 4:25 PM