DataGridView的单元格编辑结束事件 [英] DataGridView cell edit end event

查看:481
本文介绍了DataGridView的单元格编辑结束事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView中,我有一列'总'。 DataGridView的是可编辑的真实。网格视图下面我文本中,我网格的总计栏总希望。我所做的是,当用户进入电网的总列,它反映到贝洛奥里藏特的网格视图总文本字段。在文本框中显示总,我添加了网格视图总列。但问题是,如果我第一次进入到网格视图总列,它会立即反映到下面的文本框。但如果我编辑DataGridView中的总列的值相同,网格下的文本框与我想要的文本字段的编辑后的新价值previous值添加它。 ?如何解决这个问题下面是code: -

 私人无效grdCaret_CellEndEdit(对象发件人,DataGridViewCellEventArgs E)
{
    尝试
    {
        字符串值= grdCaret.Rows [e.RowIndex] .Cells [e.ColumnIndex] .Value.ToString();
        如果(e.ColumnIndex == 1)
        {
           // INT VAL = int.Parse(值);
           //数量= VAL;
           // ekundag = ekundag +量;
           //tbTotDag_cr.Text = ekundag.ToString();

            INT量= 0;

            的foreach(的DataGridViewRow行grdCaret.Rows)
                量+ =(int)的grdCaret.Rows [e.RowIndex] .Cells [1] .Value.ToString();
                //量+ =(int)的row.Cells [1] .value的;

            tbTotDag_cr.Text = quantity.ToString();
        }

        如果(e.ColumnIndex == 2)
        {
            浮VAL = float.Parse(值);
            总= VAL;
            ekunrakam = ekunrakam +总和;
            tbTotPrice_cr.Text = ekunrakam.ToString();
        }
        grdCaret.Columns [3] .ReadOnly = FALSE;
    }
    赶上(例外前)
    {
        的MessageBox.show(ex.Message.ToString());
    }
}
 

解决方案

使用 CellEndEdit 事件来更新你的总价值:

 私人无效dataGridView_CellEndEdit(对象发件人,DataGridViewCellEventArgs E)
{
    INT总= 0;

    的foreach(的DataGridViewRow行dataGridView.Rows)
        共有+ =(int)的row.Cells [columnTotal.Index] .value的;

    totalTextBox.Text = total.ToString();
}
 

I have a DataGridView in which i have a column 'total'. DataGridView is editable true. below the grid view i have textbox in which i want the total of the 'total' column of grid. What i did is, when user enters into the total column in grid, it reflects into the total text field belo the grid view. to display total in the textfield, i added the total column of grid view. But the problem is, if first time i enter into the total column of grid view, it immediately reflects into the textfield below. but if i edit the same value in total column of DataGridView, the textfield below the grid adds it with previous value where i want the new edited value in the textfield. How to fix this?Following is the code:-

private void grdCaret_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    try
    {  
        string value = grdCaret.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        if (e.ColumnIndex == 1)
        {
           // int val = int.Parse(value);
           // quantity = val;
           // ekundag = ekundag + quantity;
           //tbTotDag_cr.Text =ekundag.ToString();

            int quantity = 0;

            foreach (DataGridViewRow row in grdCaret.Rows)
                quantity +=(int) grdCaret.Rows[e.RowIndex].Cells[1].Value.ToString();
                //quantity +=(int) row.Cells[1].Value;

            tbTotDag_cr.Text = quantity.ToString();
        }

        if (e.ColumnIndex == 2)
        {
            float val = float.Parse(value);
            total = val;
            ekunrakam = ekunrakam + total;
            tbTotPrice_cr.Text = ekunrakam.ToString();
        }
        grdCaret.Columns[3].ReadOnly = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

解决方案

Use CellEndEdit event to update your total value:

private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    int total = 0;

    foreach (DataGridViewRow row in dataGridView.Rows)            
        total += (int)row.Cells[columnTotal.Index].Value;

    totalTextBox.Text = total.ToString();           
}

这篇关于DataGridView的单元格编辑结束事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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