簡體   English   中英

C#DataGridView和MySQL

[英]c# datagridview and mysql

我正在做一些C#和mysql,並且第一次成功將mysql數據放入網格視圖中! 現在,我的主要問題是,如何以此管理網格視圖樣式? 例如,假設我已經創建了列,這樣,如何將mysql數據放入網格視圖中的特定列?

下面是將數據實際加載到網格視圖中的代碼。

 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(); 
            }

另外,這會基於mysql數據創建列,如何修改這些列的寬度,或者像上面所說的那樣,將自己的自定義列用於數據? 我從未在任何用戶界面中完成任何mysql工作,因此也歡迎提出建議和教程。 提前致謝!

如果您確實要執行此操作(就像有人已經說過的那樣,您應該查看其他選項),則可以在設計器中創建列,並將每個列上的DataGridViewColumn.DataPropertyName設置為自動生成的數據集返回的列。 切記要在網格上自動生成列( AutoGenerateColumns )。 這樣,您可以完全控制列樣式。

嘗試這個

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