我是WPF的新手,我想将SQL查询中的数据可视化为WPF DataGrid。 我有一个如何正确绑定数据的问题:
SqlCommand cmd = new SqlCommand(sql_dotaz, conn);
InfoText.Text += ("Příkaz vytvořen a připojen" + "\n");
try
{
conn.Open();
InfoText.Text += ("Připojení otevřeno" + "\n");
SqlDataReader reader = cmd.ExecuteReader();
int row_count = reader.FieldCount;
ArrayList propoj = new ArrayList();
for (int i = 0; i < row_count; i++)
{
propoj.Add(reader.GetName(i));
tabView.Columns.Add(new DataGridTextColumn
{
Header = reader.GetName(i),
Binding = new Binding(reader.GetName(i))
});
//Here is the problem!!!
tabView.Items.Add(new {propoj[i] = "Hello"});
}
问题是当尝试添加新项目时,它会引发错误。我无法像这样明确设置标题名称(Invitation =“Hello”)。
I also tried
tabView.Columns.Add(new DataGridTextColumn
{
Header = reader.GetName(i),
Binding = new Binding(reader.GetName(i))
});
string record = reader.GetName(i));
tabView.Items.Add(new {record = "Hello"});
但标题名称仍有问题 - DataGrid为空。
如果您有任何想法,请告诉我。 非常感谢!
答案 0 :(得分:1)
首先,我认为匿名类型不会在绑定中起作用。你将以某种方式为此创建一个包装器对象:
// easiest case
public class DatabaseObject
{
public List<string> Items = new List<string>();
}
通过它,您可以轻松生成列:
// this should be renamed in something like columns or fieldCount...
int row_count = reader.FieldCount;
for (int i = 0; i < row_count; i++)
{
tabView.Columns.Add(new DataGridTextColumn
{
Header = reader.GetName(i),
Binding = new Binding("Items[i]")
});
}
填写你的DataGrid:
if(reader.HasRows)
{
do
{
DatabaseObject obj = new DatabaseObject();
for (int i = 0; i < row_count; i++)
{
obj.Items.Add(reader.GetString(i));
}
tabView.Items.Add(obj);
}
while(reader.NextResult());
}
答案 1 :(得分:0)
答案 2 :(得分:0)
如果想要轻松的方式:
只需绑定从datagrid到数据表的itemssource。 (阅读有关DataBinding和MVVM的信息)
<DataGrid ItemsSource="{Binding MyFilledDataTableProperty}" />
设置正确的datacontext。
答案 3 :(得分:0)
在我找到解决方案。也许这不是最好的,但它的工作。 我创建了DataSet和SQL Adapter
adapter.Fill(dSet);
this.DataContext = dSet.Tables[0].DefaultView;
并在XAML中:
<DataGrid Height="Auto" Width="Auto" Name="tabView3" ItemsSource="{Binding}" />
现在工作正常: - )
答案 4 :(得分:-1)
这对我有用:
在XAML文件中:
<DataGrid Name="YourDataGrid" CanUserAddRows="true" AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected" Width="auto" SelectionUnit="FullRow" RowEditEnding="YourDataGrid_RowEditEnding" AddingNewItem="YourDataGrid_AddingNewItem">
<DataGrid.Columns>
<DataGridTextColumn x:Name="VideoID_column" Binding="{Binding IDVideo, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Video ID" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="VideoTitle_column" Binding="{Binding Titlu,NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Title" Width="auto"/>
</DataGrid.Columns>
</DataGrid>
在.CS文件中
private void YourDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit && isInsertMode == true)
{
var vid = e.Row.DataContext as video;
var context = new YourEntities();
var Video = new video //video is the class
{
IDVideo = vid.IDVideo,
Titlu = vid.Titlu
};
context.videos.Add(Video);
context.SaveChanges();
}
}
private void YourDataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
isInsertMode = true;
}
希望有帮助!