• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

Help with Datasets and Datagrids and Adding Rows

Effectively I need to create a little excel like section on my form to display an array of data.  It makes things look clean.

I've got my string and I want to show it in 8 columns, 1 character per cell, for as many rows as it takes.

I tried the following code after reading other questions that were posted here and I get a compile error when trying to create row.  It says that "Object reference not set to an instance of an object."

Please help, i've never used any of this stuff before and just know basic vb.  Thank You.  I would like to move past this problem some time in the next day.


Here is my code







'Fill DataGrid
        Dim dsData As New DataSet
        Dim dstData As New DataTable("Binary")
        Dim dc1 As DataColumn = dstData.Columns("1")
        Dim dc2 As DataColumn = dstData.Columns("2")
        Dim dc3 As DataColumn = dstData.Columns("3")
        Dim dc4 As DataColumn = dstData.Columns("4")
        Dim dc5 As DataColumn = dstData.Columns("5")
        Dim dc6 As DataColumn = dstData.Columns("6")
        Dim dc7 As DataColumn = dstData.Columns("7")
        Dim dc8 As DataColumn = dstData.Columns("8")

        Dim row As DataRow = dsData.Tables("Binary").NewRow
        For i = 0 To binData.Length - 1 Step 8
            'Dim row As DataRow = dsData.Tables("Binary").NewRow()
            row(0) = binData.Substring(i, 1)
            row(1) = binData.Substring(i + 1, 1)
            row(2) = binData.Substring(i + 2, 1)
            row(3) = binData.Substring(i + 3, 1)
            row(4) = binData.Substring(i + 4, 1)
            row(5) = binData.Substring(i + 5, 1)
            row(6) = binData.Substring(i + 6, 1)
            row(7) = binData.Substring(i + 7, 1)
            dsData.Tables("Binary").Rows.Add(row)

        Next i

        DataGrid1.DataSource = dstData
0
Brothernod
Asked:
Brothernod
  • 7
  • 6
1 Solution
 
RonaldBiemansCommented:
you haven't added the columns to the table or the table to the dataset, (there is no need to create a dataset either)

        Dim dstData As New DataTable("Binary")
        dstData.Columns.Add("1",gettype(system.string))
        dstData.Columns.Add("2",gettype(system.string))
        dstData.Columns.Add("3",gettype(system.string))
        dstData.Columns.Add("4",gettype(system.string))
        dstData.Columns.Add("5",gettype(system.string))
        dstData.Columns.Add("6",gettype(system.string))
        dstData.Columns.Add("7",gettype(system.string))
        dstData.Columns.Add("8",gettype(system.string))

        Dim row As DataRow = dstdata.NewRow
        For i = 0 To binData.Length - 1 Step 8
            row(0) = binData.Substring(i, 1)
            row(1) = binData.Substring(i + 1, 1)
            row(2) = binData.Substring(i + 2, 1)
            row(3) = binData.Substring(i + 3, 1)
            row(4) = binData.Substring(i + 4, 1)
            row(5) = binData.Substring(i + 5, 1)
            row(6) = binData.Substring(i + 6, 1)
            row(7) = binData.Substring(i + 7, 1)
            dstData.Rows.Add(row)
        Next i
0
 
RonaldBiemansCommented:
also what is in bindata (because I think your loop won't work like you want it to)
0
 
RonaldBiemansCommented:
is it is a very long string where you want to put every 8 characters in the table then it should be changed to

        For i = 0 To binData.Length - 1 Step 8
           Dim row As DataRow = dstdata.NewRow
            row(0) = binData.Substring(i, 1)
            row(1) = binData.Substring(i + 1, 1)
            row(2) = binData.Substring(i + 2, 1)
            row(3) = binData.Substring(i + 3, 1)
            row(4) = binData.Substring(i + 4, 1)
            row(5) = binData.Substring(i + 5, 1)
            row(6) = binData.Substring(i + 6, 1)
            row(7) = binData.Substring(i + 7, 1)
            dstData.Rows.Add(row)
        Next i
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
BrothernodAuthor Commented:
binData is a very long string of arbitrary length with binary data.

binData = "0010101101010111101010010101011100110000" for example.

and yes I want to break it into 8 bits per row, 1 bit per column.

I tried your first set of code and it no longer complains about it not being an object but it tells me that row has already been added on the 2nd pass through the for loop.

How do I handle this?  I don't know how many rows there wil be and it will change with the input.
0
 
BrothernodAuthor Commented:
Okay, after playing a little I realized that your 2nd series of code fixed that for me.

Thanks so much you got the points :)


