使用c#在网格中获取passbok视图总数

时间:2017-03-07 17:00:57

标签: c# datagrid

我正在使用Data Grid来显示存折余额。我已经做了所有我无法在相应列中获得余额值的事情。

  private void dgview2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int credit1, debit1;
        int balance = new int();

        if (int.TryParse(dgview2.Rows[e.RowIndex].Cells["debit"].Value.ToString(),
            out debit1) && int.TryParse(dgview2.Rows[e.RowIndex].Cells["credit"].Value.ToString(), 
            out credit1) && int.TryParse(dgview2.Rows[e.RowIndex].Cells["balance"].Value.ToString(),
            out balance))
        {
              balance = balance + credit1 - debit1;
            dgview2.Rows[e.RowIndex].Cells["balance"].Value = balance.ToString();
        }

    }

Vfp9sp2 Application output

2 个答案:

答案 0 :(得分:0)

原因代码不起作用是网格是只读的,因此不会触发事件处理程序(dgview2_CellEndEdit)中指定的代码。

理想情况下,您应该在更新网格之前进行平衡计算,但作为一种快速解决方法,您可以将代码移动到这样的私有方法:

private void UpdateBalance()
{
    for (int i = 0; i < dgview2.Rows.Count; i++)
    {
        int credit1, debit1, balance;

        if (int.TryParse(dgview2.Rows[i].Cells[3].Value.ToString(),
                out debit1) && int.TryParse(dgview2.Rows[i].Cells[4].Value.ToString(),
                out credit1) && int.TryParse(dgview2.Rows[i].Cells[5].Value.ToString(),
                out balance))
        {
            balance = balance + credit1 - debit1;
            dgview2.Rows[i].Cells[5].Value = balance.ToString();
        }

    }
}

完成填充网格后调用此方法:

UpdateBalance();

该方法遍历每一行并使用相同的逻辑更新余额单元。

[编辑更新代码]

除了上述内容,您还可以在更新逻辑中执行计算,如下所示:

while (dr.Read())
{
    var n = dgview2.Rows.Add();
    dgview2.Rows[n].Cells[0].Value = Convert.ToDateTime(dr["tdate"].ToString());
    dgview2.Rows[n].Cells[1].Value = dr["particular"].ToString();
    dgview2.Rows[n].Cells[2].Value = dr["trmode"].ToString();
    dgview2.Rows[n].Cells[3].Value = dr["debit"].ToString();
    dgview2.Rows[n].Cells[4].Value = dr["credit"].ToString();
    dgview2.Rows[n].Cells[5].Value = dr["balance"].ToString();     

    int credit1, debit1, balance;

    if (int.TryParse(dr["debit"].ToString(),
        out debit1) && int.TryParse(dr["credit"].ToString(),
        out credit1) && int.TryParse(dr["balance"].ToString(),
        out balance))
    {
        dgview2.Rows[n].Cells[5].Value = balance + credit1 - debit1;
    }

    dgview2.FirstDisplayedScrollingRowIndex = n;
    dgview2.CurrentCell = dgview2.Rows[n].Cells[0];
}

答案 1 :(得分:0)

这是我用来填充网格的代码

  private void fill_div()
    {
        var com = new MySqlCommand("Select *From shtrn where mem_no ='" + textBox1.Text + "' order by tdate  ", con_db.con);
        var dr = com.ExecuteReader();
        try
        {
            dgview2.Rows.Clear();
            while (dr.Read())
            {
                var n = dgview2.Rows.Add();
                dgview2.Rows[n].Cells[0].Value = Convert.ToDateTime(dr["tdate"].ToString());
                dgview2.Rows[n].Cells[1].Value = dr["particular"].ToString();
                dgview2.Rows[n].Cells[2].Value = dr["trmode"].ToString();
                dgview2.Rows[n].Cells[3].Value = dr["debit"].ToString();
                dgview2.Rows[n].Cells[4].Value = dr["credit"].ToString();
                dgview2.Rows[n].Cells[5].Value = dr["balance"].ToString();
                dgview2.FirstDisplayedScrollingRowIndex = n;
                dgview2.CurrentCell = dgview2.Rows[n].Cells[0];
                UpdateBalance();
            }
            {

                dr.Close();
            }

        }
        catch (FormatException)
        {
            MessageBox.Show("No Records Found ");
        }
    }