简体   繁体   English

无法从 MySql 数据库填充数据网格视图

[英]Unable to populate Data Grid View from MySql database

Can anyone point out what I am doing wrong here?谁能指出我在这里做错了什么? I am connecting to my database without an issue and the query produces results in MySql workbench.我正在毫无问题地连接到我的数据库,并且查询在 MySql 工作台中产生结果。 However, I cannot get the results of the query into my dataGridView, dailyApptsDgv.但是,我无法将查询结果放入我的 dataGridView、dailyApptsDgv 中。

I have tried all 3 LoadDailyAppointments() individually, but none of them fill my dgv nor cause an error.我已经单独尝试了所有 3 个 LoadDailyAppointments(),但它们都没有填满我的 dgv,也没有导致错误。

       public Form()
       {
            InitializeComponent();
            CheckConnection();
            LoadDailyAppointments1()
            //LoadDailyAppointments2()
            //LoadDailyAppointments3()      
       }

       public void CheckConnection()
       {
           MySqlConnection con = new MySqlConnection(cs);
           try
           {
               con.Open();
               MessageBox.Show("Connection Open!");
               con.Close();
           }
           catch (Exception)
           {
               MessageBox.Show("No connection!");
           }
       }

       private void LoadDailyAppointments1() 
       {
           using (MySqlConnection con = new MySqlConnection(cs))
           {
               con.Open();
               MySqlCommand cmd = new MySqlCommand("Select * from Appointments", con);
               
               MySqlDataReader rdr = cmd.ExecuteReader();
               dailyApptsDgv.DataSource = rdr;
           }
       }

       private void LoadDailyAppointments2() 
       {
           using (MySqlConnection con = new MySqlConnection(cs))
           {
               con.Open();
               MySqlCommand cmd = new MySqlCommand("Select * from Appointments", con);
               dailyApptsDgv.DataSource = cmd.ExecuteReader();
           }
       }

       private void LoadDailyAppointments3()  
       {
           MySqlDataAdapter adp = new MySqlDataAdapter("Select * from Appointments", cs);
           DataSet dsDailyAppts = new DataSet();           
           adp.Fill(dsDailyAppts);
           
           dailyApptsDgv.DataSource = dsDailyAppts;     // I've tried running with and without this line 
       }

Form Designer.cs表单设计器.cs

            // dailyApptsDgv
            // 
            this.dailyApptsDgv.AllowUserToAddRows = false;
            this.dailyApptsDgv.AllowUserToDeleteRows = false;
            this.dailyApptsDgv.AllowUserToResizeRows = false;
            this.dailyApptsDgv.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dailyApptsDgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dailyApptsDgv.Cursor = System.Windows.Forms.Cursors.Default;
            this.dailyApptsDgv.Location = new System.Drawing.Point(263, 44);
            this.dailyApptsDgv.MultiSelect = false;
            this.dailyApptsDgv.Name = "dailyApptsDgv";
            this.dailyApptsDgv.ReadOnly = true;
            this.dailyApptsDgv.RowHeadersVisible = false;
            this.dailyApptsDgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dailyApptsDgv.ShowEditingIcon = false;
            this.dailyApptsDgv.Size = new System.Drawing.Size(629, 240);
            this.dailyApptsDgv.TabIndex = 15;
            // 

I have simplified my code and query as much as possible and still cannot get it to execute properly.我已经尽可能地简化了我的代码和查询,但仍然无法正常执行。

Any suggestions are appreciated!任何建议表示赞赏! Thank-you!谢谢!

DataTable dt = (DataTable)JsonConvert.DeserializeObject<DataTable>(JsonString);
dataGridView1.DataSource = dt;

My case just request to the API for data.我的案例只是向 API 请求数据。 it returns JSON I convert the JSON to Datatable and set to my DGV source and its working in my case.它返回 JSON 我将 JSON 转换为 Datatable 并设置为我的 DGV 源并在我的情况下工作。

****Just got the Data from the database and assign this to DGV source it will populate your DGV to populate the DGV you can also bind your Database Model to DGV in c#. ****刚刚从数据库中获取数据并将其分配给 DGV 源,它将填充您的 DGV 以填充 DGV 您还可以将数据库 Model 绑定到 c# 中的 DGV。 C# desktop application provides the DGV Functionality to bind it to the Database and this is easy for you to implement the real-time CRUD operation on the DGV. C# 桌面应用程序提供 DGV 功能将其绑定到数据库,这便于您在 DGV 上实现实时 CRUD 操作。

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

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