本文介绍了在预定义列上将mysql添加到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 上。我是C#.NET和MySQL的初学者。我想知道如何将选定的sql语句显示到datagridview的特定列。到目前为止,这是我的代码。 public void BindingMetaboliteName(frmMetCon config) { schemaForm = new SchemaName(); string connStr = datasource = localhost;端口= 3306;用户名=根;密码=根;; conn = new MySqlConnection(connStr); command = conn.CreateCommand(); string database = schemaForm.getData; 尝试 { dtable = 新 DataTable (); bindingSource = new BindingSource(); conn.Open(); dbSumMetabolite.ResetBindings(); switch (cmbMetabolite.SelectedIndex) { case 0 : command.CommandText = SELECT MetaboliteID, Metabolite_Name FROM + database + 。代谢物 + WHERE MetaboliteID IN('met1','met2');; MySqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection); dtable.Load(dr); dbSumMetabolite.AutoGenerateColumns = false ; dbSumMetabolite.DataSource = dtable; break ; } conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 我在datagridview中定义的列是 代谢物ID |代谢物名称 和dbSumMetabolite是我的datagridview。解决方案 for(int i = 0; i< dtable.rows.count; i ++)> { dbSumMetabolite.Rows.Add(); dbSumMetabolite.Rows [dbSumMetabolite.Rows.Count - 1] .Cells [Metabolite ID]。Value = dtable.rows [i] [MetaboliteID]。ToString(); dbSumMetabolite.Rows [dbSumMetabolite.Rows.Count - 1] .Cells [Metabolite Name]。Value = dtable.Rows [i] [Metabolite_Name] .ToString(); } 如果代码有效,回复 Hai. I'm a beginner of C#.NET and MySQL. I would like to know how to display selected sql statement to specific column of datagridview. here is my code so far.public void BindingMetaboliteName(frmMetCon config) { schemaForm = new SchemaName(); string connStr = "datasource=localhost;port=3306;username=root;password=root;"; conn = new MySqlConnection(connStr); command = conn.CreateCommand(); string database = schemaForm.getData; try { dtable = new DataTable(); bindingSource = new BindingSource(); conn.Open(); dbSumMetabolite.ResetBindings(); switch (cmbMetabolite.SelectedIndex) { case 0: command.CommandText = "SELECT MetaboliteID, Metabolite_Name FROM " + database + ".Metabolites " + "WHERE MetaboliteID IN ('met1', 'met2');"; MySqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection); dtable.Load(dr); dbSumMetabolite.AutoGenerateColumns = false; dbSumMetabolite.DataSource = dtable; break; } conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }The column i defined in datagridview isMetabolite ID | Metabolites Nameand the dbSumMetabolite is my datagridview. 解决方案 这篇关于在预定义列上将mysql添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 00:46