get data from datagridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shripal
    New Member
    • Jul 2008
    • 6

    get data from datagridview

    hello,

    i am new to c#.net.

    i have imported data in datagridview form ms access database.

    now i want to store each row of datagridview saperatly in string array

    so in saperate class i have created string array named dirurl

    my code is as follows

    IN CLASS1

    code:

    public static string[] dirurl = new string[100];

    and in my form i want to get as follows ::

    int tin = dataGridView1.R owCount;

    for (int j=0; j <= tin; j++)
    {
    Class1.dirurl[j] = dataGridView1.R ows[j].Cells[1].Value.ToString ();

    }

    but when program goes to execute for loop it gives me following error:

    Object reference not set to an instance of an object.

    please help me to get data in dirurl array...
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Not sure why you want to declare your string in a class and call it from the form? Suggest debug. Does Class1.dirurl[j] have a value for any instances of j?

    Comment

    • shripal
      New Member
      • Jul 2008
      • 6

      #3
      Originally posted by kenobewan
      Not sure why you want to declare your string in a class and call it from the form? Suggest debug. Does Class1.dirurl[j] have a value for any instances of j?


      actually i declared dirurl array in class for SIMPLICITY so that i can get data of it by any form in my application .

      No,class1.dirur l[j] has not any value for any instance of j.but i want to assign it by for loop as mentioned earlier .

      I want to know why the error comes like

      "Object reference not set to an instance of an object."

      OR

      alternatively can you give me an idea by which i can get data from datagridview1 and store in string array for further manipulation..

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        Here is an exmple:
        Code:
        System.Collections.ArrayList array = new System.Collections.ArrayList();
        for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
        array.Add(this.dataGridView1[i, 0].Value); /// Make sure the second
        value is a valid row

        Comment

        • shripal
          New Member
          • Jul 2008
          • 6

          #5
          Originally posted by kenobewan
          Here is an exmple:
          Code:
          System.Collections.ArrayList array = new System.Collections.ArrayList();
          for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
          array.Add(this.dataGridView1[i, 0].Value); /// Make sure the second
          value is a valid row



          thanks a lot .

          it is working now....

          thanks again.

          Comment

          Working...