昨天的datagridview

时间:2014-07-21 05:30:01

标签: c# .net windows datagridview

我有一个带有dataGridView的程序,我想让它显示我今天输入的数据,并在昨天有另一个按钮等等(日期明智的分页)。

但是,如果日期发生变化,我希望每天都有一个空的datagridview(有0行),如果当天没有任何更新或插入的话。

这是我的代码:

    private void button5_Click(object sender, EventArgs e)
    {
        try
        {
            da.SelectCommand = new SqlCommand("Select * from Jobs", con);
            ds.Reset();
            da.Fill(ds);
           // DateTime.Now.AddDays(0);
            //DateTime.Now.ToShortDateString(1);
        }
        catch
        {
            MessageBox.Show("No SQL connection");
        }
        try
        {
            dataGridView1.DataSource = ds.Tables[0];
            bs.DataSource = ds.Tables[0];
        }
        catch (Exception i)
        {
            MessageBox.Show(i.Message, "Error");
        }
}
}

我在日期时间也试过“工作”:

请指导我..

3 个答案:

答案 0 :(得分:0)

尝试将 SelectCommand 更改为

Select * from Jobs where myDateColumn >= @myDate

然后将值赋给参数

da.SelectCommand.Parameters.Add("@myDate", SqlDbType.DateTime); da.SelectCommand.Parameters("@myDate").Value = DateTime.Now; //CHANGE DATE FILTER HERE

免责声明:代码未经过测试

答案 1 :(得分:0)

我得到了那些人

 select 

* 
from yourTable
WHERE YourDate >= dateadd(day,datediff(day,1,GETDATE()),0)
    AND YourDate < dateadd(day,datediff(day,0,GETDATE()),0)

今天

  SELECT GETDATE() 'Today'
  Yesterday

 SELECT DATEADD(d,-1,GETDATE()) 'Yesterday'
 First Day of Current Week


  SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) 'First Day of Current    Week'
   Last Day of Current Week

   SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) 'Last Day of Current Week'
  First Day of Last Week


  SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) 'First Day of Last Week'
  Last Day of Last Week

答案 2 :(得分:0)

I DID GUYS这是昨天的sql数据

private void button6_Click(object sender, EventArgs e)
   {
        try
        {
            da.SelectCommand = new SqlCommand("Select * from Jobs Where Jobs_Date = dateadd(day,datediff(day,1,GETDATE()),0) ", con);
            //  da.SelectCommand = new SqlCommand("Select * from Jobs", con);
            ds.Reset();
            da.Fill(ds);
        }
        catch
        {
            MessageBox.Show("No SQL connection");
        }
        try
        {
            dataGridView1.DataSource = ds.Tables[0];
            bs.DataSource = ds.Tables[0];                
        }
        catch (Exception i)
        {
            MessageBox.Show(i.Message, "Error");
        }