如何添加datagridview的列的值 [英] how to add the values of columns of the datagridview

查看:77
本文介绍了如何添加datagridview的列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个带有两列的datagridview,其中一列包含项目,另一列包含价格,现在每当将一个项目添加到其中时,它下面的标签的值都应使用addrows事件中所有项目的总和来更新..我如何执行此操作..

thnx

hi all ,
I have a datagridview with two columns one contains item and another contain price and now whenever an item is added to it the values of the label below it should get updated with the sum of all the items on addrows event .. how can i perform this ..

thnx

推荐答案

处理DataGridView Row Transactions事件,并对它们求和(这是用字符串完成的,但这是您的作业,因此您应该做 某事 您自己):

Handle the DataGridView RowsAdded event, and sum them (this does it with strings, but it''s your homework, so you should do something yourself):

private void myDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    StringBuilder sb = new StringBuilder();
    if (dgv != null)
        {
        foreach (DataGridViewRow row in dgv.Rows)
            {
            sb.Append(row.Cells["Column2"].Value + " ");
            }
        myTotalLabel.Text = sb.ToString();
        }
    }


private void myGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
 long iCount = 0;
 foreach (DataGridViewRow row in myGridView.Rows)
 {
  iCount=iCount+Convert.ToInt64(row.Cells["Column2"].Value);
 }
ItemTotal.Text = iCount.toString();
}



希望对您有帮助



Hope this helps


这篇关于如何添加datagridview的列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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