【问题标题】:How to read data from a DataGridView?如何从 DataGridView 中读取数据?
【发布时间】:2019-03-02 12:30:19
【问题描述】:

所以我想通过单击按钮获取每日收入(来自 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();
    }
}

【问题讨论】:

标签: c# winforms gridview


【解决方案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 应使用具有valid date 而不是完整的SalesGridView 的特定SalesGridView单元格 进行检查。

【讨论】:

    【解决方案2】:

    我认为您需要将 if 条件中的 GridView SalesGridView 替换为某个有效日期。

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多