Compartilhar via


How do I edit tabular data/data in a DataGridView in a details form?

Abstract
This sample shows how to edit data from a DataGridView in a details form which opens when one double clicks on the RowHeader of the DataGridView. Since both forms are bound to the same datasource one sees an instant update on both forms when the user decides to save the data and additional roundtrips to the database are avoided. It also provides a brief introduction to DataBinding in .NET.
EditDetailsForm.png

Source Code
Visual Basic | C#

Additional Resources
Connecting to Data in Visual Studio
Getting Started with Data Access
Windows Forms Data Binding
Forms over Data Video Series

Prerequisites
The examples were created using Visual Basic 2008 Express (free download)/Visual C# 2008 Express (free download) but do also apply to earlier versions of Visual Studio (with minor modifications).

Enjoy!

   Daniel

Comments

  • Anonymous
    September 17, 2009
    It is look very stupid to create edit form static – I have many grid views bind to different tables. Is it possible to provide a sample code how to create controls on edit form dynamically based on the number of columns and they types?

  • Anonymous
    October 03, 2009
    Sorry,I got nothing, though this is very use ful for me in my VB-6 application to keep record of my class students and to edit on screen there Marks or Roll NO etc

  • Anonymous
    October 03, 2009
    The comment has been removed

  • Anonymous
    October 13, 2009
    @Peter >> I can post a bit of code when I find the time. Basically what you'd have to do is to walk over the datatable in the code of the details form, add the controls dynamically to the form and bind them to the datasource. Does this help? @lohani >> I just downloaded the source code again and it worked for me. Which problems do you have exactly?

  • Anonymous
    October 31, 2009
    If I change the value, but don't click save,just close the window. The values in the Binding changed.

  • Anonymous
    November 01, 2009
    Yes, Daniel. I succeeded to create dynamically the edit form - it was little tricky with Image fields and  Form size - should recalculate form layout. If you want I will provide a source code (C# 2.0)?

  • Anonymous
    November 10, 2009
    @Peter: Sure, thanks for the offer! Glad you made it! If you want I can upload your source code to http://code.msdn.microsoft.com/EditDataInDetailForm">http://code.msdn.microsoft.com/EditDataInDetailForm and give you credit or you could upload it to http://code.msdn.microsoft.com yourself and I reference it from there. Whatever you prefer is fine with me! @lexxkrt: You're right. I'll update the code when I find a bit of time. Sorry for the inconvenience!