none
DataGridView updating values RRS feed

  • Question

  • I have a DGV with values.  I want to separately add/delete rows and update cell values without creating new objects and duplicating columns.  Is this possible? If so how?
    • Moved by 宝宝徐 Wednesday, December 6, 2017 2:00 AM
    Tuesday, December 5, 2017 1:12 AM

Answers

  • Solved.  Updating the values in the table wasn't really the issue, it was loading the data into the DGV after.  I passed an instance of the form1 to form2, then, when the table was updated, use the passed instance of form1 to call the method in form1 to load the data.  
    Friday, February 16, 2018 11:44 PM

All replies

  • Hi,

    I suppose it is possible but I would like to confirm:
    (1) programming language? VB, C#, and so on
    (2) what do you mean by "without creating new objects and duplicate columns"?
    For example, is it permitted that we put [Add] and [Delete] button, or create ContextMenu for adding or deleting row?  

    This is my sample with Buttons and ContextMenu for adding/deleting row.
    Can it satisfy your requirements? 


    Regards,

    Ashidacchi

    • Edited by Ashidacchi Tuesday, December 5, 2017 5:15 AM
    Tuesday, December 5, 2017 2:16 AM
  • 1 C#

    2 I have a function that creates a DGV, adds columns, then calls another function which reads data from a database, creates objects for each read. Returns to the original function, then inserts each object into the DGV.  As the data in the DGV needs to be updated, I want to add/delete rows, update cells.  I want to bypass creating the DGV again, just update values.  There is no manual adding/deleting rows.

    Tuesday, December 5, 2017 12:00 PM
  • Hi,

    I will move your thread to winform forum, if you have problems about visual studio IDE, please feel free to let me know.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 6, 2017 1:51 AM
  • Hi Richard,

    I'm afraid you need to provide some code to describe your purpose, we can not understand what you mean about "update cell values without creating new objects" just through some words.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 6, 2017 6:30 AM
    Moderator
  • I have a function that creates DGV's and also populates them.  I don't want to execute the code that creates the DGV's again, but I want to change/update values in them.  So I want to separate them, one function that creates the DGV's with columns, the other that puts values in and changes them.  Firstly, is this possible?  If its not, any further questions are a waste. One step at a time.

    Wednesday, December 6, 2017 9:46 AM
  • Why won't you provide your code.
    Please remember that people who want to help you use their time for your issue. 

    Ashidacchi

    Wednesday, December 6, 2017 10:39 AM
  • I'm able to create the DGV's and populate them in the same function, that code works.  I'm trying to figure out separate them if possible.  I tried referring to the DGV's in a separate function via the form like this

    Form.DGV.Rows[rowNumber].Cells["ColumnName"].Value = 

    but I get variable OutOfRangeException as the Form doesn't exist in the context, so it doesn't have any rows, hence the exception.  If I call the original function, it duplicates all the columns, which I want to avoid.  Can I access the existing DGV's from another function?

    Wednesday, December 6, 2017 12:07 PM
  • Good-bye, Richard,
    I have no time to continue your issue... you will never understand what I mentioned.

    Ashidacchi

    Wednesday, December 6, 2017 12:14 PM
  • Hi Richard,

    You should be databinding your DGV to a BindingSource:

    // Define a Binding Source variable
    private BindingSource bs = new BindingSource();
    
    // Get the data, I don't know what kind it is. I'll assume DataTable, but it doesn't have to be
    // It could also  be BindingLists or other things
    
    DataTable dt = this.GetTheData();
    
    // DataBind it all together
    this.bs.DataSource = dt;
    this.dataGridView1.DataSource = this.bs;
    
    // Use the various BindingSource methods and properties
    // this.bs.Add(); this.bs.Remove(); this.bs.Position etc.
    // Or simply work directly on the data (dt in my example).
    And, in case it might be of further help, you could read some of the databinding blog posts I have written, by using this search query for my blog: https://geek-goddess-bonnie.blogspot.com/search?q=databind


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    • Edited by BonnieBMVP Wednesday, December 6, 2017 3:50 PM more goodies
    Wednesday, December 6, 2017 3:46 PM
  • This example code creates the DGV and add a column.

    DataGridView DataGridView1 = new DataGridView();
    DataGridViewTextBoxColumn Column1 = new DataGridViewTextBoxColumn();
    DataGridView1.Columns.Add(Column1);  // add 

    This code inserts data into a cell in a for loop

    Form.DataGridView1.Rows[x].Cells["Column1"].Value = value

    At the moment, everything works if they are executed in the same function.  My question is can I separate them into different functions without getting an OutOfRange exception?  If so, how?  

    Wednesday, December 6, 2017 3:52 PM
  • I'm reading from a database and creating objects from multiple classes.  Then populating the DGV from those objects.  The reason Binding isn't suitable in my case is that lets say Class Object has properties object1,object2,object3,object4, object3 displays if object1 is above a certain value, otherwise object4 displays.  If there were no dependencies, Binding would of course make sense.  
    Wednesday, December 6, 2017 4:07 PM
  • OK, but maybe your Class Object could have another property, object5. When you read the data into the Class Object, you could populate object5 based on object1:

    object5 = object1 > xxx ? object3 : object4;

    Then, you could use databinding (and you'd be binding that particular DGV column to object5).


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, December 6, 2017 4:37 PM
  • That seems like a workaround.  My main issue is though, initially, I'm creating DGV's and populating them in the same method.  When it comes to updating data in them, I want to assign values to rows/cells, without recreating the DGV columns again, which is what happens when I call the method again.  Is it possible to separate these actions into different methods?  
    Wednesday, December 13, 2017 6:50 PM
  • Hi,

    You can not add some new rows without column, if you have some columns, then you can only add the corresponding column data, If you want to add data beyond the scope of the column, there is an exception says out of range.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 18, 2017 8:04 AM
    Moderator
  • I have added columns in one method and inserted values.  I want to add/change values (not columns) in another method.  How do I reference an existing object (column) in another method?
    Monday, December 18, 2017 11:41 AM
  • What do your methods currently look like. I don't think we can make suggestions without seeing some of that code ...

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, December 18, 2017 11:30 PM
  • As long as the code is inside that public partial class its OK.  Because the code creating the DGV's is in a method, the DGV's are only in scope inside that method.  I want them in scope in multiple methods.  If I put that code outside the methods but within the public partial class, would that work?

    Tuesday, December 19, 2017 1:22 AM
  • I don't know, Richard ... I'm having a hard time visualizing what your class looks like and what your methods look like and where your DGV is. How can the DGV only be scoped to one method? Isn't this a grid on your Form? It would be scoped to the whole class. In other words, you should be able to access your DGV from any method in the class with this.dataGridView1 (or whatever you've called it).

    Really, a little bit of code goes a long way ... can't you share at least some of it? The relevant bits, obviously.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com


    • Edited by BonnieBMVP Tuesday, December 19, 2017 1:45 AM
    Tuesday, December 19, 2017 1:44 AM
  • Solved.  Updating the values in the table wasn't really the issue, it was loading the data into the DGV after.  I passed an instance of the form1 to form2, then, when the table was updated, use the passed instance of form1 to call the method in form1 to load the data.  
    Friday, February 16, 2018 11:44 PM
  • Hi Richard,

    I don't think you mentioned previously that there was a second Form involved. Well, it works, so that's good. The only problem I see with that, is that the two Forms are tightly-coupled. I would have used an Interface. Take a look at my blog post here:

    https://geek-goddess-bonnie.blogspot.com/2010/06/program-to-interface.html

    In my blog post, I'm filling a ListView, but that could just as easily be a DGV. I think, judging from what you said about your two Forms, your Form1 would implement the IFillFromFinder interface. And your Form2 would still be receiving Form1 as a parameter, but by using an Interface, it's not tightly-coupled to Form1. In other words, you could pass in any Form or class or Control, as long as whatever you passed implemented the IFillFromFinder interface. Read the blog and see if it makes sense to you ...


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, February 17, 2018 12:38 AM