如何在WPF应用程序中将MySQL表绑定到DataGrid [英] How to bind MySQL table to datagrid in WPF app

查看:411
本文介绍了如何在WPF应用程序中将MySQL表绑定到DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常遇到这个问题,但似乎找不到令人满意的答案.我在C#中有一个WPF应用程序,可以连接到远程MySQL数据库.我的目标是从数据库中选择一个表并将其呈现在数据网格中.但是之后,我希望能够打印选定的行(使用给定的报告定义文件).因此,我对一种填充数据网格(我知道所需的mysql语句)而不在本地存储mysql表的方法感兴趣,例如一个ObservableCollection.我只想查看所需表中的内容,以后便可以分别读取每条记录以填写模板(在rdlc文件中定义).如何才能做到这一点?我在MVVM体系结构中.非常感谢!

I've come across this question a lot, but I can't seem to find a satisfying answer. I have a WPF app in c# with a connection to a remote MySQL database. My goal is to select a table from the database and present it in a datagrid. But after this I want to be able to print a selected row (using given report definition files). So I'm interested in a way to populate my datagrid (I know the mysql statements needed) without storing the mysql table locally in, say an ObservableCollection. I just want to see what's in the desired table and later be able to read each record separately to fill out a template (defined in the rdlc file). How can this be done? I'm in MVVM architecture. Thanks a lot!

推荐答案

DataTable dt = new DataTable();
using (MySqlConnection conn = new MySqlConnection("Your connection string"))
{
    conn.Open();
    string query = "SELECT * FROM table";
    using (MySqlDataAdapter da = new MySqlDataAdapter(query, conn))
        da.Fill(dt);
}
yourDataGrid.ItemsSource = dt.DefaultView;

这是我知道的用数据库值填充DataGrid的代码.

This is the code i know to fill a DataGrid with database values.

对于打印,它有些先进.我认为您应该考虑参加Google会话.

As for printing, it's a bit mor advanced. I think you should consider a google session.

这篇关于如何在WPF应用程序中将MySQL表绑定到DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