在预定义列上将mysql添加到datagridview [英] Add mysql to datagridview on predefined column

查看:61
本文介绍了在预定义列上将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。

解决方案

您将数据表设置为数据源和它会在datagridview中添加字段。而不是这个使用for循环并填充datagridview中的数据。


看看有两种方法在datagridview中显示数据: -

1)通过设置datasource : - 通过设置数据源,datagridview获取所有列并按原样记录并将其添加到datagridview(注意: - 如果查询中有可用的req。字段,则无需添加列。)

$ / b $ b)通过循环: - 通过循环,您需要在运行之前添加列,而不是通过循环,您可以根据需要分配每个单元格的值。


< blockquote>

 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 is

Metabolite ID | Metabolites Name

and the dbSumMetabolite is my datagridview.

解决方案

you are setting datatable as data source and it would add the fields in datagridview. instead of this use "for loop" and fill data in datagridview.


see there are 2 ways of showing data in datagridview :-
1) by setting datasource :- by setting datasource , datagridview takes all the columns and record as it is and add it to datagridview (NOTE:- there is no need to add columns if you have req. fields available in the query).

2) by looping:- by looping you need to add columns before runtime and than through looping you can assign value of each cell according to your need.


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

}



reply if the code works


这篇关于在预定义列上将mysql添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