Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to update data from DataGridView with one column to database with two columns?

$
0
0
   

I'm trying to update data from `DataGridView` to my database. While I was looking for the solution of this problem on google, I noticed that all of the solutions are managed by using class variables (for `DataTable`,`SqlDataAdapter`,...). I'm trying to do this just by using function variables.

This is how I loaded data to `DataGridView`:

private void LoadDataGridView(int ID) 
{        try        {             SqlConnection connection = new SqlConnection(connString);             SqlCommand cmd = new SqlCommand("SELECT SENTENCE FROM Sentences WHERE CategoryID = @ID",connection);             cmd.Parameters.Add("@ID",SqlDbType.Int).Value = ID;          DataTable dataTable = new DataTable();            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);               dataAdapter.Fill(dataTable);             DataGridView.DataSource = dataTable;        }            catch (Exception)        {             
             MessageBox.Show("ERROR WHILE CONNECTING TO DATABASE!");        }
}



`DataGridView` is showing just those sentences that match particular ID. The user enters new sentence to dataGridView and I need to store them and all changes made to sentences already shown in dataGridView to my database. All these sentences should have CategoryID, which user previously entered, in database table. So can you show me how to update all changes made in dataGridView to my database? Please help


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>