|
Subject:
|
Creating a Datagrid
|
|
Posted By:
|
ja8261
|
Post Date:
|
11/12/2004 2:47:15 PM
|
I am trying to create a Datagrid to import and edit from text and csv files. If I was to import into Excel and use the Text to Columns feature they would have 34 columns. With this in mind I need a Datagrid that has 34 columns. I am just now learning how to create datagrids. This is what I have so far:
Private Sub FormatGrid() Dim Tbl As New DataTable Dim Col1 As New DataColumn
Col1.DataType = System.Type.GetType("System.Int32") Col1.ColumnName = "Date" Col1.AutoIncrement = True Col1.Caption = "Date" Col1.ReadOnly = True Tbl.Columns.Add(Col1)
datagridOutput.DataSource = Tbl end sub
I'm not sure if this is the best way to create a datagrid but it was the first way I learned how. As you can see the problem I have is that I will have to declare 34 variables for each column and then go through the process of defining each column like I have done. Is there a way that I can declare the columns in an array as well as the heading names in an array and then create a loop that will run 34 times to create the datagrid with 34 columns with their heading names? Maybe someone could give me a code snippet that would show how to accomplish this.
Thanks.
Jeff Armstrong Sr. Systems Mgr SBC Services, Inc.
|
|
Reply By:
|
ja8261
|
Reply Date:
|
11/12/2004 3:43:00 PM
|
I may have asked a little prematurely. I've seem to at least create all the headings although it is a bit slow. Any ideas as to how to speed up the execution??? Below is the code I came up with.
Private Sub FormatGrid() Dim Tbl As New DataTable Dim I As Integer Dim Col(31) As DataColumn Dim HeadingName() As String = New String(31) {"Date", ...."Last Heading"} ' Define columns, specify the column data type, name is to identify the column, ' enable automatically increment the data, value to be displayed in the header ' of the column, make this column read only, Since it is autoincrement, ' add column to the datatable For I = 0 To 31 Col(I) = New DataColumn Col(I).DataType = System.Type.GetType("System.Int32") Col(I).ColumnName = HeadingName(I) Col(I).AutoIncrement = True Col(I).Caption = HeadingName(I) Col(I).ReadOnly = True Tbl.Columns.Add(Col(I)) Next
' BIND THE DATATABLE TO THE DATAGRID datagridOutput.DataSource = Tbl End Sub
Jeff Armstrong Sr. Systems Mgr SBC Services, Inc.
|
|
Reply By:
|
jaucourt
|
Reply Date:
|
11/12/2004 4:35:21 PM
|
If the data you're trying to import from the CSV will always have the same columns, you'd be best off creating a strongly-typed dataset. To do this, in the solution explorer right click and choose to add a new item, then select dataset.
You'd be best off doing a ado.net tutorial; try a search on google, or http://visualbasic.about.com/od/learnvbnet/ss/ecvbsbs2001.htm
Once you're happy with datasets, you can populate one from a csv file through using the streamreader classes in the System.IO namespace
|