جمع مقادیر یک ستون خاص از DataGridView
یک برنامه ویندوزی مانند شکل زیر ایجاد کنید :
سپس در رویداد Load فرم و Click دکمه کدهای زیر را بنویسید :
using System; using System.Windows.Forms; namespace SumColumn { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.ColumnCount = 2; this.dataGridView1.Rows.Add(new object[] { "1", "5" }); this.dataGridView1.Rows.Add(new object[] { "9", "8" }); } private void button1_Click(object sender, EventArgs e) { int sum = 0; for (int i = 0; i < dataGridView1.Rows.Count; ++i) { sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value); } label1.Text = "Total sum is:" + sum.ToString(); } } }
برنامه را اجرا و با کلیک بر روی دکمه نتیجه را مشاهده کنید.