0

I want to add a new item into my datagridview. I know can achieve this using several TextBoxes and a Button. When the user clicks this button, it will add a new item into database and refresh my datagridview.

I wonder if I can use the last row of my datagridview to add a new item. How can I do that?

Marijn
  • 10,367
  • 5
  • 59
  • 80
user609511
  • 4,091
  • 12
  • 54
  • 86

2 Answers2

2

did you use the RAD-Databind (DataSource on your winforms)? If so it might be a bit tricky. You should load the data in a seperate layer (not in the code-behind if you can help it). Then it should be rather simple to add another row into your local data.

As you asked: here is a very simple sample. I use same generic data:

public struct SimpleData
{
    public int Id { get; set; }
    public string Text { get; set; }
}

And initialize a DataGridView named GridView via:

        var bindingSource = new BindingSource();
        bindingSource.Add(new SimpleData {Id = 1, Text = "Hello"});
        bindingSource.Add(new SimpleData {Id = 2, Text = "World"});
        GridView.DataSource = bindingSource;

then you can add another row simply by

        var data = GridView.DataSource as BindingSource;
        data.Add(new SimpleData{Id=3, Text="!!! added"});

In a real world example I would use some well known pattern: MVVM for Winforms or MVP for Winforms do seperate the View from the logic. But after all the important bit is using some kind of Bindingsource (that informs the Grid that data has changed) or reassing the changed data to the DataSource if you choose to use a shallow/simple datacontainer like List or similar.

Community
  • 1
  • 1
Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • BTW: in my code I the logic "knows" the view - normaly you don't want this so just read it as a demo. In the *MVVM-pattern* mentioned above you would expose the BindingSource in your ViewModel and the view (maybe in your code-behind) would bind to this. – Random Dev Aug 17 '11 at 12:46
  • Thanks you for your response. i forgot to tell you that i use 3Layer. DAL(INSERT INTO...UPDATE ...). BAL, and View. How can i retrive the value of each NEW row or CHANGE row to connect it with my DAL ? – user609511 Aug 17 '11 at 13:19
  • this is getting even more implementation-specific. In a system where you use some kind of Repository-Pattern for your data-access you might take *all the data* (table) as a object and check for changes in the DAL - or you might query the *rows* of your data - in this case all you'd have to do is adding a call to you DAL inside the ViewModel that does the updates. Or the ViewModel might be responsible for keeping track of changes. It's a matter of concrete case and what you prefere. – Random Dev Aug 17 '11 at 13:55
1

Or,

You could bind your DataGridView to a Generic.List of WrapperObject (or whatever you want to call the class), where each instance of WrapperObject can either represent one of the records already in the database and expose its properties, or represent a 'blank record'.

In the EditingControlShowing event handler for the DataGridView, you can determine whether the user is typing over the last row (as it is the only row which contains the blank part) and do whatever you need to do to add a part to the database.

When you want to update the contents on the DataGridView, simply re-create a new list of WrapperObjects from the records you want to display, and add one to represent the blank record at the end, so it will show up at the bottom, then set the DataGridView's datasource to the new list.

andyhasit
  • 14,137
  • 7
  • 49
  • 51