Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 
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 30-Nov-12 22:24pm
Edited 30-Nov-12 22:27pm
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Same way, go for updating and inserting into database.. Smile | :)
Hey go with this, awesome article:
http://www.c-sharpcorner.com/UploadFile/rohatash/insert-edit-update-and-delete-data-with-datagridview-in-wi/[^]
  Permalink  
v2
Comments
vinayak chavan at 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
Krunal R at 1-Dec-12 4:44am
   
I have just updated solution, have a look at that :)
vinayak chavan at 1-Dec-12 5:08am
   
@Krunal R :
http://localhost:2268/Service1.svc error while using this url in add reference services
Krunal Rohit at 1-Dec-12 5:14am
   
Lol, You have used that service, thats fine, but why didn't you change the references of it ?
Krunal Rohit at 1-Dec-12 5:14am
   
To consume this service, you need to add service reference in your app..
vinayak chavan at 1-Dec-12 5:17am
   
ya while add this url in my application i got error that its link is not valid
Krunal Rohit at 1-Dec-12 5:21am
   
Could you please mail me your code ?
vinayak chavan at 1-Dec-12 5:24am
   
i have already posted it sir
Krunal Rohit at 1-Dec-12 5:26am
   
entire project..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
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
 
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();
           }
       }
  Permalink  
v2
Comments
vinayak chavan at 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
vinayak chavan at 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();
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi dear,
 
Check this code.
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();
}
  Permalink  
v2
Comments
vinayak chavan at 3-Dec-12 2:59am
   
need code for delete row through datagrid
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
ShamSmile | :)
  Permalink  
v2
Comments
vinayak chavan at 3-Dec-12 1:53am
   
shamboo pls do the changes in code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi dear,
 
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,651
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,542


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100