请问Form1  List<DallClass1> cla;        public Form2()
        {
            InitializeComponent();
            dataGrid.AutoGenerateColumns = false;             this.dataGridView1.Rows.Clear();
        }        string num;
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            if (f1.ShowDialog() == DialogResult.OK)
            {
                cla = f1.CLA;
                num = f1.Num1;
            }
            
            dcop();
        }        private void dcop()
        {
            try
            {
                for (int i = 0; i < cla.Count; i++)
                {
                    dataGrid.Rows.Add();
                    dataGrid.Rows[dataGrid.Rows.Count - 1].Cells[0].Value = cla[i].商品类型;
                    dataGrid.Rows[dataGrid.Rows.Count - 1].Cells[1].Value = cla[i].商品编码;
                    dataGrid.Rows[dataGrid.Rows.Count - 1].Cells[2].Value = cla[i].商品名称;
                    dataGrid.Rows[dataGrid.Rows.Count - 1].Cells[3].Value = cla[i].规格型号;
                    dataGrid.Rows[dataGrid.Rows.Count - 1].Cells[4].Value = cla[i].单位;
                    dataGrid.Rows[dataGrid.Rows.Count - 1].Cells[5].Value = cla[i].价格;
                    dataGrid.Rows[dataGrid.Rows.Count - 1].Cells[6].Value = cla[i].数量;                    
                }                TotalRow(dataGrid);
               
            }
            catch { }           
            
        }
    
        private void TotalRow(DataGridView dg)
        {
            try
            {
                dg.Rows.Add();
                DataGridViewRow dgr = dg.Rows[dg.Rows.Count - 1];
                dgr.ReadOnly = true;
                dgr.Cells[0].Value = "合计";
                for (int i = 0; i < dg.Rows.Count - 1; i++)
                {
                    dgr.Cells[6].Value = Convert.ToSingle(dgr.Cells[6].Value) + Convert.ToSingle(dg.Rows[i].Cells[6].Value);
                    dgr.Cells[7].Value = Convert.ToSingle(dgr.Cells[7].Value) + Convert.ToSingle(dg.Rows[i].Cells[7].Value);
                }
            }
            catch (StackOverflowException)
            {
            }        }             //单元格更改时发生
        float tsum = 0;
        private void dataGrid_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dataGrid.Rows.Count > 0)
                {
                    if (e.ColumnIndex == 6)
                    {
                        tsum = Convert.ToSingle(dataGrid[6, e.RowIndex].Value.ToString()) * Convert.ToSingle(dataGrid[5, e.RowIndex].Value.ToString());
                        dataGrid[7, e.RowIndex].Value = tsum.ToString();                    }
                }
                TotalRow(dataGrid);
            }
            catch { }
        }
Form2
  private void button1_Click(object sender, EventArgs e)
        {
            cla = new List<DallClass1>();            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                if ((bool)dataGridView2.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    DallClass1 dc = new DallClass1();
                    dc.商品类型 = dataGridView2.Rows[i].Cells[2].Value.ToString();
                    dc.商品编码 = dataGridView2.Rows[i].Cells[3].Value.ToString();                    dc.商品名称 = dataGridView2.Rows[i].Cells[4].Value.ToString();
                    dc.规格型号 = dataGridView2.Rows[i].Cells[5].Value.ToString();                    dc.单位 = dataGridView2.Rows[i].Cells[6].Value.ToString();
                    dc.价格 = dataGridView2.Rows[i].Cells[7].Value.ToString();
                    CLA.Add(dc);
                }
            }
            this.Close();
        }第一次点击from1按钮显示Form2传过来的值,如http://hi.csdn.net/space-9426246-do-album-picid-733771-goto-down.html第二次点击from1按钮显示Form2传过来的值,如http://hi.csdn.net/space-9426246-do-album-picid-733770-goto-up.html应该怎么改?将TotalRow(dataGrid)放在单元格更改时,出现 未处理的“System.StackOverflowException”类型的异常出现在 System.Drawing.dll 中。错误,应怎么处理?

解决方案 »

  1.   

    Convert.ToSingle 改为 Convert.ToInt32 试试。int totalCount = 0;
    int totalPrice = 0;
    for (int i = 0; i < dg.Rows.Count; i++)
    {
      totalCount += Convert.ToInt32(dg.Rows[i].Cells[6].Value);
      totalPrice += Convert.ToInt32(dg.Rows[i].Cells[7].Value);
    }dg.Rows.Add();
    DataGridViewRow dgr = dg.Rows[dg.Rows.Count - 1];
    dgr.ReadOnly = true;
    dgr.Cells[0].Value = "合计";
    dgr.Cells[6].Value = totalCount;
    dgr.Cells[7].Value = totalPrice;
      

  2.   

    sorry,你应该在事件里把Cell_6和7排除掉。否则你改变合计值,又触发了这个事件。
      

  3.   

    用 RowValidated 效率高些。private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
    {
        // do not caculate when it is last row.
        if (e.RowIndex == this.dataGridView1.Rows.Count - 1)
            return;    int total1 = 0;
        int total2 = 0;
        for(int i=0; i<this.dataGridView1.Rows.Count-1; i++)
        {
            var row = this.dataGridView1.Rows[i];
            total1 += Convert.ToInt32(row.Cells[0].Value);
            total2 += Convert.ToInt32(row.Cells[1].Value);
        }    var totalRow = this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1];
        totalRow.Cells[0].Value = total1;
        totalRow.Cells[1].Value = total2;
    }