Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 12th, 2004, 03:47 PM
Authorized User
 
Join Date: Aug 2004
Location: OKC, OK, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating a Datagrid

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.
__________________
Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.
  #2 (permalink)  
Old November 12th, 2004, 04:43 PM
Authorized User
 
Join Date: Aug 2004
Location: OKC, OK, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
  #3 (permalink)  
Old November 12th, 2004, 05:35 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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/lear...cvbsbs2001.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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom paging in Datagrid with datagrid page count madhusrp ASP.NET 1.0 and 1.1 Professional 12 June 2nd, 2008 01:15 PM
creating columns in datagrid pady123 ASP.NET 1.x and 2.0 Application Design 0 September 8th, 2007 01:08 AM
Creating DataGrid In Repeater/DataGrid liduwan ASP.NET 1.0 and 1.1 Professional 9 March 6th, 2007 03:31 PM
Creating on-demand validation in DataGrid sankar General .NET 0 June 22nd, 2004 05:02 AM
Creating a Hyperlink in a datagrid aadz5 ASP.NET 1.0 and 1.1 Basics 0 October 25th, 2003 07:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.