Link to home
Start Free TrialLog in
Avatar of mgmhicks
mgmhicks

asked on

Binding web datagrid control to a dataset

I have bound a dataset to a web datagrid.  do you still have to update the dataset progammatically with the new values of the grid?  In windows forms that is pretty much taken care of by the databinding and the grid.  Doesnt seem to be working that way.  I edit the grid and update, however all the values I enter are gone once I click the "Update".  Here is the code in my Row_updating event.


Protected Sub gvInspectionDetails_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInspectionDetails.RowEditing
        ' If Loading Then
        'Else
        gvInspectionDetails.EditIndex = e.NewEditIndex
        gvInspectionDetails.DataSource = myDS.Tables(1).DefaultView
        gvInspectionDetails.DataBind()

        'End If

    End Sub

    Private Sub gvInspectionDetails_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvInspectionDetails.RowUpdating
                        myDS.Tables(1).AcceptChanges()
        gvInspectionDetails.EditIndex = -1
                gvInspectionDetails.DataSource = myDS.Tables(1)
        gvInspectionDetails.DataBind()
    End Sub

Open in new window

Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Can you include he markup of gvInspectionDetails? (also how is the dataset created?)
For starters your events should update the dataset directly before binding.
Asp.net is stateless and your code is controling where the binding is happening, thus whatever current contents of the dataset are loaded again to gvInspectionDetails.

Pseudo code follows:
Protected Sub gvInspectionDetails_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInspectionDetails.RowEditing
        'Assing changes  from gvInspectionDetails to myDS.Tables(1)
        gvInspectionDetails.EditIndex = e.NewEditIndex
        gvInspectionDetails.DataSource = myDS.Tables(1).DefaultView
        gvInspectionDetails.DataBind()
    End Sub

Avatar of mgmhicks
mgmhicks

ASKER

I've attached the codebehind as well as the aspx page.  thank you for your help.  I can do this in windows forms no problem, but web pages just seem entirely different to me.

thanks again.



Imports System.Data.SqlClient
Imports System.Data
Imports System.IO


Partial Public Class MOInspectionsEdit
    Inherits System.Web.UI.Page
    Dim myWO As String = ""
    Dim myPropertyID As String = ""
    Dim myDA As New SqlDataAdapter
    Shared myDS As New DataSet
    Private Const myConnStr As String = "SERVER=xx.xx.xx.xx,8433;Database=AptGalleryInspections;uid=sa;pwd=sh4207"
    Dim myconn As New SqlConnection(myConnStr)
    Dim Loading As Boolean = True

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            myWO = Request.QueryString("WO")
            myPropertyID = Request.QueryString("PropName")
            gvInspectionDetails.AlternatingRowStyle.BackColor = Drawing.Color.White

            initializeForm()
        Else
        End If
        Loading = False
    End Sub
    Protected Sub initializeForm()
        Dim i As Integer = 0
        Dim myCMD As New SqlCommand
        Dim myCMD2 As New SqlCommand
        With myCMD
            .Connection = myconn
            .CommandText = "SELECT * FROM inspectionMaster WHERE wo_number='" & myWO & "'"
            .CommandType = CommandType.Text
        End With
        With myCMD2
            .Connection = myconn
            .CommandText = "SELECT * FROM inspectiondetails WHERE wo_number='" & myWO & "' ORDER BY ItemGroup"
            .CommandType = CommandType.Text
        End With
        Try
            myconn.Open()
            myDA.SelectCommand = myCMD
            myDA.Fill(myDS, "Master")
            myDA.SelectCommand = myCMD2
            myDA.Fill(myDS, "Details")
            myconn.Close()
            Dim myCol As New DataColumn
            myCol.ColumnName = "UpDateCost"
            myCol.DefaultValue = False
            myDS.Tables(1).Columns.Add(myCol)
            Dim myCol2 As New DataColumn
            myCol2.ColumnName = "UpDatePrice"
            myCol2.DefaultValue = False
            myDS.Tables(1).Columns.Add(myCol2)

            myDS.Tables(1).AcceptChanges()
            ' myDS.Tables(1).DefaultView.Sort = "itemgroup"
            gvInspectionDetails.DataSource = myDS.Tables(1)
            gvInspectionDetails.DataBind()
            lblWoNumber.Text = myDS.Tables(0).Rows(0).Item(1)
            lblWoDescrip.Text = myDS.Tables(0).Rows(0).Item(2)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub gvInspectionDetails_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvInspectionDetails.RowCancelingEdit
        myDS.Tables(1).RejectChanges()
        gvInspectionDetails.EditIndex = -1
        gvInspectionDetails.DataSource = myDS.Tables(1)
        gvInspectionDetails.DataBind()
    End Sub

    Protected Sub gvInspectionDetails_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInspectionDetails.RowEditing
        ' If Loading Then
        'Else
        gvInspectionDetails.EditIndex = e.NewEditIndex
        gvInspectionDetails.DataSource = myDS.Tables(1).DefaultView
        gvInspectionDetails.DataBind()

        'End If

    End Sub

    Private Sub gvInspectionDetails_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvInspectionDetails.RowUpdating
        '  Dim myText As TextBox
        'myText = dgGoalsAdd.Rows(e.RowIndex).FindControl("txtGoals")


        myDS.Tables(1).AcceptChanges()
        gvInspectionDetails.EditIndex = -1
        ' myDS.Tables(0).Rows(e.RowIndex).Item(4) = myText.Text
        gvInspectionDetails.DataSource = myDS.Tables(1)
        gvInspectionDetails.DataBind()
    End Sub

    Protected Sub gvInspectionDetails_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvInspectionDetails.SelectedIndexChanged

    End Sub
