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. Windows Forms
  4. DataGridView and Generics

DataGridView and Generics

Scheduled Pinned Locked Moved Windows Forms
helpquestion
6 Posts 3 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.
  • P Offline
    P Offline
    Ponzano Paolo
    wrote on last edited by
    #1

    Hello to everybody, I've got a problem with datagridview and generics, here's the piece of code : dataGridView1.ReadOnly = false; List myProds = new List(); myProds.Add(new Product("Prod 1", 1, 1)); myProds.Add(new Product("Prod 2", 2, 2)); myProds.Add(new Product("Prod 3", 3, 3)); dataGridView1.DataSource = myProds; myProds.Add(new Product("Prod 99", 99, 99)); on video I've 3 rows but in the datagrid.datasource I've 4 items. Where do I do wrong? Thanks in advance Paolo Ponzano

    C 1 Reply Last reply
    0
    • P Ponzano Paolo

      Hello to everybody, I've got a problem with datagridview and generics, here's the piece of code : dataGridView1.ReadOnly = false; List myProds = new List(); myProds.Add(new Product("Prod 1", 1, 1)); myProds.Add(new Product("Prod 2", 2, 2)); myProds.Add(new Product("Prod 3", 3, 3)); dataGridView1.DataSource = myProds; myProds.Add(new Product("Prod 99", 99, 99)); on video I've 3 rows but in the datagrid.datasource I've 4 items. Where do I do wrong? Thanks in advance Paolo Ponzano

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      As this is windows forms, there's no databind command, the grid is bound at the point that you set the data source. So, the objects you add after, are there, but the grid does not rebind every time you add an item.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      P 1 Reply Last reply
      0
      • C Christian Graus

        As this is windows forms, there's no databind command, the grid is bound at the point that you set the data source. So, the objects you add after, are there, but the grid does not rebind every time you add an item.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        P Offline
        P Offline
        Ponzano Paolo
        wrote on last edited by
        #3

        so what should I do? Thanks Paolo

        P 1 Reply Last reply
        0
        • P Ponzano Paolo

          so what should I do? Thanks Paolo

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Reset the datasource after you have added the new items.

          Deja View - the feeling that you've seen this post before.

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            Reset the datasource after you have added the new items.

            Deja View - the feeling that you've seen this post before.

            P Offline
            P Offline
            Ponzano Paolo
            wrote on last edited by
            #5

            First of all, thanks to all of you for your answe, I've resolved it using BindingList instead of List, now I've a problem of cross-threading but I'm gonna take care of it! Thanks again Paolo

            P 1 Reply Last reply
            0
            • P Ponzano Paolo

              First of all, thanks to all of you for your answe, I've resolved it using BindingList instead of List, now I've a problem of cross-threading but I'm gonna take care of it! Thanks again Paolo

              P Offline
              P Offline
              Ponzano Paolo
              wrote on last edited by
              #6

              ok, now that the problem has been fixed we've another one that came out, and for this I need your help at all!!! This is the main form event : private void Form1_Load(object sender, EventArgs e) { myWorker = new Worker(); dataGridView1.ReadOnly = false; dataGridView1.DataSource = myWorker.myList; Thread myThread = new Thread(new ThreadStart(myWorker.DoWork)); myThread.Start(); } The Worker class is this one : public class Worker { public BindingList myList = new BindingList(); public void DoWork() { Random r = new Random(); for(int i=0; i<1000; i++) { myList.Add(new Product("Test",r.Next(),r.Next())); Thread.Sleep(1000); } } } At the myList.Add(...) I get an exception of type "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." But this happen in a Business Object not in a Window's Control. I suppose this's because of the datagridview.Datasource property set, but how can I fix it? Thanks in advance, Bests

              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