Click here to Skip to main content
12,510,189 members (54,665 online)
Rate this:
 
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
Updated 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.. :)
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 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 1-Dec-12 4:44am
   
I have just updated solution, have a look at that :)
vinayak chavan 1-Dec-12 5:08am
   
@Krunal R :
http://localhost:2268/Service1.svc error while using this url in add reference services
Krunal Rohit 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 1-Dec-12 5:14am
   
To consume this service, you need to add service reference in your app..
vinayak chavan 1-Dec-12 5:17am
   
ya while add this url in my application i got error that its link is not valid
Krunal Rohit 1-Dec-12 5:21am
   
Could you please mail me your code ?
vinayak chavan 1-Dec-12 5:24am
   
i have already posted it sir
Krunal Rohit 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 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 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 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
Sham:)
  Permalink  
v2
Comments
vinayak chavan 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2016
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