如何在 gridview 中只显示当前日期数据?

how to show only current date data in gridview?

我正在 ASP.NET 中构建网站,我想在 GridView 中仅显示当前日期数据。

这是我的 C# 代码。

public void GridBind()
{

    SqlCommand cmd_std = new SqlCommand("SELECT * FROM  StudentInfo WHERE GRNo = '" + GR_No + "' AND  school_id = '" + a + "' ", dbcon);
    SqlDataAdapter sda_std = new SqlDataAdapter(cmd_std);
    DataSet ds_std = new DataSet();
    sda_std.Fill(ds_std);


    if (ddlSubject.SelectedItem.Text == "All")
    {

        SqlCommand cmd = new SqlCommand("select * from HomeWork where Date >= '" + txtdate.Text + "'  AND school_id='" + a + "' AND Standard='" + ds_std.Tables[0].Rows[0]["CurrentStd"].ToString() + "'", dbcon);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        gvhw.DataSource = ds;
        gvhw.DataBind();
    }
    else
    {
        SqlCommand cmd = new SqlCommand("select * from HomeWork where Date >= '" + txtdate.Text + "'  AND Subject = '"+ddlSubject.SelectedItem.Text+"'  AND school_id='" + a + "' AND Standard='" + ds_std.Tables[0].Rows[0]["CurrentStd"].ToString() + "'", dbcon);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        gvhw.DataSource = ds;
        gvhw.DataBind();
    }
}

我需要在默认情况下在 GridView 中显示当前日期插入的数据,即,如果我们在页面加载时绑定网格,那么所有数据都显示在其中我需要相同的过程但只显示当前日期数据.

不太明白你问的是什么。你能提供一些预期的输出吗?但是如果你只想显示当前日期:

DateTime tudey = DateTime.Now;

问题不清楚所以我做了一些假设:

使用下面的代码将加载当前日期的记录(实际页面加载时)

string TDate = string.Empty;

if(string.IsNullOrEmpty(txtdate.Text))
{
   TDate = DateTime.Now.ToString("dd/MM/yyyy"); 
}
else
{
   TDate = txtdate.Text;
}

SqlCommand cmd = new SqlCommand("select * from HomeWork where Date = '" + TDate  + "'  AND school_id='" + a + "' AND Standard='" + ds_std.Tables[0].Rows[0]["CurrentStd"].ToString() + "'", dbcon);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
gvhw.DataSource = ds;
gvhw.DataBind();

编辑:

string NewDate = DateTime.Now.ToString("dd/MM/yyyy"); 

public void GridBind() 
{ 
  dbcon.Open();  
  SqlCommand cmd = new SqlCommand("select Id,FORMAT(Date,'dd/MM/yyyy') AS Date,Subject,Disc from NoticeBoard where school_id='" + a + "' and FORMAT(Date,'dd/MM/yyyy')= '" + NewDate + "' , dbcon); 

  SqlDataAdapter sda = new SqlDataAdapter(cmd); 
  DataSet ds = new DataSet(); 
  sda.Fill(ds); 
  gvTeacher.DataSource = ds; 
  gvTeacher.DataBind(); 
  dbcon.Close(); 
} 
public void GridBind()
{

    dbcon.Open();
    SqlCommand cmd = new SqlCommand("select Id,FORMAT(Date,'yyyy/MM/dd')AS Date,Subject,Disc from NoticeBoard where school_id='" + a + "'", dbcon);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    gvTeacher.DataSource = ds;
    gvTeacher.DataBind();
    dbcon.Close();
}
protected void DownloadFile(object sender, EventArgs e)
{
    int id = int.Parse((sender as LinkButton).CommandArgument);
    byte[] bytes;
    string fileName, contentType;
    string constr = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select Name, Data, ContentType from tblFiles1 where Id=@Id ";
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                sdr.Read();
                bytes = (byte[])sdr["Data"];
                contentType = sdr["ContentType"].ToString();
                fileName = sdr["Name"].ToString();
            }
            con.Close();
        }
    }
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = contentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();


}

asp.net 页面代码

'>