2 Replies - 976 Views - Last Post: 20 February 2012 - 10:20 AM Rate Topic: -----

#1 urwithmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-September 10

Funny nature of sqlAdapter.Update and DataGridView

Posted 19 February 2012 - 12:08 PM

Attached File  datagridview.bmp (853.81K)
Number of downloads: 43

Hi guys,
I was learning how to create Windows Forms application in which I get data from a table into a DataGridView, update a row over there and reflect the changes back to the database.

I created a demo table Student in SqlServer Express.


studentId int - primary key (auto increment)
studentName varchar(50)


Then i filled the DataGridView using the following code

private void btnLoad_Click(object sender, EventArgs e)
       {

           try
           {
               sqlAdapter = new SqlDataAdapter("SELECT *, 'Update' AS [Delete] FROM Student", sqlCon);
               sqlCommand = new SqlCommandBuilder(sqlAdapter);

               sqlAdapter.UpdateCommand = sqlCommand.GetUpdateCommand();

               dataset = new DataSet();
               sqlAdapter.Fill(dataset, "Student");
               dataGridStudents.DataSource = dataset.Tables["Student"];

               for (int i = 0; i < dataGridStudents.Rows.Count; i++)
               {
                   DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
                   dataGridStudents[2, i] = linkCell;
               }
           }
           catch (Exception ex) 
           {
               MessageBox.Show(ex.Message);
           }
       }



Thus the DataGridView had three columns

****************************************
studentId , studentName , Update (DataGridViewLinkCell)

****************************************


Update column is a column of DataGridViewLinkCells.

To update the database i used to following code on the click of a DataGridViewLinkCell


private void dataGridStudents_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           try
           {
               if (e.ColumnIndex == 2)
               {
                  string Task =
                  dataGridStudents.Rows[e.RowIndex].Cells[2].Value.ToString();
                  if (Task == "Update")
                   {

                       int r = e.RowIndex;
                       MessageBox.Show(r.ToString());
                       dataset.Tables["Student"].Rows[r]["studentName"] =
                       dataGridStudents.Rows[r].Cells["studentName"].Value;
                       sqlAdapter.Update(dataset, "Student");  
                   
                   }
                              
               }
           }
           catch (Exception ex) {
               MessageBox.Show(ex.Message);
           }
       }




Now comes the funny part (as per me).
Whenever I update a studentName in any row and click on the Update link in the Update column of the same row, then the data is not reflected in the database.
However, if i click the Update link on any other row (other than the updated row), the data gets reflected to the database.
Also if I update any other row other along the with the desired row and then click on Update link in the desired row, then also the changes are reflected.

I wan't to know, what is the reason of this strange behaviour ?

The code I have taken is from noorahmad's post Insert, Update & Delete Table from DataGridView in C#.Net.
http://www.dreaminco...iew-using-c%23/

I could have posted the question there but it's a pretty old post, thought would get answers by a new post.

Hoping to get my concepts cleared.

Thanks and regards
mit .
ps : check the attachment

Is This A Good Question/Topic? 0
  • +

Replies To: Funny nature of sqlAdapter.Update and DataGridView

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,571
  • Joined: 12-June 08

Re: Funny nature of sqlAdapter.Update and DataGridView

Posted 19 February 2012 - 12:40 PM

It sounds like your row, the one you want to update, is still in edit mode.. you need to end edit on the cells/row so the grid can update the dataset..
Was This Post Helpful? 0
  • +
  • -

#3 urwithmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-September 10

Re: Funny nature of sqlAdapter.Update and DataGridView

Posted 20 February 2012 - 10:20 AM

Hi modi thanks for the reply ,

I tried reading some content on EditMode but did not understand it completely.

I tried calling
dataGridStudents.EndEdit() ; before calling the Update() method. But it made no difference.

However in my case the DataGridView is updating the dataset. I printed the dataset contents.

Thanks,
mit
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1