Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Binding web datagrid control to a dataset

Posted on 2011-10-08
6
Medium Priority
?
1,233 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:mgmhicks
  • 3
  • 2
6 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 36937245
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

0
 

Author Comment

by:mgmhicks
ID: 36938738
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

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36941732
Its bit different when using Dataset in ASP.NET. As soon as page is rendered to the client, your dataset goes out of scope and is destroyed. ASP.NET has a better option called SQLDataSource

http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx#introduction
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

Expert Comment

by:Miguel Oz
ID: 36941873
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)
0
 

Author Comment

by:mgmhicks
ID: 36942092
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.
0
 

Author Comment

by:mgmhicks
ID: 36942140
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.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Loops Section Overview
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question