Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid Update

Status
Not open for further replies.

Compkitty

Programmer
Jan 7, 2005
121
US
My datagrid isn't updating the right value . I type the value into the record and hit update, and instead of updating the record w/ the value I put in the TxtBx it will update w/ the value from the last record in the datagrid.
Ie. it's updating the right record, but w/ the value from the last record of the datagrid.

Code:
Dim Dgi As DataGridItem

        For Each Dgi In Me.Dg.Items

            Dim AdventId As String = Convert.ToString(Dg.DataKeys(Dgi.ItemIndex))
            Dim MgrId As String = CType(Dgi.FindControl("LblMgrID"), Label).Text
            Dim Manager As String = CType(Dgi.FindControl("txtManager"), TextBox).Text

            Dim MgrIdParam As New SqlParameter("@MgrId", SqlDbType.VarChar)
            MgrIdParam.Direction = ParameterDirection.Input

            Dim AdvParam As New SqlParameter("@AdventId", SqlDbType.VarChar)
            AdvParam.Direction = ParameterDirection.Input


            Dim MgrParam As New SqlParameter("@Manager", SqlDbType.VarChar)
            MgrParam.Direction = ParameterDirection.Input

            Dim DaUpdate As New SqlDataAdapter
            DaUpdate.UpdateCommand = New SqlCommand
            DaUpdate.UpdateCommand.Connection = SqlConn1
            DaUpdate.UpdateCommand.CommandText = "Sp_UpdateReconData"
            DaUpdate.UpdateCommand.CommandType = CommandType.StoredProcedure
            DaUpdate.UpdateCommand.Parameters.Add(MgrIdParam)
            DaUpdate.UpdateCommand.Parameters("@MgrId").Value = "10dr"
            DaUpdate.UpdateCommand.Parameters.Add(AdvParam)
            DaUpdate.UpdateCommand.Parameters("@AdventId").Value = AdventId
            DaUpdate.UpdateCommand.Parameters.Add(MgrParam)
            DaUpdate.UpdateCommand.Parameters("@Manager").Value = Manager

            SqlConn1.Open()
            DaUpdate.UpdateCommand.ExecuteNonQuery()
            SqlConn1.Close()
        Next

THANKS
 
I have found that you must both call EndEdit on the DataGrid and do a little trick with changing the CurrentRowIndex:

Code:
'This is the table style that is displayed and the DataGrid
'initialized elsewhere in your code
Dim tbl As DataGridTableStyle
Dim grd As DataGrid

'This method is necessary because the second method does
'not work when switching tabs if the DataGrid is on a tab
grd.EndEdit(tbl.GridColumnStyles(grd.CurrentCell.ColumnNumber), grd.CurrentRowIndex, False)
				
'Using the EndEdit method of the DataGrid alone is not good
'enough because it does not work for added rows
grd.CurrentRowIndex = grd.CurrentRowIndex

Note that the current row index is not changed but it does trick the DataGrid into ending an edit for added rows.

Be aware that you will run into the same problem for any data bound form controls like a TextBox.
 
This seems to be quite a common post - datagrids and updating. Here is a snippet of code I keep posting. It may help (it may not!!!), but it's something to chew on !!!



Assuming you have already got a few controls in place, this should work. It's an extract of code from one of my programs which does exactly what you want. (of course you'll have to change a few descriptions, but hopefully you'll get the idea)


Try ' update the datagrid with the data off the form
sqlcnn.Open()
Dim dr As DataRow = ds.Tables("tblAuthorisedOverrides").NewRow()
Dim d1 As String = Format(Date.Today, "yyyyMMdd")
Dim t1 As String = Format(Date.Now, "HHmmssss")
Dim r1 As String = Int(Rnd() * 9999)
Dim key As String
key = d1 & t1 & r1
dr.Item("intRecordKey") = key
dr.Item("strEmpID") = txtEmployeeID.Text
dr.Item("dtedate") = txtOverTimeDate.Text
dr.Item("strSurname") = txtEmployeeSurname.Text
dr.Item("strForename") = txtEmployeeForename.Text
dr.Item("strOvertimeClaimed") = txtTimeClaimed.Text
dr.Item("strOverrideCode") = txtOverrideCode.Text
dr.Item("strAuthorisedBy") = txtstrUserID.Text
dr.Item("strOverrideType") = txtOvertimeType.Text
ds.Tables("tblAuthorisedOverrides").Rows.Add(dr)


Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

sqlda.Update(ds)
sqlcnn.Close()


PS this bit of code is assigned to a button click event
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top