Click here to Skip to main content
15,881,776 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
HI all, i am new to c# , pls guide me.

i have fetch the data in gridview as per below code ,
now i have to edit and update value in gridview and save into database.
how can i achive???
thx in advance allll

C#
private void findlaksajob_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                string sql = "SELECT [Jobcard],[Planqty],[Mouldqty],[Operator],[Supervisor],[Profilename],[Process],[Date],[Remark],[Approvedby],[id],[Division],[Od] FROM [SAPProduction].[ProductionData] WHERE JobCard ='" + findlaksajob.Text.ToString() + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, objConn1);
                DataSet ds = new DataSet();
                objConn1.Open();
                da.Fill(ds, "ProductionData");
                dataGridView2.DataSource = ds;
                dataGridView2.DataMember = "ProductionData";
                objConn1.Close();
                e.Handled = true;
            }

        }
Posted
Updated 30-Nov-12 22:27pm
v4

Same way, go for updating and inserting into database.. :)
Hey go with this, awesome article:
http://www.c-sharpcorner.com/UploadFile/rohatash/insert-edit-update-and-delete-data-with-datagridview-in-wi/[^]
 
Share this answer
 
v2
Comments
Master Vinu 1-Dec-12 4:32am    
krunal, but i have to edit it after fetching data in gridview and update it..
pls advice how to code it
[no name] 1-Dec-12 4:44am    
I have just updated solution, have a look at that :)
Master Vinu 1-Dec-12 5:08am    
@Krunal R :
http://localhost:2268/Service1.svc error while using this url in add reference services
[no name] 1-Dec-12 5:14am    
Lol, You have used that service, thats fine, but why didn't you change the references of it ?
[no name] 1-Dec-12 5:14am    
To consume this service, you need to add service reference in your app..
Hi Dear,

Please see this code.
Here Use Two function
(1) FxInsert
(2) FxUpdate

FxInsert : Description how to insert data in SQL server Database.
FxUpdate : Description how to Update data in SQL server Database.

Function : FxInsert

