【问题标题】:Getting value from database and add it's Value?从数据库中获取价值并添加它的价值?
【发布时间】:2016-03-17 10:05:15
【问题描述】:

我有一个 datagridview,我从 phpmyadmin 调用值并尝试添加它的值行,但是当我编辑单元格1 时,它得到相同的值

       MySqlConnection conn = new MySqlConnection("datasource=localhost;port = 3306;username = root;password = ");
        MySqlCommand comm = new MySqlCommand("select Fee,Amount from system.other_school_fees ;", conn);

            MySqlDataAdapter ssda = new MySqlDataAdapter();
            ssda.SelectCommand = comm;
            DataTable ddbdataset = new DataTable();
            ssda.Fill(ddbdataset);
            BindingSource bbsource = new BindingSource();

            bbsource.DataSource = ddbdataset;
            dataGridView2.DataSource = bbsource;
            ssda.Update(ddbdataset);
            dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            int d = 0;
            for (int b = 0; b < dataGridView2.Rows.Count; b++)
            {
                d += Convert.ToInt32(dataGridView2.Rows[b].Cells[1].Value);
            }

            lblOSF.Text = d.ToString();

【问题讨论】:

  • w8 应该是 MySqlConnection conn = new MySqlConnection("datasource=localhost;port = 3306;username = root;password = "); MySqlCommand comm = new MySqlCommand("select Miscellaneous_Fee,Amount from system.miscellaneoues;", conn);
  • 编辑单元格后是否有运行代码?不幸的是,它不会重新计算自己。

标签: c# mysql winforms datagridview


【解决方案1】:

解决了!

    private int CellSum()
    {
               string a = string.Empty;
            int sum = 0;
            for (int b = 0; b < dataGridView1.Rows.Count; ++b)
            {
                try
                {
                    int d = 0;
                    d += Convert.ToInt32(dataGridView1.Rows[b].Cells[1].Value);
                    sum += d;
                }
                catch 
                {
                    MySqlConnection conn = new MySqlConnection("datasource=localhost;port = 3306;username = root;password = ");
                    MySqlCommand comm = new MySqlCommand("select Miscellaneous_Fee,Amount from system.miscellaneoues;", conn);

                    MySqlDataAdapter ssda = new MySqlDataAdapter();
                    ssda.SelectCommand = comm;
                    DataTable ddbdataset = new DataTable();
                    ssda.Fill(ddbdataset);
                    BindingSource bbsource = new BindingSource();

                    bbsource.DataSource = ddbdataset;
                    dataGridView1.DataSource = bbsource;
                    ssda.Update(ddbdataset);
                    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

                    MessageBox.Show("Incorrect Input");
                }

            }
            return sum;     

    }

 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {

        if (e.ColumnIndex == 1)
            lblMiscellaneous.Text = CellSum().ToString();
    }

【讨论】:

    猜你喜欢
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2020-11-15
    • 1970-01-01
    • 2018-08-24
    • 2018-06-23
    • 1970-01-01
    相关资源
    最近更新 更多