简体   繁体   English

C#DataGridView和MySQL

[英]c# datagridview and mysql

I am doing some c# and mysql and I was successful at getting mysql data into a grid view for the first time! 我正在做一些C#和mysql,并且第一次成功将mysql数据放入网格视图中! Now, my main question is, how do I manage the grid view style with this? 现在,我的主要问题是,如何以此管理网格视图样式? For example, say I have already created columns and such, how do I put the mysql data into a specific column in the grid view? 例如,假设我已经创建了列,这样,如何将mysql数据放入网格视图中的特定列?

Below is the code that is actually loading the data into the grid view. 下面是将数据实际加载到网格视图中的代码。

 try
            {
                conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
                conn.Open();
                // - DEBUG 
                // MessageBox.Show("Connection successful!"); 
                MySqlDataAdapter MyDA = new MySqlDataAdapter();
                MyDA.SelectCommand = new MySqlCommand("SELECT * FROM `swipes`", conn);
                DataTable table = new DataTable();
                MyDA.Fill(table);

                BindingSource bSource = new BindingSource();
                bSource.DataSource = table;

                dataGridView1.DataSource = bSource; 

            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                Close(); 
            }

Also, this creates columns based on the mysql data, how do I modify the width of these columns and such, or like stated above, use my own custom columns for my data? 另外,这会基于mysql数据创建列,如何修改这些列的宽度,或者像上面所说的那样,将自己的自定义列用于数据? I've never done any mysql work in any UI, so I'm open to suggestions and tutorials as well. 我从未在任何用户界面中完成任何mysql工作,因此也欢迎提出建议和教程。 Thanks in advance! 提前致谢!

If you really want to do this (as someone has already stated you should look at other options) you can create the columns in the designer and set the DataGridViewColumn.DataPropertyName on each column to the columns returned by the autogenerated dataset. 如果您确实要执行此操作(就像有人已经说过的那样,您应该查看其他选项),则可以在设计器中创建列,并将每个列上的DataGridViewColumn.DataPropertyName设置为自动生成的数据集返回的列。 Remember to turn of autogeneration of columns ( AutoGenerateColumns ) on the grid. 切记要在网格上自动生成列( AutoGenerateColumns )。 This way you have full control of the column styles. 这样,您可以完全控制列样式。

try this 尝试这个

string connection = "server=localhost;database=adil;user=root;password=";
        MySqlConnection con = new MySqlConnection(connection);
        con.Open();
        MySqlCommand command = new MySqlCommand();

        command.Connection = con;
        MySqlDataAdapter MyDA = new MySqlDataAdapter();
        string sqlSelectAll = "SELECT * from studentrec";
        MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, con);

        DataTable table = new DataTable();
        MyDA.Fill(table);

        BindingSource bSource = new BindingSource();
        bSource.DataSource = table;


        dataGridView1.DataSource = bSource;

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

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