You must Sign In to post a response.
  • Category: ASP.NET

    Need detail about gridview

    Dear Friends
    In my project i have one gridview with add button and input control text box ,
    when i was click that add button my all put should be saved in datatable and that datatable value should display in gridview.. these all operation fire only with out database..

    when i was click "Save" button the gridview values are store into database. can any one guide..
  • #764613
    Hi,
    Try this:
    protected void btnAdd_Click(object sender, EventArgs e)
    {
    DataTable dtTable = new DataTable();
    DataRow drRow;
    dtTable.Columns.Add(new System.Data.DataColumn("Col1", typeof(String)));
    drRow = dtTable.NewRow();
    drRow[0] = txtYourInputTextBox.Text;
    dtTable.Rows.Add(drRow);
    GridView1.DataSource = dtTable;
    GridView1.DataBind();
    drRow = null;
    dtTable = null;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
    foreach (GridViewRow gvRow in GridView1.Rows)
    {
    string szData = gvRow.Cells[0].ToString();
    //... Now insert szData in DB ...
    }
    }
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #764616
    Hi,

    If you want to save the gridview row information based on click, then use OnRowCommand event and save the particular row inforamtion into database.

    EX:

    protected void gv_OnRowCommand(object sender,GridviewRowCommandEventArgs e)
    {
    if(e.CommandName=="Save")
    {
    //save data into database.
    }
    }


    If you want to save all the records into database then onclick event of button loop through all rows of gridview and save it into database.


    protected void btnsave_click(object sender, EventArgs e)
    {
    foreach(GridviewRow row in gv.Rows)
    {
    //save each row into database...
    }
    }


    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764623
    Hi navaneethakrishnan.

    If you want the details to be shown after adding the records inline then bind the gridview with datatable.

    With in the add_btn_click event you can make the datasource as datatable and bind the gridview like below

    DataTable dt=new DataTable();
    GridView1.DataSource = dt;

    With in the save_btn_click you can save those records to the database.

    Sridhar Thota.
    Editor: DNS Forum.

  • #764656
    Below articles will help you find sample code in this regard,

    http://stackoverflow.com/questions/10556556/insert-all-data-of-a-datagridview-to-database-at-once

    http://stackoverflow.com/questions/22727374/saving-multiple-data-grid-view-value-to-database


    Regards,
    Asheej T K
    Microsoft MVP[ASP.NET/IIS]
    DotNetSpider MVM


Sign In to post your comments