简体   繁体   中英

How to add rows and insert data in datagridview C#

Can someone help me ?

i want datagridview display data

datagridview1.datasource = _db.Students.Tolist();

and it display on data grid. and has a button.if the button click, datagridview1 will add the rows with no data. And the User fill the rows. when it done it will save to database.

How to make it work ?

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "createNewRecord")
    {
             //Another Gridview will be created here, and it will contains EditTemplate.
             //you can use that gridview to receive data by Edit.
    }
}

Sample of Edit tamplate gridview

<Columns>
    <asp:TemplateField>
    <EditItemTemplate><asp:textboxt id="txt1" runat="server" /></EditItemTemplate>
    </asp:TemplateField>
    </Columns>

If I remember correctly, DataGridView has a .Rows.Add property, like this:

dataGridView.Rows.Add  

and

dataGridView.Rows.Insert

You should convert your data into an array first.
Take a look here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM