C# 如何在datagridview列中显示的总和?

C# 如何在datagridview列中显示的总和?,c#,winforms,datagridview,C#,Winforms,Datagridview,我需要显示此datagridview中count列的总和,但我不知道如何获取datagridview中的数据 单击按钮时,我想在label1中显示94 如何做到这一点 int sum = 0; for (int i = 0; i < dataGridView1.Rows.Count; ++i) { sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value); } label1.Text = sum.ToString();

我需要显示此
datagridview
count
列的总和,但我不知道如何获取datagridview中的数据

单击按钮时,我想在
label1
中显示
94

如何做到这一点

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

int sum=0;
对于(int i=0;i
如果可以,请使用LINQ

  label1.Text =  dataGridView1.Rows.Cast<DataGridViewRow>()
                                   .AsEnumerable()
                                   .Sum(x => int.Parse(x.Cells[1].Value.ToString()))
                                   .ToString();
label1.Text=dataGridView1.Rows.Cast()
.可计算的()
.Sum(x=>int.Parse(x.Cells[1].Value.ToString())
.ToString();

如果您的网格绑定到一个
数据表
,我相信您可以:

// Should probably add a DBNull check for safety; but you get the idea.
long sum = (long)table.Compute("Sum(count)", "True");
如果它没有绑定到表,您可以轻松地将其绑定到:

var table = new DataTable();
table.Columns.Add("type", typeof(string));
table.Columns.Add("count", typeof(int));

// This will automatically create the DataGridView's columns.
dataGridView.DataSource = table;

将总行添加到将绑定到网格的数据集合中。

使用两个datagridview可以做得更好,可以添加相同的数据源,隐藏第二个数据源的标题,将第二个数据源的高度=设置为第一个数据源行的高度,关闭第二个数据源的所有可调整大小的属性,同步两个数据源的滚动条,仅水平放置,将第二个放置在第一个的底部等

看一看:

   dgv3.ColumnHeadersVisible = false;
   dgv3.Height = dgv1.Rows[0].Height;
   dgv3.Location = new Point(Xdgvx, this.dgv1.Height - dgv3.Height - SystemInformation.HorizontalScrollBarHeight);
   dgv3.Width = dgv1.Width;

   private void dgv1_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                dgv3.HorizontalScrollingOffset = e.NewValue;
            }
        }
decimal总数=0;
对于(int i=0;i
使用LINQ的快速清洁方式

int total = dataGridView1.Rows.Cast<DataGridViewRow>()
                .Sum(t => Convert.ToInt32(t.Cells[1].Value));
inttotal=dataGridView1.Rows.Cast()
.Sum(t=>Convert.ToInt32(t.Cells[1].Value));
已在VS2013上验证

//声明总变量
//declare the total variable
int total = 0;
//loop through the datagrid and sum the column 
for(int i=0;i<datagridview1.Rows.Count;i++)
{
    total +=int.Parse(datagridview1.Rows[i].Cells["CELL NAME OR INDEX"].Value.ToString());

}
string tota
int-total=0; //循环遍历datagrid并对列求和
对于(int i=0;i使用LINQ,它非常干净

label1.Text=(来自datagridview1.Rows中的DataGridViewRow行
其中!String.IsNullOrEmpty(row.Cells[1].FormattedValue.ToString())
选择Convert.ToDouble(row.Cells[1].FormattedValue)).Sum().ToString();

欢迎访问StAdvExcel!考虑你的其他问题的最佳答案,最好的一个,你应该用绿色的复选标记来标记“被接受的答案”。这将有助于确保你对未来的问题有好的答案!“<代码> > DATGRIDVIEWVIEW行/<代码>是否被转换为<代码> DATAROW ?但是…但是…是否有<代码>字段?
DataGridViewRow上的扩展方法
?我想起了我在这里的原因……我甚至从未意识到DataTable对象上存在一个
Compute
函数!
//declare the total variable
int total = 0;
//loop through the datagrid and sum the column 
for(int i=0;i<datagridview1.Rows.Count;i++)
{
    total +=int.Parse(datagridview1.Rows[i].Cells["CELL NAME OR INDEX"].Value.ToString());

}
string tota