Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all,

I have a datagrid binding to a datatable, and this datagrid has checkbox on each row. upon checking specific checkboxes on each row, those rows will be updated in the DB and for those rows that do not have the check, they will be deleted.

 

Dim ckbDelete as CheckBox 
Dim Conn as new sqlConnection(.....)

           daDelete = New SqlDataAdapter("", Conn)
           For intCount = 0 To dgTest.Items.Count - 1
               ckbDelete = dgTest.Items(intCount).FindControl("chkDelete")
               If ckbDelete.Checked = True Then
                   'If any checkbox is checked, then execute the following command and transaction.
                   With daDelete.SelectCommand
      .CommandText = "sp_Update"
                       .CommandType = CommandType.StoredProcedure
                       .Parameters.Add("@RowID", SqlDbType.NVarChar, 20).Value = dgTest.Items(intCount).Cells(1).Text
                       .ExecuteNonQuery()	
                   End With
               Else
  'If the checkbox is not checked, then delete them...
                   With daDelete.SelectCommand
      .CommandText = "sp_Delete"
                       .CommandType = CommandType.StoredProcedure
                       .Parameters.Add("@RowID", SqlDbType.NVarChar, 20).Value = dgTest.Items(intCount).Cells(1).Text
                       .ExecuteNonQuery()	
                   End With	
               End If
           Next

 

Instead of going through each rows like this tedeously, is there a way to update the datagrid more effeciently.

 

NOTE: the datagrid is binding to a datatable.

Donald DUCK : YOU ARE FIRED!!!
Posted

If the fields you are updating are all the same, then you can do a batch update after you have looped thru your datagrid, building a string of ID's you want to update. update table set field = value where id IN(1,2,3,4)

 

Do the same with the delete,except you dont need the SET part.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...