DataGrid Question

  • Thread starter Thread starter WStoreyII
  • Start date Start date
W

WStoreyII

Do you need a pre made database in order to populate a datagrid?
or is there a way to make a datagrid with user defined columns and queries
ect and then save this info to a new database or an existing database if
desired?


WSToreyII
 
Hi WSToreyII

You need a database, and in my opinion the most easy one is the datatable.

This I did send to Babich today.

You can use the Datagrid, it is a misunderstanding often that you cannot use
it when there is no database.

You only have to create your own datatable which is very easy roughly typed
not checked..
dim dt as new datatable
dim dc as new datacolumn
dt.add(dc)
dim dr as datarow = new datatable.newrow
dt.add(dr)
Now you have a one row one column datatable.
datatable(0).rows(0).item(0)

this you can use as datasource for your datagrid.

I hope this helps?

Cor
 
I Tried this out Cor But i got an error
"Additional information: Cannot create a child list for field MyTable"

Here is My Code
Private Sub New_DataTable(ByVal NColumns As Integer, ByVal NRows As Integer)

Dim MyTable As DataTable = New DataTable

Dim MyDs As DataSet = New DataSet

Dim MyDv As DataView = New DataView

Dim MyColumns(NColumns - 1) As DataColumn

Dim MyRows(NRows - 1) As DataRow

Dim I, X, Z As Integer

MyDs.Tables.Add(MyTable)

For I = 0 To UBound(MyColumns)

MyColumns(I) = New DataColumn

With MyColumns(I)

..Caption = I

..ColumnName = I

..DataType = System.Type.GetType("System.Decimal")

..DefaultValue = I

..ExtendedProperties.Add("TimeStamp", DateTime.Now)

End With

MyTable.Columns.Add(MyColumns(I))

Next

For X = 0 To UBound(MyRows)

MyRows(X) = MyTable.NewRow

MyTable.Rows.Add(MyRows(X))

Next

MyDg.SetDataBinding(MyDs, "MyTable") ' Here is Where
I get The Error

End Sub

Where did I go wrong? Thanks for in advance for your help.

WStoreyII
 
Hi WStorey,

That I have more, I wanted to write datasource and write database.
You do not need a database you need a datasource and the most easy one is a
datatable.

I start changing your code it is late here so watch very good typos and
other errors.
\\\
Private Sub New_DataTable(ByVal NColumns As Integer, ByVal NRows As Integer)
Dim MyTable As DataTable = New DataTable
For i as integer = 0 to NColumns
dim dc as new datacolumn(chr(i))
MyTable.add(dc)
Next
For i as integer = 0 to NRows
dim dr as dr = MyTable.newrow
MyTable.add(dr)
Next
Dim MyDv as New DataView(MyTable)
MyDatagrid.datasource=dv
///
I never did this (with an empty dataset, filled I did), but this should
create in my opinion a complete empty datagrid with the columns and rows
that you did give. (I have not given the datatypes, this is only a quick
sample using your code)

I hope this helps, and if you have more questions tell them, but that I
answer not anymore today.

Cor
 
Actually, the previous code would work as long as you give the DataTable a
name when you create it.

Change this line

Dim MyTable As DataTable = New DataTable

to

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

The call to SetDataBinding takes the name of the table as the second
argument. You were passing in "MyTable" as the name, but you had never given
the table a name. I tried this out and it works...
 
Back
Top