如何从DataGridView读取数据?

时间:2019-03-02 12:30:19

标签: c# winforms gridview

所以我想通过单击按钮来获取每日收入(从gridview)

private void button1_Click(object sender, EventArgs e)
{
    int sum = 0;
    if (DateTime.Today == SalesGridView)
    {
        for (int i = 0; i < SalesGridView.Rows.Count; ++i)
        {
            sum += Convert.ToInt32(SalesGridView.Rows[i].Cells[3].Value);
        }
        label1.Text = sum.ToString();
    }
}

2 个答案:

答案 0 :(得分:1)

这是从DataGridView读取数据的方式。

for (int rows = 0; rows < SalesGridView.Rows.Count; rows++)
{
    for (int col= 0; col < SalesGridView.Rows[rows].Cells.Count; col++)
    {
        string value = SalesGridView.Rows[rows].Cells[col].Value.ToString();    
    }
} 

if (DateTime.Today == SalesGridView) //此条件是错误的。

应使用特定的DateTime.Today单元格检查该SalesGridView,该列具有valid date而不是完整的SalesGridView

答案 1 :(得分:0)

我认为您需要用一些有效日期替换SalesGridView条件下的GridView if

int sum = 0;
if (DateTime.Today == validDate)
{
    for (int i = 0; i < SalesGridView.Rows.Count; ++i)
    {
        sum += Convert.ToInt32(SalesGridView.Rows[i].Cells[3].Value);
    }
    label1.Text = sum.ToString();
}