Skip to content

How to Save Datagrid values on Button Click Event

Aamir Khan

Asked Jun 20, 2012, 11:38 PM EDT4 answers
Share
LinkedInX
Dear Friends,
I have a gridview with 2 columns, ID & quantity.
and a Button outside Gridview named 'Save'.
how can i save the values from Gridview(ID & quantity), on button Click event of Save Button
Pls reply.

Regards,
aamir

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    What is the datasource of the grid or its plain textboxes you have placed ?


    You can iterate all the items in the grid and send it to database.
    The database insertion operation may be for each records or you can bulk insert using sqldataadapter also.

    foreach (DataGridItem dgi in gv.Items)  
    {  
        string id = dgi.Cells[0].Text;  
        string name = dgi.Cells[1].Text;  
        SqlConnection conn = new SqlConnection(Common.GetConnectionString("SqlConnectionString"));  
        conn.Open();  
        SqlCommand comm = new SqlCommand("insert into table1 values (" + id + ",'" + qty+ "')", conn);  
        comm.ExecuteNonQuery();  
        conn.Close();  
    }
    0
  • 0
    hi murali, my datasource for gridview is on aspx page and i am using in .cs file like below GridView1.DataSourceID= "dsInvMast; GridView1.DataBind();
    0
  • 0
    On the save button event just put the below it will work.

    foreach (DataGridItem dgi in gv.Items)   
    {   
        string id = dgi.Cells[0].Text;   
        string name = dgi.Cells[1].Text;   
        SqlConnection conn = new SqlConnection(Common.GetConnectionString("SqlConnectionString"));   
        conn.Open();   
        SqlCommand comm = new SqlCommand("insert into table1 values (" + id + ",'" + qty+ "')", conn);   
        comm.ExecuteNonQuery();   
        conn.Close();   
    }
    0
  • 0
    hi murali, the gv.items what is used for, as i am getting that it contains Gridview name and its properties named Items. but on my side the items Properties not been showing in intelisense.
    0