Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with Datasets and Datagrids and Adding Rows

Posted on 2005-04-18
13
Medium Priority
?
427 Views
Last Modified: 2011-09-20
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
Comment
Question by:Brothernod
  • 7
  • 6
13 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13804412
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13804420
also what is in bindata (because I think your loop won't work like you want it to)
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 2000 total points
ID: 13804426
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Brothernod
ID: 13809669
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
 
LVL 1

Author Comment

by:Brothernod
ID: 13810187
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13813253
You mean the columns of a datagrid ?
0
 
LVL 1

Author Comment

by:Brothernod
ID: 13813279
Yes, the column widths.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13813336
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
 
LVL 1

Author Comment

by:Brothernod
ID: 13813398
"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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13813423
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
 
LVL 1

Author Comment

by:Brothernod
ID: 13813506
 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
 
LVL 1

Author Comment

by:Brothernod
ID: 13813513
Oops, ignore btnEncode_Click
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13813661
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question