Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 3rd, 2006, 10:47 PM
Authorized User
Points: 514, Level: 8
Points: 514, Level: 8 Points: 514, Level: 8 Points: 514, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default Gridview DataBind to DataTable not showing changes

I've got a gridview successfully bound to a datatable, at least for the first display.

However, when I add a row to the datatable (through another method), and then do a gridview.databind, the new row doesn't show up.

I'm pretty sure my syntax and timing for the databind are ok, because I swapped in the datatable for a DB table, where everything was working, otherwise not changing the "contextual" code. And it displays correctly on the first display, right after loading the datatable from the DB.

I do a debug.write on the datatable right before I databind, and the new row is there. The new row is also there in the "Get" method that the objectdatasource uses to "select" the datatable.

Maybe this has something to do with the way the gridview gets bound to the datatable, that there are some internal variables (like "row changed" ) which are currently beyond my newbility to grasp?

Any suggestions on how to debug this would be appreciated.

Thanks!
 
Old March 4th, 2006, 08:59 PM
Authorized User
Points: 514, Level: 8
Points: 514, Level: 8 Points: 514, Level: 8 Points: 514, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I believe the expression is: PBCK - "Problem Between Chair and Keyboard"

I was using multiple template types in the gridview, and during table row add, didn't include record type. Never finding record type, gridview wisely declined to display that row.

So, problem solved.

For an example of a test bed for using datatable as object data source, adding to it, and seeing the add row displayed, here's this.

===========PAGE================

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AppTestManager.aspx.vb" Inherits="MemberPages_AppTestManager_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <link href="../../MemberPages/CSS/Standard.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
<!--

function Button2_onclick() {
//alert("html button click")
document.getElementById("Hidden1").value = "AddRow"
document.forms[0].submit()
}

// -->
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Style="z-index: 100; left: 320px; position: absolute;
            top: 16px" Text="MakeTable" Width="72px" />
        <br />
        <input id="Button2" style="z-index: 104; left: 288px; width: 164px; position: absolute;
            top: 144px" type="button" value="AddRow HTML Submit" language="javascript" onclick="return Button2_onclick()" />
        <br />
        &nbsp;</div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" Style="z-index: 102;
            left: 8px; position: absolute; top: 12px" Width="216px">
        </asp:GridView>
        <asp:Button ID="Button3" runat="server" Text="AddRow" style="z-index: 103; left: 320px; position: absolute; top: 100px" />&nbsp;
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetTestTable"
            TypeName="TestTable"></asp:ObjectDataSource>

        <asp:HiddenField ID="Hidden1" runat="server" />
    </form>
</body>
</html>
==================

================== CODE BEHIND
Imports System.Diagnostics
Imports System.data
Partial Class MemberPages_AppTestManager_Default
    Inherits System.Web.UI.Page
    Public AC = System.Web.HttpContext.Current.Application

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'MsgBox("page load")
        Debug.WriteLine("---------------------PAGE LOAD--------------------")
        If IsPostBack Then
            Debug.WriteLine("---------------------IS POSTBACK --------------------")
            If Me.Hidden1.Value = "AddRow" Then
                AddNewFromPageCB()
                Me.Hidden1.Value = ""
            End If
        End If

    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Debug.WriteLine("---------------------ASP ADD ROW BUTTON--------------------")
        'MsgBox("add row from asp button")
        Dim exe As New TestTable
        exe.AddTableRow()

        Dim wrkTable As DataTable = AC("TestTable")
        Debug.WriteLine("-------JUST AFTER ADD ROW BEFOR DATABIND------")
        For n1 As Integer = 0 To wrkTable.Rows.Count - 1
            Dim wrkR As DataRow = wrkTable.Rows(n1)
            For n2 As Integer = 0 To wrkR.ItemArray.Length - 1
                Debug.Write(wrkR.Item(n2).ToString & " ")
            Next
            Debug.WriteLine("")
        Next
        Debug.WriteLine("-----------------------------------")
        Me.GridView1.DataBind()


    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Debug.WriteLine("---------------------MAKE TABLE BUTTON --------------------")
        'MsgBox("make table")
        Dim exe As New TestTable
        exe.MakeTestTable()
        Me.GridView1.DataBind()
    End Sub

    Public Sub AddNewFromPageCB()
        Debug.WriteLine("---------------------HTML ADD ROW BUTTON--------------------")

        'MsgBox("add row from html button")
        Dim exe As New TestTable
        exe.AddTableRow()

        Dim wrkTable As DataTable = AC("TestTable")
        Debug.WriteLine("--From HTML Button------")
        For n1 As Integer = 0 To wrkTable.Rows.Count - 1
            Dim wrkR As DataRow = wrkTable.Rows(n1)
            For n2 As Integer = 0 To wrkR.ItemArray.Length - 1
                Debug.Write(wrkR.Item(n2).ToString & " ")
            Next
            Debug.WriteLine("")
        Next
        Debug.WriteLine("-----------------------------------")

        Me.GridView1.DataBind()

    End Sub

