using rows in datagrid in stored procedure

crussell96

New member
Joined
Jul 18, 2005
Messages
1
Programming Experience
Beginner
Hello, here is what I am doing.


I have a datagrid named dgClassStartDates with 2 colums displayed
Description and date. When the user clicks the 'Save' button it
executes a stored procedure that accepts 3 parameters. 2 come from the
columns datagrid and the third is a variable. The code for attaching
and giving the values to the parameters is below. Now I can get it to
work fine with only 1 row in the datagrid but exactly what steps do I
need to take if there are multiple rows in the datagrid. Someone suggested looping through all rows in the datagrid but I cant seem to get that to work.


'Attach Parameters
cmdClassPeriodStartDatesAddNew­.Parameters.Add(prmStartDescri­ption)
cmdClassPeriodStartDatesAddNew­.Parameters.Add(prmStartDate)
cmdClassPeriodStartDatesAddNew­.Parameters.Add(prmCaseID)


'Give Parameters values
prmStartDescription.Value =
frmCaseKeyDatesOpen.dgClassSta­rtDates.Item(0, 0)
prmStartDate.Value = frmCaseKeyDatesOpen.dgClassSta­rtDates.Item(0, 1)
prmCaseID.Value = globalCaseID 'This is a variable
'Execute command
cmdClassPeriodStartDatesAddNew­.ExecuteNonQuery()
 
you would just loop thru the grid something like so. this is an example of an attendance grid that i had

Dim grdItem As DataGridItem

ForEach grdItem In grdParticpants.Items

Me.Attended = CType(grdItem.Cells(1).Controls(1), CheckBox).Checked

Me.TimeStamp = Convert.FromBase64String(CType(grdItem.Cells(3).Controls(1), Label).Text)

Me.AttendanceID = CType(grdItem.Cells(4).Controls(1), Label).Text

Me.ParticipantID = CType(grdItem.Cells(5).Controls(1), Label).Text

intResult = SaveAttendanceRecord(
Me.AttendanceID, Me.ParticipantID, Me.TimeStamp, Me.Attended)

Next


SaveAttendance is simply a function that fires a stored procedure to update a record
 
If you have multiple rows to update, you should absolutely not do it one row at a time. Put your data into a DataTable and use a DataAdapter to do a batch update. You can bind your DataGrid to a DataTable so that any changes made in one are reflected in the other. I'd suggest you check the help documentation for the DataAdapter classes. Each Data namespace (SqlClient, OleDb, etc.) has its own version.
 
Back
Top