c# - 从数据库获取值(value)并添加它的值(value)?

标签 c# mysql winforms datagridview

我有一个 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();

enter image description here

最佳答案

解决了!

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

关于c# - 从数据库获取值(value)并添加它的值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36057272/

相关文章:

c# - 添加 Controller 时无法获取元数据mvc4

java - 使用 Java 和 JDBC 将无符号 64 位数字插入 BigInt MySQL 列

c# - 更改月历大小

C# 强制表单焦点

c# - 带或不带 .ToString() 的字符串连接?

c# - C# 中的 Concat 数据绑定(bind)和静态文本

C# 在 asp.net 中禁用 GridView header

php - 在另一个sql语句中重用资源

mysql - 内部连接问题

c# - 如何以编程方式在 C# 中创建 Microsoft Access 数据库?