C#
private void FxInsert()
        {
            for (int i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                string sql = "INSERT INTO [SAPProduction].[ProductionData] ( [Jobcard],[Planqty],[Mouldqty],[Operator],[Supervisor],[Profilename],[Process],[Date],[Remark],[Approvedby],[id],[Division],[Od]) " +
               " 

VALUES('" + dataGridView2.Rows[i].Cells[" Jobcard]"].Value.ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Planqty]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Mouldqty]"].ToString() + "' " +
"'" + dataGridView2.Rows[i].Cells["[Operator]"].ToString() + "'  " +
"'" + dataGridView2.Rows[i].Cells["[Supervisor]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Profilename]"].ToString() + "'" +
" '" + dataGridView2.Rows[i].Cells["[Process]"].ToString() + "'" +
" '" + dataGridView2.Rows[i].Cells["[Date]"].ToString() + "' " +
"  '" + dataGridView2.Rows[i].Cells["[Remark]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Approvedby]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[id]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Division]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Od]"].ToString() + "' ) ";

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
            }
        }


--------------------------------------------------------------

Function : FxUpdate

C#
private void FxUpdate()
       {
           for (int i = 0; i < dataGridView2.RowCount - 1; i++)
           {
               string sql = "UPDATE [SAPProduction].[ProductionData] SET " +

              " [Jobcard]= '" + dataGridView2.Rows[i].Cells["[Jobcard]"].Value.ToString() + "' , " +
              " [Planqty]= '" + dataGridView2.Rows[i].Cells["[Planqty]"].ToString() + "',  " +
              " [Mouldqty] = '" + dataGridView2.Rows[i].Cells["[Mouldqty]"].ToString() + "', " +
              " [Operator] =  '" + dataGridView2.Rows[i].Cells["[Operator]"].ToString() + "', " +
              " [Supervisor] = '" + dataGridView2.Rows[i].Cells["[Supervisor]"].ToString() + "' , " +
              " [Profilename] ='" + dataGridView2.Rows[i].Cells["[Profilename]"].ToString() + "' , " +
              " [Process]='" + dataGridView2.Rows[i].Cells["[Process]"].ToString() + "' , " +
              " [Date]='" + dataGridView2.Rows[i].Cells["[Date]"].ToString() + "' , " +
              " [Remark]='" + dataGridView2.Rows[i].Cells["[Remark]"].ToString() + "' , " +
              " [Approvedby]='" + dataGridView2.Rows[i].Cells["[Approvedby]"].ToString() + "' , " +
              " [id]='" + dataGridView2.Rows[i].Cells["[id]"].ToString() + "' , " +
              " [Division]='" + dataGridView2.Rows[i].Cells["[Division]"].ToString() + "' ";
               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
           }
       }
 
Share this answer
 
v2
Comments
Master Vinu 1-Dec-12 7:11am    
dear aziz , thx for your code but gives error jobcard column not found but its exsist and column id is primary we have to update it on id
Master Vinu 1-Dec-12 7:27am    
i have tried this but not working
private void Update_Click(object sender, EventArgs e)

{
for (int i = 0; i < dataGridView2.RowCount - 1; i++)
{
string sql = "UPDATE [SAPProduction].[ProductionData] SET " +

" Jobcard= '" + dataGridView2.Rows[i].Cells["Jobcard"].ToString() + "' , " +
" Planqty= '" + dataGridView2.Rows[i].Cells["Planqty"].ToString() + "', " +
" Mouldqty = '" + dataGridView2.Rows[i].Cells["Mouldqty"].ToString() + "', " +
" Operator = '" + dataGridView2.Rows[i].Cells["Operator"].ToString() + "', " +
" Supervisor = '" + dataGridView2.Rows[i].Cells["Supervisor"].ToString() + "' , " +
" Profilename ='" + dataGridView2.Rows[i].Cells["Profilename"].ToString() + "' , " +
" Process='" + dataGridView2.Rows[i].Cells["Process"].ToString() + "' , " +
" Date='" + dataGridView2.Rows[i].Cells["Date"].ToString() + "' , " +
" Remark='" + dataGridView2.Rows[i].Cells["Remark"].ToString() + "' , " +
" Approvedby='" + dataGridView2.Rows[i].Cells["Approvedby"].ToString() + "' , " +
" id='" + dataGridView2.Rows[i].Cells["id"].ToString() + "' , " + " Division='" + dataGridView2.Rows[i].Cells["Division"].ToString() + "' , " + " Od='" + dataGridView2.Rows[i].Cells["Od"].ToString() + "'";

SqlCommand cmd = new SqlCommand(sql, objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
}
Hi dear,

Check this code.
C#
private void Update_Click(object sender, EventArgs e) 
{
 string sql = "UPDATE [SAPProduction].[ProductionData] SET " +                  
                    " Planqty= '" + dataGridView2.Rows[i].Cells["Planqty"].ToString() + "', " + 
                    " Mouldqty = '" + dataGridView2.Rows[i].Cells["Mouldqty"].ToString() + "', " + 
                    " Operator = '" + dataGridView2.Rows[i].Cells["Operator"].ToString() + "', " + 
                    " Supervisor = '" + dataGridView2.Rows[i].Cells["Supervisor"].ToString() + "' , " + 
                    " Profilename ='" + dataGridView2.Rows[i].Cells["Profilename"].ToString() + "' , " + 
                    " Process='" + dataGridView2.Rows[i].Cells["Process"].ToString() + "' , " + 
                    " Date='" + dataGridView2.Rows[i].Cells["Date"].ToString() + "' , " + 
                    " Remark='" + dataGridView2.Rows[i].Cells["Remark"].ToString() + "' , " + 
                    " Approvedby='" + dataGridView2.Rows[i].Cells["Approvedby"].ToString() + "' , " + 
                    " id='" + dataGridView2.Rows[i].Cells["id"].ToString() + "' , " + 
                    " Division='" + dataGridView2.Rows[i].Cells["Division"].ToString() + "' , " + 
                    " Od='" + dataGridView2.Rows[i].Cells["Od"].ToString() + "' "+
                    " Where Jobcard = '" + dataGridView2.CurrentRow.Cells["Jobcard"].Value.ToString() + "' "; // Jobcard = your desire Rows Jobcard Id

                SqlCommand cmd = new SqlCommand(sql, objConn1); 
                objConn1.Open(); 
                cmd.ExecuteNonQuery(); 
                objConn1.Close();
}
 
Share this answer
 
v2
Comments
Master Vinu 3-Dec-12 2:59am    
need code for delete row through datagrid
Only u add a button and type update on it's text property
updatebtn_click event

da.update(ds);
messagebox.show("Update successfully");



where da=SqlDataAdapter and ds=DataSet

Thanks & regard
Sham:)
 
Share this answer
 
v2
Comments
Master Vinu 3-Dec-12 1:53am    
shamboo pls do the changes in code
Hi dear,

C#
private void FxDelete()
{
    string sql = "DELETE FROM [SAPProduction].[ProductionData] WHERE  [id] = '" + dataGridView2.Rows[i].Cells["[id]"].Value.ToString() + "' ";
    SqlCommand cmd = new SqlCommand(sql, objConn1);
    objConn1.Open();
    cmd.ExecuteNonQuery();
    objConn1.Close();
}


If you like this solution plz vote.
 
Share this answer
 

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