I am quite a newbie with wpf...any help will be appreciated. I started a small project with a listview that displays content from MySQL. So far I had no problems except a column that has 2 items in it. I need to separate each item in its own column. It was easy to do with date and time but this one is beyond my skills.
The display of the listview is like that (I can't post images yet):
Date |Time |CallerID |From|To |Duration
10 June 2015 |22:45|"alex" <210555555>|101 |201|234
The CallerID
column contains the two values with distinct "" and <>. I need to separate as I did with Date
and Time
. Thanks for any help.
<ListView x:Name="Datalist" Grid.Column="1" Grid.Row="4"
ItemsSource="{Binding Path=DS}" Background="White" Foreground="Black" FontSize="16" Grid.ColumnSpan="4" FontFamily="Segoe UI" Margin="1,0,8,0">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=calldate,StringFormat={}{0:dd MMMM yyyy}}"/>
<GridViewColumn Header="Time" DisplayMemberBinding="{Binding Path=calldate,StringFormat={}{0:HH:mm:ss}}"/>
<GridViewColumn Header="CallerID" DisplayMemberBinding="{Binding Path=clid}"/>
<GridViewColumn Header="From" DisplayMemberBinding="{Binding Path=src}"/>
<GridViewColumn Header="To" DisplayMemberBinding="{Binding Path=dst}"/>
<GridViewColumn Header="Duration" DisplayMemberBinding="{Binding duration}" />
</GridView>
</ListView.View>
</ListView>
private void OnLoad(object sender, RoutedEventArgs e)
{
string cs = @"server=192.168.1.123;userid=alex;
password=r3s3ll3r;database=asteriskcdrdb";
MySqlConnection conn = null;
MySqlDataReader rdr = null;
try
{
conn = new MySqlConnection(cs);
conn.Open();
string stm = "(SELECT * FROM cdr ORDER BY uniqueid DESC LIMIT 1000)";
mySqlDataAdapter = new MySqlDataAdapter(stm, cs);
mySqlDataAdapter.Fill(DS);
Datalist.ItemsSource = DS.DefaultView;
}
catch (MySqlException ex)
{
MessageBox.Show("Error: {0}", ex.ToString());
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
Consider moving towards a more complete view model approach. Instead of binding your
ItemsSource
to theDataTable.DefaultView
directly, you could create a collection of view models that define the presentation logic for each entry. For example, you could define a view model class such asThis will open up a lot of options for dealing with your current problem. For example, how could we split up that caller ID string? You could create another view model, such as the following
Then the
PhoneCallViewModel
would expose aCallerIDViewModel
property. In yourOnLoad
method, you would create a collection of thesePhoneCallViewModel
objects (preferably anObservableCollection
) and bind theItemsSource
to it. Then you could simply replace your existing caller id column in yourListView
with two new columns. One bound toCallerID.CallerName
, and the second bound toCallerID.CallerNumber
(assuming the property onPhoneCallViewModel
is namedCallerID
).I've left out the details of splitting the original caller ID as well as instantiating the collection, as those are separate implementation problems.