[.NET 2.0+] Update Grid Row in Dialogue-VBForums
Results 1 to 1 of 1

Thread: [.NET 2.0+] Update Grid Row in Dialogue

  1. #1

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,603

    [.NET 2.0+] Update Grid Row in Dialogue

    VB version here.

    A lot of people ask how to update a record from a bound grid using a dialogue. The process is so easy it's laughable, but most people don't realise because they don't understand how powerful data-binding is. Following are instructions on how to update a row from a DataGridView bound to a DataTable using a dialogue. I've also attached a working sample but I strongly suggest that you follow the instructions and create your own project to get a better feel for it.

    1. Create a new Windows Forms project.
    2. Add a DataGridView and a BindingSource to the form.
    3. Double-click the title bar of the form to create a Load event handler and add the following code:
    CSharp Code:
    1. DataTable dt = new DataTable();
    2.  
    3. dt.Columns.Add("ID", typeof(int)).AutoIncrement = true;
    4. dt.Columns.Add("Name", typeof(string));
    5.  
    6. dt.Rows.Add(null, "Jane");
    7. dt.Rows.Add(null, "John");
    8. dt.Rows.Add(null, "Jack");
    9.  
    10. this.bindingSource1.DataSource = dt;
    11. this.dataGridView1.DataSource = this.bindingSource1;
    Now run the project and observe the data you added to the DataTable displayed in the bound grid via the BindingSource.

    4. Add a new form to the project.
    5. Add a Label to the form and set its Text property to "ID:".
    6. Add a TextBox to the form, set its Name property to "idText" and its ReadOnly property to True.
    7. Add a Label to the form and set its Text property to "Name:".
    8. Add a TextBox to the form and set its Name property to "nameText".
    9. Press F7 to open the code window.
    10. Add a class level variable like so:
    CSharp Code:
    1. private DataRow data;
    11. Change the constructor from the default to the following:
    VB.NET Code:
    1. public Form2(DataRow data)
    2. {
    3.     InitializeComponent();
    4.  
    5.     this.data = data;
    6.     this.idText.Text = data["ID"].ToString();
    7.     this.nameText.Text = (string)data["Name"];
    8. }
    That means that to create an instance of this form you need to supply a DataRow, the ID and Name fields of which will be displayed in the TextBoxes.
    12. Go back to the first form's design window.
    13. Select dataGridView1, open the Properties window, click the Events button and double-click the CellDoubleClick event. That will create an empty handler for the grid's CellDoubleClick event.
    14. Add the following code to the empty event handler:
    CSharp Code:
    1. DataRow row = ((DataRowView)this.bindingSource1.Current).Row;
    2.  
    3. using (Form2 dialogue = new Form2(row))
    4. {
    5.     dialogue.ShowDialog();
    6. }
    Now run the project and double-click any cell in the grid and observe the data for that record displayed in a dialogue. Note that you can click the column headers to sort the data and double-clicking will still always display the correct data. That's one of the advantages of using a BindingSource.

    15. Go back to the design window for Form2.
    16. Add a Button, set its Name property to "okButton", its Text property to "OK" and its DialogResult property to OK.
    17. Add a Button, set its Name property to "cancelOperationButton" and its Text property to "Cancel".
    18. Select the okButton for the form's AcceptButton property.
    19. Select the cancelOperationButton for the form's CancelButton property.
    20. Double-click the okButton to create an empty Click event handler.
    21. Add the following code to the empty event handler:
    VB.NET Code:
    1. this.data["Name"] = this.nameText.Text;
    That's it. You've finished the whole thing. Now you can run the project and double click a record to open it for editing in the dialogue. You can change the value of the Name property and press the OK button and observe the grid update automatically. Note also that if you edit the name field in the dialogue and press Cancel no changes are made to the grid. Finally, note that you can also add new rows and then edit them the same way.
    Attached Files Attached Files

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.