C# Winform DataGridView inline add New row?

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.

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 = "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.

Thanks you for your response. Do you have examle code? – user609511 Aug 17 at 12:21 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.

– Carsten König Aug 17 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 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. – Carsten König Aug 17 at 13:55.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions