【发布时间】:2013-12-19 11:52:06
【问题描述】:
我有以下代码,但我没有找到任何方法来添加来自用户的数据 到表。我想动态地做到这一点。
我创建了 WPF 应用程序并添加了数据网格和按钮,当我单击时 在按钮上查看数据网格上的数据,我应该如何进行?
private void Button_Click(object sender, RoutedEventArgs e)
{
//get user data...
DataTable dt = new DataTable("Users Info");
DataGrid.ItemsSource = dt.DefaultView;
foreach (var user in users)
{
string firstName = user.Firstname;
string lastName = user.Lastname;
}
Xaml 是:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTest01" x:Class="WPFTest01.MainWindow"
Title="WPF_DEMO_Main" Height="350" Width="525">
<Grid x:Name="Grid" Background="#FF1BA1E2">
<DataGrid x:Name="DataGrid" HorizontalAlignment="Left" Margin="65,60,0,0" VerticalAlignment="Top" Height="185" Width="385" SelectionChanged="DataGrid_SelectionChanged_1"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="390,280,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
</Window>
我已经尝试过使用其中的一些代码但没有成功:(
//dt.Columns.Add("First Name", typeof(string));
//dt.Columns.Add("Last Name", typeof(string));
//DataSet ds = new DataSet();
//ds.Tables.Add(dt);
【问题讨论】:
标签: c# wpf wpf-controls wpfdatagrid