Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have done the following code for matching record from database and show in datagrid but,how to save the result shown in datagrid in the database table.
C#
private void button4_Click(object sender, EventArgs e)
            {
              
                if (txtcombo.Text == "Form ID")
                {                  
                    label1.Hide();
                    Global.Open_DataConn("doc", "conData");
                    OleDbDataAdapter da = new OleDbDataAdapter("SELECT WCDCH.formid, WCDCH.surname, WCDCH.firstname, WCDCH.midname, WCDCH.ttransid FROM WCDCH INNER JOIN bankdata ON WCDCH.formid = bankdata.formid", Global.conData);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                    label2.Show();
                    label4.Visible = true;          
                    label3.Text = ds.Tables[0].Rows.Count.ToString(); 
                    dataGridView1.Visible = true;      
                                       
                }
                else if (txtcombo.Text == "TransactionID")
                {                  
                    label2.Hide();
                    Global.Open_DataConn("doc", "conData");
                    OleDbDataAdapter da = new OleDbDataAdapter("SELECT WCDCH.formid, WCDCH.surname, WCDCH.firstname, WCDCH.midname, WCDCH.ttransid FROM WCDCH INNER JOIN bankdata ON WCDCH.ttransid = bankdata.ttransid", Global.conData);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                    label1.Show();
                    label4.Visible = true;               
                    label3.Text = ds.Tables[0].Rows.Count.ToString();                
                    dataGridView1.Visible = true;
                }
                else
                {
                    MessageBox.Show("Please,Select option which you want");
                }
          
            }                         
Posted
Updated 16-Jun-12 2:15am
v3
Comments
Shahin Khorshidnia 16-Jun-12 8:16am    
Please: 1. Send the code in a right format. 2. Do not type the question in the title.

1 solution

Hello Amit

For Saving Data in database table
Use insert query in which values would be from DataGridView Cells .


Use foreach loop to iterate on DataGridView Rows .


like :
C#
 int formId = 0 ;
string surname = "" ;
  foreach( DataGridViewRow row in yourdatagridviewName.rows)
{

   formId = Convert.ToIn32(row.Cells["Column Name / index ").Value) ;
   surname = (row.Cells["Column Name / Index ").Value).ToString();
   
   sqlquery = Insert InTo TableName (Id , Name) Values ( formId , surname) ;
   
   other function of database insert command -------- Connection obj , sql command , executeNonquery / etc .
  
   

}

this foreach loop will iterate through each row of your datagridview and will fatch data from cell to variables and then you can use them to save in Insert Query .
Hope you will do your work .

one thing more that you can get this type of solution by putting some efforts in googling .
 
Share this answer
 
v2

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