Converting WinForms to WPF - Unbound Datagrid?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Due to the previous issue I had where the Designer lost most of my WinForms design, I decided since Im rebuilding my forms from scratch anyway to convert to WPF. Most of my vbcode was intact and was able to copy to the new project with relative ease once
I figured out a couple differences like .Enabled -> .IsEnabled and .Text -> .Content, and figured out how to replace the Timers with DispatcherTimers... Im sure as I go through updates and other changes Ill slowly get things more streamlined with newer
items available in .net4/WPF, but the one thing I havent been able to figure out is how to work with an unbound DataGrid. I have columns manually added from the XAML designer, but adding, removing and reading Rows from the VB side has me baffled. ".Rows"
doesnt appear to be a member of DataGrid as it was with DGVs, and no other member I can find has an Add function. Ive also come up empty looking in the help for DG on MSDN, and any other sites I find either deals directly with bound data grids or use other
members I cant find like "RowsHierarchy"...
My DataGrid has 4 columns as follows:<br/>
TextBox column: Path & File Name of a sound file, Hidden<br/>
TextBox column: File Name only, Visible<br/>
TextBox column: Length of a sound file in seconds, Hidden<br/>
TextBox column: Formatted "time" of sound file (m:ss), Visible
Some of the actions Im trying to perform with this DataGrid are as follows:
<pre class="prettyprint lang-vb Private Sub FileAdd(Filename As String)
Code
DataGridView1.Rows.Add(Filename, IO.Path.GetFileNameWithoutExtension(Filename), dxListPlay.Duration, TimeSpan.FromSeconds(Math.Round(dxListPlay.Duration, 0)))
More Code
End Sub

Private Sub tottimecalc()
Dim total As Double = 0
For Each r As DataGridViewRow In DataGridView1.Rows
Dim cellvalue As Double
If Double.TryParse(CStr(r.Cells(2).Value), cellvalue) Then total += cellvalue
Next
More Code
End Sub

Private Sub MoveRow(ByVal i As Integer)
If (DataGridView1.SelectedCells.Count > 0) Then
Dim curr_index As Integer = DataGridView1.CurrentCell.RowIndex
Dim curr_col_index As Integer = DataGridView1.CurrentCell.ColumnIndex
Dim curr_row As DataGridViewRow = DataGridView1.CurrentRow
DataGridView1.Rows.Remove(curr_row)
DataGridView1.Rows.Insert(curr_index + i, curr_row)
DataGridView1.CurrentCell = DataGridView1(curr_col_index, curr_index + i)
End If
End Sub

Private Sub btn_RemList_Click(sender As System.Object, e As System.EventArgs) Handles btn_RemList.Click
If (DataGridView1.SelectedCells.Count > 0) Then
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
End If
End Sub

Private Sub btn_ClearList_Click(sender As System.Object, e As System.EventArgs) Handles btn_ClearList.Click
DataGridView1.Rows.Clear()
End Sub

Private Sub btn_PlayList_Click(sender As System.Object, e As System.EventArgs) Handles btn_PlayList.Click
If DataGridView1.Rows.Count > 0 Then
lbl_NowPlaying.Text = DataGridView1.Rows(0).Cells(1).Value
dxListPlay = New Audio(DataGridView1.Rows(0).Cells(0).Value)
Other Code
End If
End Sub[/code]
<br/>
Any help on getting this resolved would be appreciated. This is the only thing standing in the way of a working program again that I can get back to improving instead of desperately trying to get working in the first place. (I know Ive sucked at nominating/marking
answers in the past, I promise Ill be better but youve gotta give me time to test answers.)

View the full article
 
Back
Top