2010-12-06 15 views
1

私は選択したセルの合計にこのコードを使用します。その仕事は良いが、文字があるユーザーselecteセルは、例外をスローした場合:)私はselectet細胞に文字が合計私は同じ質問をしDataGridviewの選択された値を合計する

を作るいけないときに確保できるかが、そこに私は私のコードを追加することはできません

private void dataGridView1_SelectionChanged(object sender, EventArgs e) 
    { 
     for (int i = 0; i < dataGridView1.SelectedCells.Count; i++) 
     { 
      if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[i].Cells["cLoadName"])) 
      { 
       float nextNumber = 0; 
       float sumNumbers = 0; 

             nextNumber = float.Parse(dataGridView1.SelectedCells[i].FormattedValue.ToString()); 
        sumNumbers += nextNumber; 


       tsslSumSelected.Text = "ჯამი: " + sumNumbers; 
       tsslTotalSelected.Text = "მონიშნული: " + dataGridView1.SelectedCells.Count.ToString(); 
      } 
      else 
      { 

      } 
     }  

    } 

答えて

2
private void dataGridView1_SelectionChanged(object sender, EventArgs e) 
    { 
     for (int i = 0; i < dataGridView1.SelectedCells.Count; i++) 
     { 
      if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[i].Cells["cLoadName"])) 
      { 
       float nextNumber = 0; 
       float sumNumbers = 0; 

        if (float.TryParse(dataGridView1.SelectedCells[i].FormattedValue.ToString(), out nextNumber)) 
         sumNumbers += nextNumber; 


       tsslSumSelected.Text = "ჯამი: " + sumNumbers; 
       tsslTotalSelected.Text = "მონიშნული: " + dataGridView1.SelectedCells.Count.ToString(); 
      } 
      else 
      { 

      } 
     }  

    } 
+0

私にとっては機能しません。選択したセルに文字が含まれていない場合は、単に数値を合計するコードを与えることができます。 –

+0

これはコードが行うものです。セルに有効な番号が含まれているかどうかを確認します。なぜそれはあなたのために働いていないのですか? – jvanrhyn

0
public void ComputeGridTot() 
{ 
    try 
    { 
     decimal qty= 0; 
     decimal price = 0; 
     decimal tax = 0; 
     decimal Total = 0; 
     for (int i = 0; i < drGrdView1.Rows.Count; i++) 
     { 
      if (drGrdView1.Rows[i].Cells[7].Value.ToString() == blcnt.ToString()) 
      { 
       qty += Convert.ToDecimal(drGrdView1.Rows[i].Cells[3].Value); 
       price += Convert.ToDecimal(drGrdView1.Rows[i].Cells[4].Value); 
       tax += Convert.ToDecimal(drGrdView1.Rows[i].Cells[5].Value); 
       Total += Convert.ToDecimal(drGrdView1.Rows[i].Cells[6].Value); 

      } 
     } 
     lblGTax.Text = tax.ToString(); 
     lblGQty.Text = qty.ToString(); 
     lblGPrice.Text = price.ToString(); 
     lblGTot.Text = Total.ToString(); 
    } 
    catch 
    { 
    } 
} 
関連する問題