datagridview

Hi,
Iam using datagridview,How to add a new row to the datagrid view
In insert button i wrote this code,
dataGridView1.Rows.Add(3);
when i run this code,it is giving the following exception

exception: rows cannot be programatically to the datagridview's collection when control is databound

In save button i wrote like this,

private void button1_Click(object sender, EventArgs e)

{da.Update(ds,"emp");

}

It is giving the following exception:

Update requires a valid InsertCommand when passed DataRow collection with new rows.


Please help me.
Thanks In Advance




Answer this question

datagridview

  • JonathanMcCracken

    Hi m8...

    Well looking at your sample i presume you wanna add 3 rows to the DataGridView...

    it's done like this:

    dataGridView1.Rows.Add("value for column1", "value for column2", "blabla");

    and then continue to add them as long as you want...

    to add rows from a Database you have to do the complete database request on the button's event...



  • CRChambers

    you have databounded DataGRidView. in order to add row you should add row to datatable (or IList) you have bounded.

    if you have declared

    DataTable table;

    //this is code to bind datagridview

    dataGridView1.DataSource = table;

    //to add row to grid you have to add row to table

    table.Rows.Add(...your row...)

    hope this helps



  • 808numbers

    hi,

    Please go through the link below.
    Link: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=420784&SiteID=1

    Thank you,
    Bhanu.



  • datagridview