Any chance you could explain how to set column widths?  I know how many columns there will be and I want them all the same width if that effects anything.

Thanks
0
 
RonaldBiemansCommented:
You mean the columns of a datagrid ?
0
 
BrothernodAuthor Commented:
Yes, the column widths.
0
 
RonaldBiemansCommented:
something like this should do it

    Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle()
    'Map the style to the dataset table. In this case I only use one table
    ' in my query.
    myGridTableStyle.MappingName = dstdata.tablename
    datagrid1.TableStyles.Add(myGridTableStyle)
    myGridTableStyle.GridColumnStyles(0).Width = 20 ' first column 20
    myGridTableStyle.GridColumnStyles(1).Width = 30  'second etc...
    myGridTableStyle.GridColumnStyles(2).Width = 40


0
 
BrothernodAuthor Commented:
"Index was out of range. Must be non-negative and less than the size of the collection." I got this on myGridTableStyle.GridColumnStyles(0).Width = 5


I pasted your code after my     DataGrid1.DataSource = dstData  line

should I put it before?

Thanks for putting up with my total lack of understanding of this haha.
0
 
RonaldBiemansCommented:
No it should work like that (I tested it with your table). could you post the code you have now, so I can see if there is something else wrong
0
 
BrothernodAuthor Commented:
 Private Sub txtData_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtData.TextChanged
        Dim strData As String = txtData.Text
        Dim binData As String = ""
        Dim asciiData() As Byte
        asciiData = System.Text.Encoding.ASCII.GetBytes(strData)
        Dim i As Integer
        For i = 0 To (asciiData.Length - 1)
            binData = binData & ToBinary(asciiData(i))
        Next

        Dim dstData As New DataTable("Binary")
        dstData.Columns.Add("1", GetType(System.String))
        dstData.Columns.Add("2", GetType(System.String))
        dstData.Columns.Add("3", GetType(System.String))
        dstData.Columns.Add("4", GetType(System.String))
        dstData.Columns.Add("5", GetType(System.String))
        dstData.Columns.Add("6", GetType(System.String))
        dstData.Columns.Add("7", GetType(System.String))
        dstData.Columns.Add("8", GetType(System.String))

        'Dim row As DataRow = dstData.NewRow
        For i = 0 To binData.Length - 1 Step 8
            'For i = 0 To 8 - 1 Step 8
            Dim row As DataRow = dstData.NewRow
            row(0) = binData.Substring(i, 1)
            row(1) = binData.Substring(i + 1, 1)
            row(2) = binData.Substring(i + 2, 1)
            row(3) = binData.Substring(i + 3, 1)
            row(4) = binData.Substring(i + 4, 1)
            row(5) = binData.Substring(i + 5, 1)
            row(6) = binData.Substring(i + 6, 1)
            row(7) = binData.Substring(i + 7, 1)
            dstData.Rows.Add(row)
        Next i

        DataGrid1.DataSource = dstData

        Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
        myGridTableStyle.MappingName = dstData.TableName
        DataGrid1.TableStyles.Add(myGridTableStyle)
        myGridTableStyle.GridColumnStyles(0).Width = 5 ' first column 20
        myGridTableStyle.GridColumnStyles(1).Width = 5  'second etc...
        myGridTableStyle.GridColumnStyles(2).Width = 5
        myGridTableStyle.GridColumnStyles(3).Width = 5
        myGridTableStyle.GridColumnStyles(4).Width = 5
        myGridTableStyle.GridColumnStyles(5).Width = 5
        myGridTableStyle.GridColumnStyles(6).Width = 5
        myGridTableStyle.GridColumnStyles(7).Width = 5

    End Sub

    Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click
        Dim strImagePath As String = txtOriginalLocation.Text
        Dim bytImageBinary() As Byte
        Dim fs1 As IO.FileStream = New IO.FileStream(strImagePath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
        Dim br As IO.BinaryReader = New IO.BinaryReader(fs1)
        bytImageBinary = br.ReadBytes(fs1.Length)
        Dim i As Integer
        For i = 2000 To 2100
            bytImageBinary(i) = bytImageBinary(i) + 1
        Next i
        bmpEncoded = ImageFromArray(bytImageBinary)
        pbxEncoded.Image = bmpEncoded

    End Sub
0
 
BrothernodAuthor Commented:
Oops, ignore btnEncode_Click
0
 
RonaldBiemansCommented:
I just cut and pasted your code, and it works fine here. I just didn't use the textchanged event. So it has probable something to do with that. Since the table is always the same, create it outside of the textchanged event including the tablestyle of the grid, and just clear the table in  the textvhanged event like dstdata.clear
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now