End Class

Open in new window

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="Inspections.Master" CodeBehind="MOInspectionsEdit.aspx.vb" Inherits="eSiteCompanionDevelopement.MOInspectionsEdit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table style="width: 100%">
        <tr>
            <td style="width: 181px">
                WO. Number:  <asp:Label ID="lblWoNumber" runat="server" Text='Label1'></asp:Label>
&nbsp;</td>
            <td colspan="2">
                Wo. Description:&nbsp;
                <asp:Label ID="lblWoDescrip" runat="server" Text="Label"></asp:Label>
            </td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td style="width: 181px">
                &nbsp;</td>
            <td style="width: 173px">
                &nbsp;</td>
            <td style="width: 140px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td style="width: 181px">
                &nbsp;</td>
            <td style="width: 173px">
                &nbsp;</td>
            <td style="width: 140px">
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td colspan="6">
                <asp:GridView ID="gvInspectionDetails" runat="server" Width="967px" 
                    AutoGenerateColumns="False" AutoGenerateEditButton="True">
                    <Columns>
                        <asp:BoundField DataField="ItemID" HeaderText="Item ID" ReadOnly="True">
                        <ItemStyle Wrap="False" />
                        </asp:BoundField>
                        <asp:BoundField DataField="itemGroup" HeaderText="Room " ReadOnly="True">
                        <ItemStyle Wrap="False" />
                        </asp:BoundField>
                        <asp:BoundField DataField="ItemDescription" HeaderText="Description" 
                            ReadOnly="True">
                        <ItemStyle Wrap="False" />
                        </asp:BoundField>
                        <asp:BoundField DataField="ItemQty" HeaderText="Quantiy">
                        <ItemStyle Wrap="False" />
                        </asp:BoundField>
                        <asp:BoundField DataField="ItemCost" DataFormatString="{0:c}" HeaderText="Cost" 
                            HtmlEncode="False">
                        <ItemStyle Width="75px" Wrap="False" />
                        </asp:BoundField>
                        <asp:BoundField DataField="ItemPrice" DataFormatString="{0:c}" 
                            HeaderText="Price">
                        <ItemStyle Width="75px" Wrap="False" />
                        </asp:BoundField>
                        <asp:BoundField DataField="ResidentCharge" HeaderText="Type">
                        <ItemStyle Wrap="False" />
                        </asp:BoundField>
                        <asp:CheckBoxField DataField="UpdateCost" HeaderText="Update Cost">
                        <ItemStyle Width="50px" Wrap="False" />
                        </asp:CheckBoxField>
                        <asp:CheckBoxField DataField="UpdatePrice" HeaderText="Update Price">
                        <ItemStyle Width="50px" Wrap="False" />
                        </asp:CheckBoxField>
                    </Columns>
                </asp:GridView>
                <br />
                <br />
                <br />
            </td>
        </tr>
        <tr>
            <td colspan="6">
                &nbsp;</td>
        </tr>
    </table>
</asp:Content>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yep, I was about to suggest SqlDataSource after seeing your posted markup, but it depends on your reasons to binding programatically (e.g. performance or sharing the dataset/connection). Let us know more about your reasons and design decisions as the solution gets a bit more complex as you need to keep track fo changes and update the dataset programatically before binding to database.  (see my initial post)
What I have to do is give the  a list of rows to edit in the grid.  I need to add two columns to the dataset to handle whether they want to update the price and cost when the update that perticular record. It doesnt matter to me if we save the new changes from the grid to a dataset or we post back to the database and make changes then.  However no matter what I do I cant seem to pull the new values I entered into the grid, so I can change the dataset.   What can we do to use the dataset, get changes on grid and update dataset.  Then I will have save button to update dataset to the database.

thanks I'm really in a jam to get this out and all the help is appreciated.
I'm not getting why when I change the grid row, all editable items show "" when looking at the value of that rows cells.   How do I access the changed values of the grid, or the original values for that matter.