2017-03-06 2 views
1

datagridviewでQTYという名前の列の合計を計算しようとしました。 しかし、私はそうではありません。私のコードは以下の通りです。私を手伝ってくれますか? enter image description hereDatagridviewの列の合計

public int RowCount { get; set; } 

    [BrowsableAttribute(false)] 
    private void dataGridView1_SelectionChanged(object sender, EventArgs e) 

    { 

    private void UpdateLabelText() 
    { 
     int QtyTotal = 0; 

     int counter; 

     // Iterate through all the rows and sum up the appropriate columns. 
     for (counter = 0; counter < (dataGridView1.Rows.Count); 
      counter++) 
     { 
      if (dataGridView1.Rows[counter].Cells["Qty"].Value 
       != null) 
      { 
       if (dataGridView1.Rows[counter]. 
        Cells["Qty"].Value.ToString().Length != 0) 
       { 
        QtyTotal += int.Parse(dataGridView1.Rows[counter]. 
         Cells["Qty"].Value.ToString()); 
       } 
      } 
     } 

     // Set the labels to reflect the current state of the DataGridView. 
     label17.Text = "Qty Total: " + QtyTotal.ToString(); 

    } 
+0

そのコードに問題は何を試してみてください?例外、予期しない結果? – Pikoh

+0

合計はラベル17に示されていません – Bahadir

+0

予期しているエラーを示しています – Bahadir

答えて

1

これを試してみてください:

int sum = 0; 
int col_index=YOUR_COLUMN_INDEX; 
for (int i = 0; i < dataGridView1.Rows.Count; ++i) 
{ 
    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[col_index].Value); 
} 
label17.Text= sum.ToString(); 
+0

感謝を示している電子の閉じ括弧を追加する必要がありますこれが成功した場合は、受け入れられた回答として受け入れてください – Bahadir

+0

働いているエラー – imsome1

+0

歓迎されている – imsome1

0

この

foreach (DataGridViewRow item in dataGridView2.Rows) 
{ 
    int n = item.Index; 
    text_tot.Text = (int.Parse(label17.Text) + int.Parse(dataGridView1.Rows[n].Cells[(index)].Value.ToString())).ToString(); 
}