Hello all,
When I'm trying to update a selected row, only the first row in the table shows the update no matter where I am at in the table.
The selected row to update doesn't change instead the update is shown allways in the first row.
I've noticed that when I press my "move next" or "move previous" buttons, I can scroll up and down my TABLE, but the "ID" number column which is the identity column not being updated accordingly.
I think my problem is in the scrolling through the table with the buttons.
How can I synchronize my scrolling through the table with updating the "ID" number????
private void UpdateBtn_Click(object sender, EventArgs e) { int x;
WorldDA.UpdateCommand = new SqlCommand("UPDATE RaceTbl SET RaceName=@RaceName, Adulthood=@Adulthood, MiddleAge=@MiddleAge, OldAge=@OldAge, MaxAge=@MaxAge, Vision=@Vision WHERE ID=@ID", WorldConn); WorldDA.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = RaceDGView.CurrentRow.Cells["ID"].Value; WorldDA.UpdateCommand.Parameters.Add("@RaceName", SqlDbType.NChar).Value = raceNameTxt.Text; WorldDA.UpdateCommand.Parameters.Add("@Adulthood", SqlDbType.NChar).Value = AdulthoodTxt.Text; WorldDA.UpdateCommand.Parameters.Add("@MiddleAge", SqlDbType.NChar).Value = MiddleAgeTxt.Text; WorldDA.UpdateCommand.Parameters.Add("@OldAge", SqlDbType.NChar).Value = OldAgeTxt.Text; WorldDA.UpdateCommand.Parameters.Add("@MaxAge", SqlDbType.NChar).Value = MaxAgeTxt.Text; WorldDA.UpdateCommand.Parameters.Add("@Vision", SqlDbType.NChar).Value = VisionCBox.Text; WorldConn.Open(); x = WorldDA.UpdateCommand.ExecuteNonQuery(); WorldConn.Close(); if (x >= 1) MessageBox.Show("recored updated"); dgRefresh(); } private void dgRefresh() { RaceDGView.ClearSelection(); RaceDGView.Rows[RaceBS.Position].Selected = true; } private void MoveNextBtn_Click(object sender, EventArgs e) { RaceBS.MoveNext(); dgRefresh(); } private void MovePreviousBtn_Click(object sender, EventArgs e) { RaceBS.MovePrevious(); dgRefresh(); }
|
Rakesh JhaPosted Sep 30, 2010, 9:10 AM
Actually you are always refresh your datagridview, because of that it always update your first row of table. you should use following command
WorldDA.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = this.RaceDGView["ID", RaceDGView.SelectedCells[0].RowIndex].Value;
LiorPosted Sep 30, 2010, 10:46 AM
That helped!!!