displaying Array data in a Data Grid in VB.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crusson
    New Member
    • Mar 2008
    • 18

    displaying Array data in a Data Grid in VB.net

    I'm trying to get an array to display in a data grid form in vb.net

    The array has been imported from a csv, such that every array item is [date,time,flag] but I can split or join as needed to change the format of the array.

    I want date, time, and flag to be seperate columns and each array item to be a new row.

    I don't know where to start with this problem. I have looked through the msdn website and done some googling to no avail. If someone can point me to some better resources than I've been finding, it would be much appreciated.

    Cheers
  • saran23
    New Member
    • Mar 2008
    • 28

    #2
    Originally posted by crusson
    I'm trying to get an array to display in a data grid form in vb.net

    The array has been imported from a csv, such that every array item is [date,time,flag] but I can split or join as needed to change the format of the array.

    I want date, time, and flag to be seperate columns and each array item to be a new row.

    I don't know where to start with this problem. I have looked through the msdn website and done some googling to no avail. If someone can point me to some better resources than I've been finding, it would be much appreciated.

    Cheers
    Hi,
    I have an idea to implement , Just try Im not sure this might be a correct solution,
    here goes..
    1.First create a datatable with rows and columns based on the array structure, 2.using a foreach loop, write the array items to this datatable,
    3. Then bind this datatable to the gridview.

    After trying this Let me know It works or not....

    Thanks
    Saran

    Comment

    • crusson
      New Member
      • Mar 2008
      • 18

      #3
      Thanks.

      My program is a notetaking program for surveying, so I'm trying to make it super robust. The array that is holding the data, reopens the text file each time it's going to do soemthing to it and restores it after every change it makes. Towards the robust attempt, I'm trying to avoid using a database for storage altogether. Perhaps if it's just an intermediary step, It's not such a big deal, but if I could go straight from an array into the data base, It would definitely save a step.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        DataTable is an object in memory, no need for a database.

        Comment

        • crusson
          New Member
          • Mar 2008
          • 18

          #5
          DataTable was the keyword. Thanks a bunch.

          Comment

          • crusson
            New Member
            • Mar 2008
            • 18

            #6
            So using a DataTable will work,

            In order to create a table with an unknown number of rows, I need to loop to create Row objects. How do I create objects in a loop such that each has a different name. Or am I missing something and there is a better way to create a bunch of rows?

            my idea was soemthing along the lines of this...but &i doesn't work as i wished it did...
            [code=vbnet]
            Private Sub Thisarray()
            Dim table1 As DataTable
            table1 = New DataTable("stuf f")
            Try
            Dim colA As DataColumn = New DataColumn("a")
            colA.DataType = System.Type.Get Type("System.St ring")
            table1.Columns. Add(colA)
            Dim colB As DataColumn = New DataColumn("b")
            colB.DataType = System.Type.Get Type("System.St ring")
            table1.Columns. Add(colB)
            Dim colC As DataColumn = New DataColumn("c")
            colC.DataType = System.Type.Get Type("System.St ring")
            table1.Columns. Add(colC)


            Dim j As Integer
            For j = 1 To 10
            Dim row&i As DataRow
            row&i = table1.NewRow()
            row&i.Item("a") = "a"
            row&i.Item("b") = "b"
            row&i.Item("c") = "c"
            table1.Rows.Add (row&i)
            Next

            Catch
            End Try

            Dim ds As New DataSet()
            ds = New DataSet()
            ds.Tables.Add(t able1)
            DataGrid1.SetDa taBinding(ds, "letters")

            End Sub
            [/code]
            Last edited by Plater; Mar 13 '08, 12:56 PM. Reason: code tags

            Comment

            • saran23
              New Member
              • Mar 2008
              • 28

              #7
              Originally posted by crusson
              So using a DataTable will work,

              In order to create a table with an unknown number of rows, I need to loop to create Row objects. How do I create objects in a loop such that each has a different name. Or am I missing something and there is a better way to create a bunch of rows?

              my idea was soemthing along the lines of this...but &i doesn't work as i wished it did...

              Private Sub Thisarray()
              Dim table1 As DataTable
              table1 = New DataTable("stuf f")
              Try
              Dim colA As DataColumn = New DataColumn("a")
              colA.DataType = System.Type.Get Type("System.St ring")
              table1.Columns. Add(colA)
              Dim colB As DataColumn = New DataColumn("b")
              colB.DataType = System.Type.Get Type("System.St ring")
              table1.Columns. Add(colB)
              Dim colC As DataColumn = New DataColumn("c")
              colC.DataType = System.Type.Get Type("System.St ring")
              table1.Columns. Add(colC)


              Dim j As Integer
              For j = 1 To 10
              Dim row&i As DataRow
              row&i = table1.NewRow()
              row&i.Item("a") = "a"
              row&i.Item("b") = "b"
              row&i.Item("c") = "c"
              table1.Rows.Add (row&i)
              Next

              Catch
              End Try

              Dim ds As New DataSet()
              ds = New DataSet()
              ds.Tables.Add(t able1)
              DataGrid1.SetDa taBinding(ds, "letters")

              End Sub
              I suggest using the Datatable u created as the datasource for the grid.instead of using the dataset.

              just try this

              Code:
              Datagrid.Datasource=Datatablename;

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by crusson
                So using a DataTable will work,

                In order to create a table with an unknown number of rows, I need to loop to create Row objects. How do I create objects in a loop such that each has a different name. Or am I missing something and there is a better way to create a bunch of rows?

                my idea was soemthing along the lines of this...but &i doesn't work as i wished it did...
                When it comes to that sort of thing, you don't NEED different names for them, the variable name you assign goes out of scope the next itteration through the loop.

                Simply doing this should be fine:
                [code=vbnet]
                Dim j As Integer
                For j = 1 To 10
                Dim row As DataRow
                row = table1.NewRow()
                row.Item("a") = "a"
                row.Item("b") = "b"
                row.Item("c") = "c"
                table1.Rows.Add (row)
                Next
                [/code]

                Comment

                • crusson
                  New Member
                  • Mar 2008
                  • 18

                  #9
                  So I am now able to display the DataGrid with all of my data.

                  I want to set individual column widths, but code such as:
                  colB.width = 100 does not work, i think maybe because I'm running vb.net 1.0?

                  I can set a datagridcolumns tyle, but how do i apply this to a column?

                  [code=vbnet]
                  Dim styleA As DataGridColumnS tyle
                  styleA.Width() = 1200
                  [/code]

                  [code=vbnet]
                  Private Sub displayArray()

                  arrayMasterLen = terrarray.getDi splayArrayLen()

                  Dim dataTable1 As DataTable
                  dataTable1 = New DataTable("Line Log")
                  Try
                  Dim colA As DataColumn = New DataColumn("Dat e/Time")
                  colA.DataType = System.Type.Get Type("System.St ring")
                  dataTable1.Colu mns.Add(colA)
                  Dim colB As DataColumn = New DataColumn("Fla g")
                  colB.DataType = System.Type.Get Type("System.St ring")
                  dataTable1.Colu mns.Add(colB)
                  Dim colC As DataColumn = New DataColumn("Cod e")
                  colC.DataType = System.Type.Get Type("System.St ring")
                  dataTable1.Colu mns.Add(colC)
                  Dim colD As DataColumn = New DataColumn("Com ments")
                  colD.DataType = System.Type.Get Type("System.St ring")
                  dataTable1.Colu mns.Add(colD)

                  Dim j As Integer = 0
                  For j = 0 To arrayMasterLen - 1

                  Dim row As DataRow
                  row = dataTable1.NewR ow()
                  row.Item("Date/Time") = terrarray.retur nDateEntry(j)
                  row.Item("Flag" ) = terrarray.retur nFlag(j)
                  row.Item("Code" ) = terrarray.retur nCode(j)
                  row.Item("Comme nts") = terrarray.retur nComment(j)
                  dataTable1.Rows .Add(row)
                  Next

                  Catch
                  End Try

                  DataGrid1.DataS ource = dataTable1
                  DataGrid1.Refre sh()

                  End Sub[/CODE]

                  I also want to change the select method so that when I click anywhere in a row, the whole row becomes highlighted. Is this a simple property, or do I need to code this somehow?
                  Last edited by crusson; Mar 13 '08, 10:25 PM. Reason: code tag

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Your datagrid columns should have a .style or DefaultCellStyl e or something like that as a property already, you can change them withen there.

                    Comment

                    • crusson
                      New Member
                      • Mar 2008
                      • 18

                      #11
                      I moved on to different parts of the program hoping that when I came back to this problem I would magically think of a way to fix it...but I'm still having the same issues.

                      I am unable to size my columns to the width I want in the datagrid. I am able to resize all columns together, but not as seperate entities. I want column d to be really wide, but a,b,c can all be fairly small.

                      I am unable to figure this out. There is no .style or DefaultCellStyl e that I can access to change individual Columns. Can anyone throw me a few hints here?

                      Code:
                          Private Sub displayArray()
                              'displays the array in the data grid form
                      
                              arrayMasterLen = terrarray.getDisplayArrayLen()     'the number of rows to be displayed
                              ReDim arrayMaster(arrayMasterLen)                   'dim the array it's stored to
                              arrayMaster = terrarray.getDisplayArray()           'store the array
                      
                              Dim dataTable1 As DataTable                         'new data table
                              dataTable1 = New DataTable("LineLog")
                              Try
                                  Dim colA As DataColumn = New DataColumn("Date/Time")
                                  colA.DataType = System.Type.GetType("System.String")
                                  dataTable1.Columns.Add(colA)
                      
                      
                                  Dim colB As DataColumn = New DataColumn("Flag")
                                  colB.DataType = System.Type.GetType("System.String")
                                  dataTable1.Columns.Add(colB)
                                  Dim colC As DataColumn = New DataColumn("Code")
                                  colC.DataType = System.Type.GetType("System.String")
                                  dataTable1.Columns.Add(colC)
                                  Dim colD As DataColumn = New DataColumn("Comments")
                                  colD.DataType = System.Type.GetType("System.String")
                                  dataTable1.Columns.Add(colD)
                      
                      
                                  Dim j As Integer = 0
                                  For j = 0 To arrayMasterLen - 1
                                      Dim row As DataRow
                                      row = dataTable1.NewRow()
                                      row.Item("Date/Time") = terrarray.returnDateEntry(j)
                                      row.Item("Flag") = terrarray.returnFlag(j)
                                      row.Item("Code") = terrarray.returnCode(j)
                                      row.Item("Comments") = terrarray.returnComment(j)
                                      dataTable1.Rows.Add(row)
                                  Next
                              Catch
                              End Try
                      
                              DataGrid1.PreferredColumnWidth() = 120
                              DataGrid1.DataSource = dataTable1
                              DataGrid1.RowHeadersVisible = True
                              DataGrid1.Name = "LineEntries"
                              DataGrid1.Refresh()
                      
                          End Sub

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Hmm, while there does not seem to be any width parameter, all DataGridViewCol umns have that style element I was talking about.
                        (There's two, one for the header cell and one for the regular cells)

                        There is also the auto-size mode, which might do what you want it to do?

                        Hmm, this IS a windows application right and not for a webapplication?

                        Comment

                        • crusson
                          New Member
                          • Mar 2008
                          • 18

                          #13
                          Thanks for replying. I feel like kind of a moron, but I'm still unable to figure this out. This is a windows app, not a web app. I think part of my problem is that I cannot figure out how to refer to the columns of the actual data grid. I can refer to columns of the data table, but cannot do anything with them width-wise. I think it may be that I'm applying a data table to be the data source of the data grid and cannot refer to those columns once they've been added to the data grid. Is there any way to refer to these columns in the datagrid?

                          I am able to set the preferred width of the datagrid (which applies to all columns in the table) but cannot find the auto-size mode you speak of. Should it appear in my properties window and doesn't? Or should I need to code it after adding the data to the datagrid. If it just automatically sizes every column to its widest width, that would be perfect.

                          Originally posted by Plater
                          Hmm, while there does not seem to be any width parameter, all DataGridViewCol umns have that style element I was talking about.
                          (There's two, one for the header cell and one for the regular cells)

                          There is also the auto-size mode, which might do what you want it to do?

                          Hmm, this IS a windows application right and not for a webapplication?

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Hmm you are using DataGrid object and not a DataGridView object then? That will be trickier then, I am unfamiliar with the vs2003 version of the object.

                            Comment

                            • crusson
                              New Member
                              • Mar 2008
                              • 18

                              #15
                              Yes, I am using a datagrid object, not a datagridview object. I've never used one of these objects before this project, so I have no real idea how they work. I know that generally they are used with databases instead of arrays. Would a database as an intermediary step be a necessary middleman?

                              Comment

                              Working...