Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Data Grid View, Databound, Add Rows

Avatar of San24
San24 asked on
.NET ProgrammingC#
9 Comments1 Solution3798 ViewsLast Modified:
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?
ASKER CERTIFIED SOLUTION
Avatar of Arabia_vn
Arabia_vnFlag of Viet Nam image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answers