Category Archives: VB.NET

DataRowView RowState Unchanged {2} After Changing Item Value

Came across a very perplexing .NET behavior as I was working with a databound ListBoxControl.

I have multiselect enabled (SelectionMode = Multiextended), and the task was to loop through all list items, insert a new item in database after the last selected item in listbox, and write the new order index back to the database as we loop.

I am using the tableadapter.Update() method and testing return value of 1 for successful update. For some reason it updates fine up until selected item in the listboxcontrol (talk about weird!), and upon further digging I found the RowState isn’t changed after setting the new order index value to my data field: rowstate unchanged

Further research suggests I’m not the only one who came upon this weird behavior. See:

The solution is to call EndEdit() method on the DataRowView to force RowState to Modified so the subsequent TableAdapter.Update() call will actually perform an update to the database: rowstate modified after calling endedit

Hex ASCII String to Byte Array

Here is a function that takes in a string of Hex and converts to byte array.

For example:

[code]Dim bytes = HexStringToBytes(“ABCD”)[/code]

bytes will now contain 2 elements, first with value of 0xAB (or 171 decimal) and second with value of 0xCD (or 205 decimal):

Here is the function:

[code] ‘convert hex string to byte array (taking 2 chars at a time)
Public Function HexStringToBytes(ByVal hex As String) As Byte()
Dim NumChars As Integer = hex.Length
Dim bytes((NumChars / 2) – 1) As Byte
Dim index As Integer
For index = 0 To NumChars – 1 Step 2
bytes(Math.Floor(index / 2)) = Convert.ToByte(hex.Substring(index, 2), 16)
Return bytes
End Function[/code]

It’s probably a good idea that you implement your own validation that the input needs to be multiples of 2, otherwise an exception will occur.

VB.NET OpenFileDialog RestoreDirectory for each dialog

Here is a code snippet (VB.NET 2005) that demonstrates how you can implement a separate RestoreDirectory for each of the OpenFileDialog’s you have.

Basically what this does is it allows you to have multiple “Browse” dialogs that each remember their own last directory used.

I have added comments to Button1_Click code only as Button2_Click code is pretty much the same:
[code]Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
‘declare a new instance of OpenFileDialog
Dim diag As New OpenFileDialog

‘set initial directory to be shown to button’s tag value.
‘this is initially blank
diag.InitialDirectory = Button1.Tag

‘sets button’s tag to directory of the file selected
If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then
Button1.Tag = System.IO.Path.GetDirectoryName(diag.FileName)
End If

‘output last directory shown on a label
Label1.Text = Button1.Tag
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim diag As New OpenFileDialog
diag.InitialDirectory = Button2.Tag
If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then
Button2.Tag = System.IO.Path.GetDirectoryName(diag.FileName)
End If
Label2.Text = Button2.Tag
End Sub
End Class[/code]
Now, assuming that you will be putting a button to show the openfilediaglog, and assuming you will have a button (or some kind of control) for each of the openfiledialog, we can utilize the button’s or control’s .Tag property to store the directory last shown.

You may download the demo project to see this in action:
OpenFileDialog InitialDirectory Demo