问题很简单。
我给datagridview1添加了一个checkbox列,为了简单起见datagridview1绑定数据时也只绑定了一个"金额"字段,我想达到的效果是,当用户在点击某行的checkbox时,能够即刻遍历一遍该datagridview1,统计出所有打了勾的行的总金额数(未打勾的不要统计),显示在Textbox1中。应该如何编写代码呢?特请教各位高手,答对者立马给分。谢谢!

解决方案 »

  1.   

    说明一下,所添加的checkbox列是事先手工加入的,不是运行时用代码添加的。
      

  2.   

    刚开始可以先把总额算出来啊~当然刚开始默认都不选就是0了。
    事先写的checkbox的话,就用onclick事件阿,然后判断checkBox名.Checked,如果现在是true说明原来没选,总额减去该checkbox对应的值就可以了。
    如果现在是false说明原来选中了,就要总额减去现在的值。
    遍历就不需要了,除非你做全选之类的
      

  3.   

    如果是WEB的话,可以在SelectedIndexChanged事件中写相关操作,
    事件中第一步先判断你点中的是不是checkbox,如果是的话,再统计统计出所有打了勾的行的总金额数
      

  4.   

    编写CellContentClick事件
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns["CheckBoxColName"].Index)//CheckBoxColName是CheckBox列的名字
        {
           DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];       if (Convert.ToBoolean(cell.EditedFormattedValue))//如果被选中
           {
                //遍历Datagridiew        
           }
        }
    }
      

  5.   

    楼上思路是对的
    但是如果考虑使用CellValueChanged事件,效果是不是会更好呢?
    因为可能点击前面的某些单元格,但并未产生值变
      

  6.   

    CellValueChanged事件在datagridview做绑定的时候也会触发,这样不是很合理。
      

  7.   

    if (e.ColumnIndex == dataGridView1.Columns["CheckBoxCol"].Index)
    {
        DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];    if (Convert.ToBoolean(cell.EditedFormattedValue))
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell cells = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[e.ColumnIndex];
                if (Convert.ToBoolean(cells.EditedFormattedValue))
                {
                    //取值累加
                  }
            }
        }
    }
      

  8.   

    <asp:TemplateField> 
    <ItemTemplate> 
    <asp:CheckBox ID="chkRecord" runat="server" AutoPostBack="False" onclick="check(this);" /> 
    </ItemTemplate> 
    <HeaderStyle BorderWidth="1px" Font-Size="9pt" HorizontalAlign="Center" /> </asp:TemplateField> 
    function check(source) 

    var checkedCount =document.getElementById("ctl00_ContentPlaceHolder2_Hidd").value; 
      if(source.checked){ 
    checkedCount++; 
      }else{ 
    checkedCount--; 
      } 
    document.getElementById("ctl00_ContentPlaceHolder2_Hidd").value = checkedCount; 
    } 以上是验证GridView中第一列中的CheckBox是否有被选中,希望能对你有所启发! 
      

  9.   

    如果想在后台实现,以下代码请参考! 
    //将页面上的CheckBox添加事件chkRecord_CheckedChanged//后台 //首先要确保将原来选中的金额保存在隐藏字段中 protected void chkRecord_CheckedChanged(object sender, EventArgs e)
        {
            bool isChecked = false;
            for (int i = 0; i < this.gvInfo.Rows.Count; i++)
            {
                GridViewRow row = gvInfo.Rows[i];
                isChecked = ((CheckBox)row.FindControl("chkRecord")).Checked;
                if (isChecked)
                {
                      // 如果该行再被选中,将该行对应的金额值累加到隐藏字段
                      //最后将隐藏字段值赋值给页面上的TextBox
                }
            }
        }完毕,这样肯定能实现。但是有一个问题是,如果页面上显示的记录太多,页面刷新会很慢!总之前台和后台实现各有利弊吧!以上仅供参考!
      

  10.   

    我这样写的private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                //Column1是普通列
                //Column2是checkbox列
                if (e.ColumnIndex == Column2.Index)
                {
                    int Amount = 0;
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (row.Cells[Column2.Index].FormattedValue.ToString() == "True")
                        {
                            Amount += Convert.ToInt32(row.Cells[Column1.Index].Value);
                        }
                    }
                    textBox1.Text = Amount.ToString();
                }
            }
    但是运行很混乱  根本不是楼主想要的结果
      

  11.   

     private System.Collections.Generic.Dictionary<int, bool> MyCheckState;
            private void UpdateStatusBar()
            {
                int MyCount = 0;
                foreach (bool IsChecked in MyCheckState.Values)
                {
                    if (IsChecked) MyCount++;
                }            toolStripStatusLabel1.Text = "当前选择的记录条数: " + MyCount.ToString(System.Globalization.CultureInfo.CurrentUICulture);
            }
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0)
                {
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = (bool)dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
                }
            }
            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0 && e.RowIndex != -1)
                {
                    int MyFirstColumn = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
                    MyCheckState[MyFirstColumn] = (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                    this.UpdateStatusBar();
                }
            }
            private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
            {
                if (e.ColumnIndex == 0)
                {
                    int MyFirstColumn = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
                    if (MyCheckState.ContainsKey(MyFirstColumn))
                    {
                        e.Value = MyCheckState[MyFirstColumn];
                    }
                    else
                        e.Value = false;
                }
            }
            private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
            {
                if (e.ColumnIndex == 0)
                {
                    int MyFirstColumn = (int)dataGridView1.Rows[e.RowIndex].Cells[1].Value;
                    if (!MyCheckState.ContainsKey(MyFirstColumn))
                    {
                        MyCheckState.Add(MyFirstColumn, (bool)e.Value);
                    }
                    else
                        MyCheckState[MyFirstColumn] = (bool)e.Value;
                }
            }        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
            {
                if (dataGridView1.CurrentCellAddress.X != 0)
                {
                    if (e.KeyCode == Keys.Space)
                    {
                        bool checkedValue = (bool)dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells[0].Value;
                        dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells[0].Value = !checkedValue;
                    }
                }
            }FORM_LOad()
    {
     MyCheckState = new Dictionary<int, bool>();
    }
      

  12.   

    谢谢各位,采用9楼(Wangyanboq)的代码稍微改了改就用上了,再次感谢!
      

  13.   

    我有一个不用遍历的方法,当大数据量时可以提高性能,给楼主推荐下!//Amount在构造函数里初始 就是先把打钩的金额加起来赋给Amount
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                //Column1是普通列
                //Column2是checkbox列
                if (e.ColumnIndex == Column2.Index)
                {
                    
                    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString() == "True")
                        Amount = Amount + Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[Column1.Index].Value);
                    else
                        Amount = Amount - Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[Column1.Index].Value);
                    textBox1.Text = Amount.ToString();            }
            }
      

  14.   

      private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                decimal Cout = 0;
                if (e.ColumnIndex == dgvDetail.Columns["Col_Check"].Index)
                {
                    foreach (DataGridViewRow dgvr in dgvDetail.Rows)
                    {
                        if (Convert.ToBoolean(dgvr.Cells["Col_Check"].EditedFormattedValue)  )
                        {
                            Cout +=Convert.ToDecimal(dgvr.Cells["Col_Submit"].Value) ;
                        }
                    }
                }
                this.lblCout.Text = Cout.ToString();        }
    这是我的实现方法,已经实现了。点击一次就要进行所有列的循环。呵呵。目前只能想到这个,如果有最优的,希望大家粘贴出来啊。