Groups | Blog | Home
all groups > dotnet windows forms databinding > march 2005 >

dotnet windows forms databinding : Getting column widths in an untyped dataset


Erwin Pant via DotNetMonster.com
3/18/2005 3:11:34 PM
Hi,

I already sent this question however I can't seem to find it so I'm gonna
submit it again.

I'm populating .dbf and access tables into my datasets. These files are
coming from clients so the layouts always change therefore I can't rely on
using typed datasets. Is there a way of getting a column width working
with an untyped dataset ? I tried using the WriteXmlSchema method which
writes to an XSD file but no column widths. The only way that I can think
of is:
- for .dbf files:
Open the .dbf file as a text file and extract the info from the header

- for access tables:
Create an access object and get the column widths from the system
tables

There's got to be an easier way !!

Thanx

E

--
Erwin Pant via DotNetMonster.com
3/23/2005 6:03:47 PM
I seem to have found the answer to my question. Here is a way:

-----------------
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim dc As DataColumn
Dim dt As DataTable
Dim dr As DataRow
Dim strSQL As String = "SELECT * FROM Customers"

da = New SqlDataAdapter(strSQL, NorthwindConnectionString)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey

' Use the FillSchema method
ds = New DataSet()
da.FillSchema(ds, SchemaType.Source, "Customers")

dt = New DataTable()
dt.Columns.Add("ColumnName", GetType(System.String))
dt.Columns.Add("DataType", GetType(System.String))
dt.Columns.Add("AllowDBNull", GetType(System.String))
dt.Columns.Add("DefaultValue", GetType(System.String))
dt.Columns.Add("MaxLength", GetType(System.String))
dt.Columns.Add("Unique", GetType(System.String))

For Each dc In ds.Tables("Customers").Columns
dr = dt.NewRow
dr(0) = dc.ColumnName
dr(1) = dc.DataType
dr(2) = dc.AllowDBNull.ToString
dr(3) = dc.DefaultValue.ToString
dr(4) = dc.MaxLength.ToString
dr(5) = dc.Unique.ToString
dt.Rows.Add(dr)
Next dc

' Bind the DataGrid to the FillSchema DataTable
DataGrid1.DataSource = dt

' Bind the DataGrid to the results
DataGrid1.DataSource = ds.Tables("Customers")
-----------------

Hope this is useful

E

--
AddThis Social Bookmark Button