End Class


==================

================== CUSTOM CLASS

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.Common
Imports System.Diagnostics
Imports System.IO
Imports System.Data.OleDb
Imports System.Data.Common.DbConnection

Public Class TestTable
    Public AC = System.Web.HttpContext.Current.Application

    Public Sub MakeTestTable()
        'MsgBox("making table")

        Dim TestTable As DataTable = New DataTable("TestTable")

        Dim wrkArr1 As New ArrayList ' name
        Dim wrkArr2 As New ArrayList ' type
        Dim wrkArr3 As New ArrayList ' default value

        '--------------------fill arrays with particulars
        For n As Integer = 1 To 5
            wrkArr1.Add("Field" & Trim(Str(n))) : wrkArr2.Add("String") : wrkArr3.Add("")
        Next
        '------------------------convert arrays to datatable columns
        For n As Integer = 0 To wrkArr1.Count - 1
            Dim wrkColumn As DataColumn = New DataColumn
            wrkColumn.DataType = System.Type.GetType("System." & wrkArr2(n))
            wrkColumn.AllowDBNull = True
            wrkColumn.Caption = wrkArr1(n)
            wrkColumn.ColumnName = wrkArr1(n)
            wrkColumn.DefaultValue = wrkArr3(n)
            TestTable.Columns.Add(wrkColumn)
        Next
        TestTable.Constraints.Clear()


        For n As Integer = 1 To 5
            Dim wrkRow As DataRow = TestTable.NewRow
            Dim wrkVal As String = ""
            For n1 As Integer = 1 To 5
                wrkVal = wrkVal & Mid("ABCDEF", n, 1)
            Next
            For n2 As Integer = 0 To 4
                wrkRow.Item(n2) = wrkVal
            Next
            TestTable.Rows.Add(wrkRow)
        Next


        AC.Lock()
        AC("TestTable") = TestTable
        AC.UnLock()

        '------------debug.write table content
        Debug.WriteLine("---------AFTER MAKE TABLE ------------")
        For n1 As Integer = 0 To TestTable.Rows.Count - 1
            Dim wrkR As DataRow = TestTable.Rows(n1)
            For n2 As Integer = 0 To wrkR.ItemArray.Length - 1
                Debug.Write(wrkR.Item(n2).ToString & " ")
            Next
            Debug.WriteLine("")
        Next
        Debug.WriteLine("-----------------------------------")

    End Sub

    Public Sub AddTableRow()

        Dim TestTable As DataTable = AC("TestTable")

        Dim wrkRow As DataRow = TestTable.NewRow
        For n1 As Integer = 0 To 4
            wrkRow.Item(n1) = Mid("ABCDEFGHJKLMNOP", n1 + 1, 4)
        Next
        TestTable.Rows.Add(wrkRow)

        Debug.WriteLine("---------AFTER ADD ROW-------")
        For n1 As Integer = 0 To TestTable.Rows.Count - 1
            Dim wrkR As DataRow = TestTable.Rows(n1)
            For n2 As Integer = 0 To wrkR.ItemArray.Length - 1
                Debug.Write(wrkR.Item(n2).ToString & " ")
            Next
            Debug.WriteLine("")
        Next
        Debug.WriteLine("-----------------------------------")

    End Sub

    Public Function GetTestTable() As DataTable
        Dim wrkTable As DataTable = AC("TestTable")

        Debug.WriteLine("-------DURING GET TABLE------")
        For n1 As Integer = 0 To wrkTable.Rows.Count - 1
            Dim wrkR As DataRow = wrkTable.Rows(n1)
            For n2 As Integer = 0 To wrkR.ItemArray.Length - 1
                Debug.Write(wrkR.Item(n2).ToString & " ")
            Next
            Debug.WriteLine("")
        Next
        Debug.WriteLine("-----------------------------------")

        Return wrkTable

    End Function

End Class




Similar Threads
Thread Thread Starter Forum Replies Last Post
Databind DarkForce ASP.NET 2.0 Basics 5 January 25th, 2011 12:04 PM
How to Load data from GridView to DataTable csharplearner ASP.NET 2.0 Basics 1 May 9th, 2007 04:36 AM
About the Databind() kuuy BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 1 November 15th, 2006 12:56 PM
DataView will not DataBind with a GridView?! jason.l ASP.NET 2.0 Basics 0 October 13th, 2006 10:01 PM
Databind in TextBox kapi.goel C# 1 April 26th, 2006 05:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.