我正在使用C#,我从数据库中获取数据:
da.SelectCommand = new SqlCommand("SELECT name,company,startdate,enddate,DATEDIFF(day,GETDATE(),enddate) AS days FROM Person", cn);
ds.Clear();
da.Fill(ds);
dgvViolation.DataSource = ds.Tables[0];
并且数据将在datagridview中显示如下:
name company startdate enddate days
---------------------------------------
john IBM 12/2/2012 20/2/2012 5
steven IBM 1/2/2012 12/2/2012 -3
我需要datagridview才能显示仅在days列中显示的正值。我该怎么做?
答案 0 :(得分:1)
将查询更改为
"SELECT name,company,startdate,enddate,DATEDIFF(day,GETDATE(),enddate) AS days FROM Person WHERE days > 0"
答案 1 :(得分:0)
为什么不直接更改SQL命令以返回0以上的值?
但是如果你想要返回所有返回的数据,你可以使用gridview的RowDataBound事件,如下所示:
protected void gridview_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
{
//look at the value of the days column and use e.Row.Style["Display"] = "none"; to hide it.
}
}