如何在具有特定日期的c#中添加所有单元格值

时间:2017-02-25 10:53:13

标签: c# visual-studio

伙伴帮助我想在相同的日期添加所有价值,并在我点击添加时转移到日销售,只是具有相同日期的所有价值,请帮助

enter image description here

1 个答案:

答案 0 :(得分:0)

好友... 堆栈溢出是一个网站,你需要一些技巧,而不是因为人们没有足够的时间来完成整个工作......但是,这是迄今为止的代码我认为可能对你有帮助。

private string _date = "";
private double _total = 0;
private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        PopulateCombos();
    }
    catch (Exception)
    {

    }
 }

private void PopulateCombos()
{
    try
    {
        PopulateEveryDay();
        PopulateDaySales();
    }
    catch (Exception)
    {
    }
} 

private void PopulateEveryDay()
{
    dgvEvery.DataSource = null;
    DataTable dtEvery = new DataTable();
    dtEvery.Columns.Add("date", typeof(DateTime));
    dtEvery.Columns.Add("total", typeof(double));

    DataRow dr = null;
    for (int i = 0; i < 10; i++)
    {
        dr = dtEvery.NewRow();

        dr[0] = System.DateTime.Now.AddDays(-i).ToShortDateString();
        if (i == 0)
            dr[1] = 100 * 1;
        else
            dr[1] = 100 * i * 2;

        dtEvery.Rows.Add(dr);
    }
    dgvEvery.DataSource = dtEvery;
}

private void PopulateDaySales()
{
    dgvDaySales.DataSource = null;
    DataTable dtEvery = new DataTable();
    dtEvery.Columns.Add("date", typeof(DateTime));
    dtEvery.Columns.Add("total", typeof(double));

    DataRow dr = null;
    for (int i = 0; i < 10; i++)
    {
        dr = dtEvery.NewRow();

        dr[0] = System.DateTime.Now.AddDays(-i).ToShortDateString();
        if (i == 0)
           dr[1] = 100 * 1;
        else
           dr[1] = 100 * i * 3;

        dtEvery.Rows.Add(dr);
    }
    dgvDaySales.DataSource = dtEvery;
}


private void btnAdd_Click(object sender, EventArgs e)
{
    try
    {
        DataTable dt = (DataTable)dgvDaySales.DataSource;
        DataRow[] dr = dt.Select("date = '" + _date + "'");
        foreach (DataRow item in dr)
        {
            item["total"] = Convert.ToDouble(item["total"]) + _total;
        }
    }
    catch (Exception)
    {
        throw;
    }
}


private void dgvEvery_CellClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        _date = Convert.ToString(Convert.ToDateTime(dgvEvery.Rows[e.RowIndex].Cells["date"].Value).ToShortDateString());
        _total = Convert.ToDouble(dgvEvery.Rows[e.RowIndex].Cells["total"].Value);
    }
    catch (Exception)
    {
    }
}