Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. data grid view

data grid view

Scheduled Pinned Locked Moved C#
cssdatabasehelp
20 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Kawshik_itbd

    if i do

    for (int i = 0; i <=

    t.Rows.Count-1; i++)
    {
    dataGridView1.Rows.Add();
    dataGridView1.Rows[i].Cells["Column1"].Value ="roy";

            }</pre>
    

    my prob is solve but i don;t add dataGridView1.Rows.Add() prog can not be execute through a exception like Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index whatever i do either t.Rows.Count-1 or t.Rows.Count
    plz help what can i do

    V Offline
    V Offline
    vonb
    wrote on last edited by
    #4

    Change

    ...
    dataGridView1.Rows[i].Cells[...
    ...

    to

    ...
    dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[...
    ...

    Your counter is based on t, which is not equal to the dataGridView.

    The signature is in building process.. Please wait...

    K F 2 Replies Last reply
    0
    • V vonb

      Change

      ...
      dataGridView1.Rows[i].Cells[...
      ...

      to

      ...
      dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[...
      ...

      Your counter is based on t, which is not equal to the dataGridView.

      The signature is in building process.. Please wait...

      K Offline
      K Offline
      Kawshik_itbd
      wrote on last edited by
      #5

      two row in my data table it shows only last row

      String sql = "select Name,Gender,Course1,Course2 from Info ";
      DataTable t = DataAccess.GetDataTable(sql);
      for (int i = 0; i <t.Rows.Count; i++)
      {
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Column1"].Value =t.Rows[i]["Name"] ;
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Column2"].Value =t.Rows[i]["Gender"] ;
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Column3"].Value =t.Rows[i]["Course1"] ;
      dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Column4"].Value = t.Rows[i]["Course2"];

             }
      

      how i show all the row in gridview plz solve this prob :(

      1 Reply Last reply
      0
      • V vonb

        Change

        ...
        dataGridView1.Rows[i].Cells[...
        ...

        to

        ...
        dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[...
        ...

        Your counter is based on t, which is not equal to the dataGridView.

        The signature is in building process.. Please wait...

        F Offline
        F Offline
        Freak30
        wrote on last edited by
        #6

        This way you end up just filling the same row over and over, also it fails if there is no row in the DataGridView. I would do it that way instead:

        if (dataGridView1.Rows.Count <= i)
        dataGridView1.Rows.Add();
        dataGridView1.Rows[i].Cells[...

        The good thing about pessimism is, that you are always either right or pleasently surprised.

        K 1 Reply Last reply
        0
        • F Freak30

          This way you end up just filling the same row over and over, also it fails if there is no row in the DataGridView. I would do it that way instead:

          if (dataGridView1.Rows.Count <= i)
          dataGridView1.Rows.Add();
          dataGridView1.Rows[i].Cells[...

          The good thing about pessimism is, that you are always either right or pleasently surprised.

          K Offline
          K Offline
          Kawshik_itbd
          wrote on last edited by
          #7

          thank you Freak30 that means always add dataGridView1.Rows.Add()

          1 Reply Last reply
          0
          • K Kawshik_itbd

            for (int i = 0; i < t.Rows.Count; i++)
            {

                   dataGridView1.Rows\[i\].Cells\["Column1"\].Value = t.Rows\[0\]\["Address"\].ToString();
            
               }
            

            error occur :

            index was out of range,must be non-negative and less than the
            size of the collection. parameter name:index

            how i solve this prob,

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            You are using the list t as the source of your index count, rather than dataGridView1, it should be:

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
            dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString();
            }

            (or similar).

            Veni, vidi, abiit domum

            K 1 Reply Last reply
            0
            • L Lost User

              You are using the list t as the source of your index count, rather than dataGridView1, it should be:

              for (int i = 0; i < dataGridView1.Rows.Count; i++)
              {
              dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString();
              }

              (or similar).

              Veni, vidi, abiit domum

              K Offline
              K Offline
              Kawshik_itbd
              wrote on last edited by
              #9

              is this right ? String sql = "select Name,Gender,Course1,Course2 from Info "; DataTable t = DataAccess.GetDataTable(sql); MessageBox.Show(Convert.ToString(dataGridView1.Rows.Count)); for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; }

              F L 2 Replies Last reply
              0
              • K Kawshik_itbd

                is this right ? String sql = "select Name,Gender,Course1,Course2 from Info "; DataTable t = DataAccess.GetDataTable(sql); MessageBox.Show(Convert.ToString(dataGridView1.Rows.Count)); for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; }

                F Offline
                F Offline
                Freak30
                wrote on last edited by
                #10

                Should be ok, provided you want to append every time you call this function. Else you will probably have to clear the GridControl first.

                The good thing about pessimism is, that you are always either right or pleasently surprised.

                K 1 Reply Last reply
                0
                • K Kawshik_itbd

                  is this right ? String sql = "select Name,Gender,Course1,Course2 from Info "; DataTable t = DataAccess.GetDataTable(sql); MessageBox.Show(Convert.ToString(dataGridView1.Rows.Count)); for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; }

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  Kawshik_itbd wrote:

                  is this right ?

                  It looks like it probably is; what happens when you try it? I would also ask why you are not using databinding to build your view automatically.

                  Veni, vidi, abiit domum

                  K 1 Reply Last reply
                  0
                  • L Lost User

                    Kawshik_itbd wrote:

                    is this right ?

                    It looks like it probably is; what happens when you try it? I would also ask why you are not using databinding to build your view automatically.

                    Veni, vidi, abiit domum

                    K Offline
                    K Offline
                    Kawshik_itbd
                    wrote on last edited by
                    #12

                    it works .how i use databinding? can you gives some example of code ??

                    L 1 Reply Last reply
                    0
                    • F Freak30

                      Should be ok, provided you want to append every time you call this function. Else you will probably have to clear the GridControl first.

                      The good thing about pessimism is, that you are always either right or pleasently surprised.

                      K Offline
                      K Offline
                      Kawshik_itbd
                      wrote on last edited by
                      #13

                      can you gives some basics about GridControl?

                      1 Reply Last reply
                      0
                      • K Kawshik_itbd

                        it works .how i use databinding? can you gives some example of code ??

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        There are lots of samples and articles around that explain databinding and how it can speed up your applications.

                        Veni, vidi, abiit domum

                        K 1 Reply Last reply
                        0
                        • L Lost User

                          There are lots of samples and articles around that explain databinding and how it can speed up your applications.

                          Veni, vidi, abiit domum

                          K Offline
                          K Offline
                          Kawshik_itbd
                          wrote on last edited by
                          #15

                          the way that i follow is n;t correct or not ??

                          L 1 Reply Last reply
                          0
                          • K Kawshik_itbd

                            the way that i follow is n;t correct or not ??

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #16

                            Yes, it's correct, in the sense that it does what you need, but databinding is much more powerful and you can do it with a single line of code.

                            Veni, vidi, abiit domum

                            K 1 Reply Last reply
                            0
                            • L Lost User

                              Yes, it's correct, in the sense that it does what you need, but databinding is much more powerful and you can do it with a single line of code.

                              Veni, vidi, abiit domum

                              K Offline
                              K Offline
                              Kawshik_itbd
                              wrote on last edited by
                              #17

                              thnaks for advice :)

                              1 Reply Last reply
                              0
                              • V vonb

                                Kawshik_itbd wrote:

                                for (int i = 0; i < t.Rows.Count; i++) {   dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString();   }

                                Change to

                                for (int i = 0; i

                                This should do it..

                                The signature is in building process.. Please wait...

                                S Offline
                                S Offline
                                sanket164
                                wrote on last edited by
                                #18

                                you can also write like this.. //this line write before for loop //with this line grid(dataGridView1) create line as per dataTable(t) dataGridView1.RowCount=t.Rows.Count for (int i = 0; i < t.Rows.Count; i++) { dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString(); }

                                K 1 Reply Last reply
                                0
                                • K Kawshik_itbd

                                  for (int i = 0; i < t.Rows.Count; i++)
                                  {

                                         dataGridView1.Rows\[i\].Cells\["Column1"\].Value = t.Rows\[0\]\["Address"\].ToString();
                                  
                                     }
                                  

                                  error occur :

                                  index was out of range,must be non-negative and less than the
                                  size of the collection. parameter name:index

                                  how i solve this prob,

                                  S Offline
                                  S Offline
                                  sanket164
                                  wrote on last edited by
                                  #19

                                  you can also write like this.. //this line write before for loop //with this line grid(dataGridView1) create line as per dataTable(t) dataGridView1.RowCount=t.Rows.Count for (int i = 0; i < t.Rows.Count; i++) { dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString(); }

                                  1 Reply Last reply
                                  0
                                  • S sanket164

                                    you can also write like this.. //this line write before for loop //with this line grid(dataGridView1) create line as per dataTable(t) dataGridView1.RowCount=t.Rows.Count for (int i = 0; i < t.Rows.Count; i++) { dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString(); }

                                    K Offline
                                    K Offline
                                    Kawshik_itbd
                                    wrote on last edited by
                                    #20

                                    it does not show the multiple it shows only first row ,if i did like for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; } no prob occur.

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups