简体   繁体   English

如何用mysql数据库中的数据填充网格视图? (C#UWP)

[英]How to fill grid view with data from mysql database? (C# UWP)

I'm trying to write a simple UWP app in C#. 我试图用C#编写一个简单的UWP应用。 After clicking a button it should connect to MySQL remote database and post the result to grid view. 单击按钮后,它应该连接到MySQL远程数据库并将结果发布到网格视图。 As it has to be an UWP app, BindigSource and DataSource don't work. 由于它必须是UWP应用程序,因此BindigSource和DataSource不起作用。 What changes should I made to work that app in UWP? 我应该对UWP中的该应用程序进行哪些更改?

public sealed partial class MainPage : Page
{
    const string connString = "server=server_name; user id=uid; pwd=mypassword; persistsecurityinfo=True; database=mydb";

    string loadQuery = "SELECT * FROM table_name;";


    public MainPage()
    {
        this.InitializeComponent();
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MySqlConnection conn = new MySqlConnection(connString);
        conn.Open();

        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();

        mySqlDataAdapter.SelectCommand = new MySqlCommand(loadQuery, conn);

        DataTable dataTable = new DataTable();
        mySqlDataAdapter.Fill(dataTable);

        BindingSource bindingSource = new BindingSource();
        gridView1.DataSource = bindingSource;
        mySqlDataAdapter.Update(dataTable);

        conn.Close();
    }

} }

//Try this. //尝试这个。 It worked for me.. 它对我有用。

 public void getData()
       {
        const string connString = "YOUR CONNECTION SETTINGS HERE";
        Connection = new MySqlConnection(connString); 
        Connection.Open();
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter("YOUR QUERY HERE", Connection);
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        gridView1.DataSource = DS.Tables[0];
        Connection.Close();
        }

The UWP GridView is very different from the one in Windows Forms. UWP GridView与Windows窗体中的GridView非常不同。 While Windows Forms GridView is basically a table similar to Excel, in UWP it is a list of items displayed in a grid. 尽管Windows Forms GridView基本上是一个类似于Excel的表,但在UWP中,它是网格中显示的项目的列表。 There is still a concept of rows and columns, but the items themselves don't care about the row or column they are in and items in a row or in a column are not logically related. 仍然存在行和列的概念,但是项目本身并不关心它们所在的行或列,并且行或列中的项目在逻辑上并不相关。

Your use case requires a tabular display, which is not available in the UWP SDK out of the box. 您的用例需要表格显示,即装即用的UWP SDK中不提供。 Luckily however, you can find many great control on-line. 但是幸运的是,您可以在网上找到许多出色的控件。 Of the top of my mind you could look into the Telerik UI for UWP , specifically its Grid control . 在我的脑海中,您可以查看适用于UWPTelerik UI ,特别是其Grid控件 That should give you all you need to implement your scenario. 那应该给您实现方案所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM