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

Gridview DataBind to DataTable not showing changes

Status
Not open for further replies.

bitseeker

Programmer
Nov 27, 2005
60
0
0
US
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.

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!
 
one more point...I do a debug.write on the datatable in the "Get" method associated with the "select" in the object datasource, and the new record is there, too. But does not show up in the immediately following display of the gridview.
 
You need to show the code for adding the row to the datatable as well as when and where you rebind the grid.

Jim
 
Thanks for the response.

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" "
<html xmlns=" >
<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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top