Trying to bind a typed dataset to a grid

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
I can't bind a typed dataset to a datagrid. I've tried all kinds of things, but it just won't bind. I know I'm missing something! Weird thing is, I've done this many, many times before with no problem!

Here is my xml schema for the typed dataset:
<code>
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="dsNAFCounts" targetNamespace="http://tempuri.org/dsNAFCounts.xsd" elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="http://tempuri.org/dsNAFCounts.xsd" xmlns:mstns="http://tempuri.org/dsNAFCounts.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="dsNAFCounts" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="NAFCounts">
<xs:complexType>
<xs:sequence>
<xs:element name="RegLit" type="xs:string" minOccurs="0" />
<xs:element name="RegCnt" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
</code>

Here is my HTML for the grid:
<CODE>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="NAFcnts.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id=DataGrid1 runat="server" DataMember="NAFCounts" DataSource="<%# DsNAFCounts1 %>">
</asp:datagrid></form>
</body>
</HTML>

Here is my code-behind for the webform:
<code>
Imports System.Data.OleDb

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DsNAFCounts1 = New NAFcnts.dsNAFCounts
CType(Me.DsNAFCounts1, System.ComponentModel.ISupportInitialize).BeginInit()
'
'DsNAFCounts1
'
Me.DsNAFCounts1.DataSetName = "dsNAFCounts"
Me.DsNAFCounts1.Locale = New System.Globalization.CultureInfo("en-US")
CType(Me.DsNAFCounts1, System.ComponentModel.ISupportInitialize).EndInit()

End Sub
Protected WithEvents DsNAFCounts1 As NAFcnts.dsNAFCounts
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private dsCnts As New dsNAFCounts

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim drCnt As dsNAFCounts.NAFCountsRow = dsCnts.NAFCounts.NewNAFCountsRow
drCnt.RegCnt = "90"
drCnt.RegLit = "test"
dsCnts.NAFCounts.AddNAFCountsRow(drCnt)

DataGrid1.DataSource = dsCnts
DataGrid1.DataBind()

End Sub

End Class
</code>

The following code binds a grid to an untyped dataset which works fine...
Here is the html for an untyped dataset:
<code>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="NAFcnts.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 416px; POSITION: absolute; TOP: 224px"
runat="server"></asp:DataGrid>
</form>
</body>
</HTML>
</code>

Here is the code-behind for the untyped dataset:
<code>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
ds.Tables.Add("test")

Dim fNameColumn As DataColumn = New DataColumn
fNameColumn.DataType = System.Type.GetType("System.String")
fNameColumn.ColumnName = "RegCnt"
ds.Tables(0).Columns.Add(fNameColumn)
Dim fNameColumn2 As DataColumn = New DataColumn
fNameColumn2.DataType = System.Type.GetType("System.String")
fNameColumn2.ColumnName = "RegLit"
ds.Tables(0).Columns.Add(fNameColumn2)

' Once a table has been created, use the NewRow to create a DataRow.
Dim myRow As DataRow
myRow = ds.Tables(0).NewRow()

' Then add the new row to the collection.
myRow(0) = "90"
myRow(1) = "test"
ds.Tables(0).Rows.Add(myRow)

DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()

End Sub
</code>

Can someone PLEASE help me out with this? Just copy and paste the code into a project(Webform1 and Webform2 and create a dataset).
When I'm debugging my code-behind in the Typed dataset module, I check the contents of dsCnts in the following line<code> DataGrid1.DataSource = dsCnts
</code>, and it has 1 record in it which is what I'm expecting.

When the datagrid binds the data, it doesn't show the data!
Why won't the datagrid bind this data in the Typed dataset and show me the record? All it shows is just the column headings, while the Untyped test shows the column headings and the data (1 record as well).
 
datalist binding problem

WhenI took away the datasource and datamember in the html and placed it in the code-behind, it worked like so:
<code>
DataGrid1.DataSource = dsCnts
DataGrid1.DataBind()
</code>
I had the wrong Datasource in the HTML to begin with.

However, I'm trying the same code for a DataList. I don't have any Datasource or Datamember defined in the HTML. In the HTML, I have the following:
<code>
<asp:DataList id="DataList1" runat="server"></asp:DataList>
</code>
In the code-behind, I have the following:
<code>
DataList1.DataSource = dsCnts
DataList1.DataBind()
</code>
No data is displayed with the above code. What's the difference between that and the code for the datagrid using the exact same code with 1 record in the dataset? Why doesn't the DataList display?
 
Back
Top