Click here to Skip to main content
16,012,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, how do I save changes been made on datagridview. Bellow is the code have I have so far.

C#
private void refreshInvoice() //Load all Invoice result.
{
    SqlDataAdapter dataAdapter = new SqlDataAdapter("select ID_Invoice, Description_Invoice, Amount_Invoice, Invoice_Date_Invoice, Invoice_Note, Invoice_Number_Invoice, Invoice_Status, Paid_Invoice_Number, Supplier_Invoice, Upload_Date_Invoice  from StoringSystemInvoiceTable", connectionString);
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
    //new data table
    DataTable table = new DataTable();
    table.Locale = CultureInfo.InvariantCulture;
    dataAdapter.Fill(table);
    storingSystemContractTableBindingSource.DataSource = table;
    //resize the DataGridView columns to fit newly loaded content.
    storingSystemContractTableDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);

    //user can resize column and add rows
    storingSystemContractTableDataGridView.AllowUserToResizeColumns = true;
    storingSystemContractTableDataGridView.AllowUserToAddRows = true;
    //storingSystemContractTableBindingNavigator.BindingSource();
    storingSystemContractTableDataGridView.DataSource = storingSystemContractTableBindingSource;
}
Posted
Comments
Herman<T>.Instance 23-Oct-12 3:38am    
this is a seelct statement not an insert. I guess wrong part of code?

Hi

Step - 1 Take Dataset
Step - 2 Fill Dataset using appropriate select statment
Step - 3 Assgin dataset to Gridview(AllowAddNewRow Property to True)

Update Existing Data
if you update existing data then use Update() method of sqladapter object.

Insert new data
First Add new Record in datatable then Call Method (UPDATE())
 
Share this answer
 
Hi Initially you will get the by using below code


DataSet ds = new DataSet();

    static SqlConnection con = new SqlConnection("user id=sa;password=just4fun;database=sdp;data source=sitt244");

    static SqlCommand cmd = new SqlCommand("select * from employee ", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

C#
private void GetData()
       {
           dataGridView1.Visible = true;
           da.Fill(ds,"e");
           dataGridView1.DataSource = ds.Tables["e"];
           MessageBox.Show("You got the data");
       }


       private void button1_Click(object sender, EventArgs e)
       {
           GetData();
           
       }


If you want to insert data or save changes then write the below code in "Savechanges" button click
C#
SqlCommandBuilder cmd = new SqlCommandBuilder(da);
           try
           {
               da.Update(ds, "e");
               MessageBox.Show("Saved");


           }
           catch (Exception ee)
           {
               MessageBox.Show(ee.Message);
           }
 
Share this answer
 
Comments
mrjohnk 23-Oct-12 7:29am    
I'm using DataTable is that different from DataSet?
Samsani.v.s.Durga Prasad 23-Oct-12 7:31am    
Replace dataset with datatable,it will work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900