Reputation: 265
I'm working on networking based application the application connects with a networking device using C# the front end is on WPF .The problem is i want to extract data after running particular command and after extraction i want it to display on DataGrid.The data is extracting correctly using regex as i need but the part that i want to show on Datagrid is not showing however it is showing on console correctly .Code is:
public class IPMAC
{
public string ip { get; set; }
public string mac { get; set; }
}
List<IPMAC> ipmac = new List<IPMAC>();
string pattern = @"(F8-F7-D3-00\S+)";
MatchCollection matches = Regex.Matches(stringData, pattern);
foreach (Match match in matches)
{
Console.WriteLine("Hardware Address : {0}", match.Groups[1].Value);
ipmac.Add(new IPMAC(){mac=match.Groups[1].Value});
}
string pattern2 = @"(192.168.1\S+)";
MatchCollection matchesIP = Regex.Matches(stringData, pattern2);
foreach (Match match in matchesIP)
{
Console.WriteLine("IP Address : {0}", match.Groups[1].Value);
ipmac.Add(new IPMAC() { ip = match.Groups[1].Value });
XAML is :
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid Name="dg" Grid.Row="0" Height="250" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Mac Addresses" Binding="{Binding Path=mac}"/>
<DataGridTextColumn Header="IP Addresses" Binding="{Binding Path=ip}"/>
</DataGrid.Columns>
</DataGrid>
In short I don't understand how to show output on datagrid as it is showing on Console .Please help??
Upvotes: 2
Views: 1187
Reputation: 89285
Simplest way to display your list of IPMAC
in DataGrid
is, by setting ItemsSource
from code after the list has been populated :
dg.ItemsSource = ipmac;
or you can use DataBinding
by following below steps :
DataContext
properly. Because data binding resolves binding path from current data context.ipmac
as public property of type ObservableCollection
. ObservableCollection
has built in mechanism to notify UI to refresh whenever item added to or removed from collection. And data binding can't work with member/field.ItemsSource
to ipmac
property. Snippet demonstrating above steps :
//declare ipmac as public property
public ObservableCollection<IPMAC> ipmac { get; set; }
//In constructor : initialize ipmac and set up DataContext
ipmac = new ObservableCollection<IPMAC>();
this.DataContext = this;
//In XAML : bind ItemsSource to ipmac
<DataGrid ItemSource="{Binding ipmac}" Name="dg" ... />
Upvotes: 1