重置绑定不起作用。 [英] Reset Binding is not working.

查看:86
本文介绍了重置绑定不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我在刷新数据集时遇到问题。这是我的代码。



Hi. I have a problem on refreshing the dataset. here is my code.

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();
        switch (cmbMetabolite.SelectedIndex)
        {
            case 0:
                dbSumMetabolite.ResetBindings();
                dbSumMetabolite.AutoGenerateColumns = false;
                command.CommandText = "SELECT MetaboliteID, Metabolite_Name FROM " + database +
                                      ".Metabolites " +
                                      "WHERE Metabolite ID IN ('met1', 'met2');";
                MySqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
                dtable.Load(dr);
                dbSumMetabolite.DataSource = null;
                for (int i = 0; i < dtable.Rows.Count; i++)
                {
                    dbSumMetabolite.Rows.Add();
                    dbSumMetabolite.Rows[dbSumMetabolite.Rows.Count - 1].Cells["MetaboliteID"].Value = dtable.Rows[i]["MetaboliteID"].ToString();
                    dbSumMetabolite.Rows[dbSumMetabolite.Rows.Count - 1].Cells["Metabolite_Name"].Value = dtable.Rows[i]["Metabolite_Name"].ToString();
                }
                break;

        }
        conn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}









我面临的问题是重置绑定不起作用。每当我更新数据时,datagridview将复制行而不是替换它。并且此代码插入在处理的事假事件





The problem i am facing is that the reset binding is not working. whenever i updating the the data the datagridview will duplicate the rows instead of replacing it. and this code is inserted in leavepage event handled

推荐答案

中写入dbSumMetabolite.DataSource = null;在代码的开头
write "dbSumMetabolite.DataSource=null;" at the starting of the code


抱歉写dbSumMetabolite.Rows.Clear();在代码的开头
sorry write "dbSumMetabolite.Rows.Clear();" at the starting of the code


这篇关于重置绑定不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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