Brothernod
asked on
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").Ne wRow
For i = 0 To binData.Length - 1 Step 8
'Dim row As DataRow = dsData.Tables("Binary").Ne wRow()
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").Ro ws.Add(row )
Next i
DataGrid1.DataSource = dstData
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").Ne
For i = 0 To binData.Length - 1 Step 8
'Dim row As DataRow = dsData.Tables("Binary").Ne
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").Ro
Next i
DataGrid1.DataSource = dstData
also what is in bindata (because I think your loop won't work like you want it to)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
binData is a very long string of arbitrary length with binary data.
binData = "0010101101010111101010010 1010111001 10000" 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.
binData = "0010101101010111101010010
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.
ASKER
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
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
You mean the columns of a datagrid ?
ASKER
Yes, the column widths.
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.MappingNa me = dstdata.tablename
datagrid1.TableStyles.Add( myGridTabl eStyle)
myGridTableStyle.GridColum nStyles(0) .Width = 20 ' first column 20
myGridTableStyle.GridColum nStyles(1) .Width = 30 'second etc...
myGridTableStyle.GridColum nStyles(2) .Width = 40
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.MappingNa
datagrid1.TableStyles.Add(
myGridTableStyle.GridColum
myGridTableStyle.GridColum
myGridTableStyle.GridColum
ASKER
"Index was out of range. Must be non-negative and less than the size of the collection." I got this on myGridTableStyle.GridColum nStyles(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.
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.
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
ASKER
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.MappingNa me = dstData.TableName
DataGrid1.TableStyles.Add( myGridTabl eStyle)
myGridTableStyle.GridColum nStyles(0) .Width = 5 ' first column 20
myGridTableStyle.GridColum nStyles(1) .Width = 5 'second etc...
myGridTableStyle.GridColum nStyles(2) .Width = 5
myGridTableStyle.GridColum nStyles(3) .Width = 5
myGridTableStyle.GridColum nStyles(4) .Width = 5
myGridTableStyle.GridColum nStyles(5) .Width = 5
myGridTableStyle.GridColum nStyles(6) .Width = 5
myGridTableStyle.GridColum nStyles(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(bytImageBin ary)
pbxEncoded.Image = bmpEncoded
End Sub
Dim strData As String = txtData.Text
Dim binData As String = ""
Dim asciiData() As Byte
asciiData = System.Text.Encoding.ASCII
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.MappingNa
DataGrid1.TableStyles.Add(
myGridTableStyle.GridColum
myGridTableStyle.GridColum
myGridTableStyle.GridColum
myGridTableStyle.GridColum
myGridTableStyle.GridColum
myGridTableStyle.GridColum
myGridTableStyle.GridColum
myGridTableStyle.GridColum
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
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(bytImageBin
pbxEncoded.Image = bmpEncoded
End Sub
ASKER
Oops, ignore btnEncode_Click
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
Dim dstData As New DataTable("Binary")
dstData.Columns.Add("1",ge
dstData.Columns.Add("2",ge
dstData.Columns.Add("3",ge
dstData.Columns.Add("4",ge
dstData.Columns.Add("5",ge
dstData.Columns.Add("6",ge
dstData.Columns.Add("7",ge
dstData.Columns.Add("8",ge
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