Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save automatically datagridview row after that grid fully filled by user.
Already binded that gridview with my sql database file.

What I have tried:

For that I wrote one code in datagridview_RowLeave event. but error is coming.
here is my code
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
       {
           if (e.ColumnIndex == 5)
           {

               con.Open();
               SqlCommand insert = new SqlCommand("insert into invtempdb (fullname, quantity, itmbyp, itmprimary, itmtotal) values(@a,@b,@c,@d,@e)", con);

               insert.Parameters.Add("@a", dataGridView1.CurrentRow.Cells[1]);
               insert.Parameters.Add("@b", dataGridView1.CurrentRow.Cells[2]);
               insert.Parameters.Add("@c", dataGridView1.CurrentRow.Cells[3]);
               insert.Parameters.Add("@d", dataGridView1.CurrentRow.Cells[4]);
               insert.Parameters.Add("@e", dataGridView1.CurrentRow.Cells[5]);
               insert.ExecuteNonQuery();
               //con.Close();

               insert.ExecuteNonQuery();

               MessageBox.Show("value saved");
           }
           else
           {
               MessageBox.Show("Not Saved");
           }
       }
Posted
Updated 5-Apr-17 23:39pm
v6
Comments
Karthik_Mahalingam 6-Apr-17 5:12am    
what is the error?
vijay_bale 6-Apr-17 5:21am    
No mapping exist from object type System.Windows.Forms.DataGridviewTextboxCell to a known managed provider native type
Karthik_Mahalingam 6-Apr-17 5:36am    
you are using textbox cell, you will have to cast it to textbox cell type
vijay_bale 6-Apr-17 5:41am    
so I have to convert all datagridcells while I am saving. Is that right
Karthik_Mahalingam 6-Apr-17 5:47am    
depends on the cell type you will have to cast, if it is textboxcell then cast it as DataGridViewTextBoxCell else like the way you have posted.

1 solution

try like this to read value from DataGridViewTextBoxCell

string value = Convert.ToString((dataGridView1.Rows[e.RowIndex].Cells[1] as DataGridViewTextBoxCell).Value);
 
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