Data Grid View, Databound, Add Rows

San24
San24 used Ask the Experts™
on
Experts,

I`m using VS 2008, Win Forms, C# 3.5

How do I go about adding rows to a Data Grid View that is databound with a List. Let me simplify and explain what I`m trying to do.

I have a User Control which consists of a DataGridView and few other controls. And I want to add and delete rows in the Data Grid View. When I add rows, I want a blank row to be inserted and the user can type in some values at a particular index.

In the main form I have an event which loads the data into the DataGridView

            CustUserCntrl CX = new CustUserCntrl();  //Contains the Data Grid View
            BindingSource BS = new BindingSource();

            BS.DataSource = CreateDummList();         //This returns a List of objects of some class.
            CX.MotGridView.DataSource = BS;

I saw a few example of adding rows programtically in a Data Grid View by using Data Sets. But I`m using Lists here.

In the User Control Code [CustUserCntrl ] I have something like this -

private void AddMotRow()
{
// What goes in here?
}

//Deleting rows works
private void DeleteMotRow()
        {
            if (this.MotGridView.SelectedRows.Count > 0 && this.MotGridView.SelectedRows[0].Index != this.MotGridView.Rows.Count - 1)
            {
                this.MotGridView.Rows.RemoveAt(this.MotGridView.SelectedRows[0].Index);
            }
        }

Also, is there a way to copy and paste rows in the DataGridView?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Luckty85,

I still have the same problem "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound" if I`m using List and Data Source.
"Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound"--> i think it is already way too clear . In order to add a new row programmatically we have to create a new object (with type of your list item), add it to your CreateDummList() and do binding again. So better write a function to refresh view like this

void RefreshView()
{
  BindingSource BS = new BindingSource();
  BS.DataSource = YourList();        
  CX.MotGridView.DataSource = BS;
}

Then you want a blank row to be inserted and the user can type in some values at a particular index --> Yes you can do that as long as you set DataGridView.AllowUserToAddRows = true. Remember the data you put there actually haven't been added to your list yet (you can check by calling RefeshView()). To do this you use DataBoundItem, for e.x i make a "Save" button to save new data

private void btnSave_Click(object sender, EventArgs e)
{
 foreach (DataGridViewRow r in MotGridView.Rows)
{
YourListItemType item=(YourListItemType)r.DataBoundItem;
if ( !YourList.Contains(item) ) YourList.Add(item); // ---> i am not so sure about the syntax cause i don't have .net right now but i should be something similar
RefreshView(); //--->Do binding data to GridView again
}
}

Well i feel this is not an elegant way to do the job but i know it works. I'll be very appreciate if someone posts a better solution.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

Commented:
Arabia_vn,

I could be completely off track here, the User Control File [which contains the DataGridView] will not have access to the DataSource, List. Or can it? All the creating and the binding of the list is done on the Main Form, so only the instances of the User Control on the MainForm will have access to the DataSource, List.

I`m confused. A example by anyone will be greatly appreciated.

It should have access to the DataSource, if not, there's no way you can do that. I assume that you get the DataSource from a database. If that's the case, instead of adding new item to yourlist you have to CREATE(UPDATE) new data onto the database since you already have new data (YourListItemType item=(YourListItemType)r.DataBoundItem;). The rest is simply making connection to the database to UPDATE new data and refreshing the view by binding again.

Author

Commented:
I`m populating the Data from a List Object. Using a database would have made it a lot easier. The List is what is confusing me.
Then please post the code here, it would be much more easier. I also bring home my office laptop which has .NET 3.5 installed and now i want to see real code.

Author

Commented:
Arabia,

I figured this out.  To insert a row, I do this :

            XYZ Item = new XYZ();
            BindingSource BSA = (BindingSource)this.GridView.DataSource;
            BSA.Insert(Index, Item);

Author

Commented:
User solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial