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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems updating datagrid

Status
Not open for further replies.

ceyhorn

Programmer
Nov 20, 2003
93
US
For the life of me, I cannot figure out why my code is not updating the data from the datagrid. It will go into edit mode, I can cancel and I can delete, but it will not update. it is always the small things in life. Well I don't know what to do except paste my code here and hope that one of you kind people will bring me back to the light.
Thanks in advance,
Chris

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim UserID As Integer
If Not Request.Cookies("Eyhorn") Is Nothing Then
Label1.Text = _
Server.HtmlEncode(Request.Cookies("Eyhorn")("UserName"))
UserID = Server.HtmlEncode(Request.Cookies("Eyhorn")("UserID"))
Else
Response.Redirect("Login.aspx")
End If

SqlDataAdapter1.Fill(DataSet31, "Sections")
Me.BindDataGrid()
'Put user code to initialize the page here
End Sub

Private Sub BindDataGrid()
DataGrid1.DataSource = DataSet31.Tables("Sections").DefaultView
DataGrid1.DataBind()
End Sub



Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim dr As DataRow
dr = DataSet31.Tables("Sections").NewRow
'dr("SectionID") = ""
dr("Section") = ""
DataSet31.Tables("Sections").Rows.Add(dr)
DataGrid1.EditItemIndex = DataSet31.Tables("Sections").Rows.Count - 1
SqlDataAdapter1.Update(DataSet31, "Sections")
Me.BindDataGrid()
Session("AddMode") = True
End Sub

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.UpdateCommand
Dim sectionid, section As String
Dim tb As TextBox
tb = CType(e.Item.Cells(0).Controls(0), TextBox)
sectionid = tb.Text
tb = CType(e.Item.Cells(1).Controls(0), TextBox)
section = tb.Text

Dim dr As DataSet3.SectionsRow
dr = DataSet31.Sections.FindBySectionID(sectionid)
dr.Section = section

SqlDataAdapter1.Update(DataSet31)
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub


Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
If Session("AddMode") = True Then
Dim i As Integer = DataSet31.Tables("Sections").Rows.Count - 1
If e.Item.ItemIndex <> i Then
DataSet31.Tables("Sections").Rows(i).Delete()
End If
Session("AddMode") = False
End If
DataSet31.Tables("Sections").Rows(e.Item.ItemIndex).Delete()
SqlDataAdapter1.Update(DataSet31)
Me.BindDataGrid()
End Sub

Private Sub DataGrid1_EditCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_CancelCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
 
You want to be checking for postback in your Page_Load event before binding...

Like this:

Code:
if(!Page.IsPostBack)
{
    BindFunction();
}

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top