如何在UWP C#中用DataTable内容填充DataGrid [英] How to fill DataGrid with DataTable content in UWP c#
问题描述
我正在创建UWP应用,并试图用包含来自数据库的数据的DataTable填充我的DataGrid,但没有成功。
我已经在寻找解决方案,但无法摆脱错误。
XAML代码:
< StackPanel>
< Button Content = Fill DataGrid Click = Button_Click />
< controls:DataGrid x:Name = dataGrid
Margin = 30
AutoGenerateColumns = True>
< / controls:DataGrid>
< / StackPanel>
C#代码:
私有静态DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add( ID,typeof(int));
dt.Columns.Add( FirstName,typeof(string));
dt.Columns.Add( LastName,typeof(string));
dt.Columns.Add( Address,typeof(string));
dt.Columns.Add( City,typeof(string));
for(int i = 0; i< 10; i ++)
dt.Rows.Add(i, text, text, text, text) ;
return dt;
}
private void Button_Click(object sender,RoutedEventArgs e)
{
DataTable dt = GetDataTable();
dataGrid.ItemsSource = dt.DefaultView;
}
通过使用DataTable中的实体创建一个List并添加它们,我设法实现了一种解决方法作为dataGrid的来源,但是问题是我有很多不同的DataGrid,如果我能以某种方式解决该问题,就像使用带dataGridView的Windows窗体中的示例那样,将容易得多。
我们将提供任何帮助。
由于此网站。
public static void FillDataGrid(DataTable table,DataGrid grid)
{
grid.Columns.Clear();
grid.AutoGenerateColumns = false;
for(int i = 0; i< table.Columns.Count; i ++)
{
grid.Columns.Add(new DataGridTextColumn()
{
标头= table.Columns [i] .ColumnName,
绑定=新绑定{路径=新PropertyPath( [ + i.ToString()+])}
});
}
var collection = new ObservableCollection< object>();
foreach(table.Rows中的DataRow行)
{
collection.Add(row.ItemArray);
}
grid.ItemsSource =集合;
}
也许不是解决此问题的最优雅方法,但它确实有效正是我想要的。
并且必须将dataGrid属性 AutoGenerateColums设置为 False。那给我带来了很多问题。
I'm creating a UWP app and trying to fill my DataGrid with a DataTable that contains data from my database, but with no success. I have already searched for solutions but just can't get rid of the error.
XAML code:
<StackPanel>
<Button Content="Fill DataGrid" Click="Button_Click"/>
<controls:DataGrid x:Name="dataGrid"
Margin="30"
AutoGenerateColumns="True">
</controls:DataGrid>
</StackPanel>
C# code:
private static DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("City", typeof(string));
for (int i = 0; i < 10; i++)
dt.Rows.Add(i, "text", "text", "text", "text");
return dt;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DataTable dt = GetDataTable();
dataGrid.ItemsSource = dt.DefaultView;
}
and this error 20 times (the table has 10 entries)
Error: BindingExpression path error: 'Item' property not found on 'System.Data.DataRowView'. BindingExpression: Path='Item' DataItem='System.Data.DataRowView'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
When i swap ItemSource with DataContext:
dataGrid.DataContext = dt.DefaultView;
I don't get any error but I also don't change the dataGrid in any way.
I have tried to do the same in windows forms with DataGridView and I have succeeded:
private void button_Click(object sender, EventArgs e)
{
DataTable dt = GetDataTableFromDatabase();
dataGridView1.DataSource = dt;
}
I get this so its not a problem with the database or the DataTable.
I have managed to achieve a workaround by creating a List with the entities from the DataTable and adding them as the source to the dataGrid, but the problem is that I have a lot of different DataGrids and it would be a lot easier if i could somehow solve the problem in a way similar to the example in windows forms with the dataGridView.
Any help is appreciated.
I figured it out thanks to this site.
public static void FillDataGrid(DataTable table, DataGrid grid)
{
grid.Columns.Clear();
grid.AutoGenerateColumns = false;
for (int i = 0; i < table.Columns.Count; i++)
{
grid.Columns.Add(new DataGridTextColumn()
{
Header = table.Columns[i].ColumnName,
Binding = new Binding { Path = new PropertyPath("[" + i.ToString() + "]") }
});
}
var collection = new ObservableCollection<object>();
foreach (DataRow row in table.Rows)
{
collection.Add(row.ItemArray);
}
grid.ItemsSource = collection;
}
Maybe not the most elegant way to solve this but it works, and it does exactly what i want.
And you have to set the dataGrid property "AutoGenerateColums" to "False". That was causing me a lot of problems.
这篇关于如何在UWP C#中用DataTable内容填充DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!