如何使用CodeBehind动态添加DataGrid [英] How to add a DataGrid Dynamically using CodeBehind
本文介绍了如何使用CodeBehind动态添加DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void button2_Click(object sender, RoutedEventArgs e)
{
connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Project/Book.accdb";
connection = new OleDbConnection(connectionstring);
sql = "Select*from Authors";
try
{
connection.Open();
oledbAdapter = new OleDbDataAdapter(sql, connection);
oledbAdapter.Fill(dt);
// dataGrid1.DataContext = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
DataGrid dg2 = new DataGrid();
dg2.Height = 200;
dg2.Width = 200;
dg2.AutoGenerateColumns = true;
dg2.AlternatingRowBackground = Brushes.Black;
dg2.Background = Brushes.Red;
dg2.BorderBrush = Brushes.Pink;
dg2.HorizontalGridLinesBrush = Brushes.Black;
dg2.DataContext = dt;
stackPanel1.Children.Add(dg2);
}
预期代码应显示带有其基础数据的DataGrid.但它没有发生.
它只给出红色背景.完全没有数据.也没有网格线.我很想念一些东西.请我帮忙
问候
伊克巴尔
注意:我刚刚开始学习
之后的
it is expected that the code should display the DataGrid with its underlying Data. but it is not happening.
it gives only a Red back ground. no data at all .No Grid lines also. I am missing something very seriously. please i request somebody help
Regards
Iqbal
NB: I am just started learning
推荐答案
stackPanel1.Children.Add(dg2);
使用
use
dg2.ItemsSource = Your Source
我认为这会有所帮助.
I think it can help.
这篇关于如何使用CodeBehind动态添加DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文