根据日期字段将记录绑定到gridview [英] Bind records to gridview based on date field

查看:47
本文介绍了根据日期字段将记录绑定到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们...
我想基于该Date字段绑定gridview记录.
首先,我要约束今天的日期
每当我单击上一个按钮时,我的gridview数据就会与前一天的记录绑定.
当我单击其他时间时,它将显示前一天记录的前一天,请帮助我...

Hi friends...
I want to bind the gridview records based on that Date field.
First i bind with today''s date
when ever i clicking on the previous button my gridview data bind with previous day records.
when i click another time it will display day before previous day records pls help me...

推荐答案



Hi,

private void BindGridView(DateTime HireDate)
{
    string ConString = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
    SqlConnection con = new SqlConnection(ConString);
    string CmdString = "Select EmployeeID, FirstName, LastName, Salary, HireDate FROM Employees ORDER BY Salary DESC";
    SqlCommand cmd = new SqlCommand(CmdString, con);
    DataSet ds = new DataSet();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    sda.Fill(ds);
    DataView dv = ds.Tables[0].DefaultView;
    dv.RowFilter = "HireDate='"+HireDate+"'";
    GridView2.DataSource = dv;
    GridView2.DataBind();
}



您可以根据需要从不同的按钮调用此功能



You can call this function from different buttons as per your need


protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           lblDate.Text = DateTime.Now.Date.ToShortDateString();
           BindData();
       }
   }
   protected void BindData()
   {
       SqlCommand cmd = new SqlCommand("SELECT * from Records where Date='"+lblDate.Text+"'", con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds);
       GridView2.DataSource = ds;
       GridView2.DataBind();
   }
   protected void Previous_Click(object sender, EventArgs e)
   {
       DatePrevoius();

   }
   protected void DatePrevoius()
   {
       DateTime d1 = DateTime.Parse(lblDate.Text);
       var d2 = d1.AddDays(-1).ToShortDateString();
       lblDate.Text = d2.ToString();
       BindData();
   }
   protected void Button3_Click(object sender, EventArgs e)
   {
       NextDate();
   }
   protected void NextDate()
   {
       DateTime d1 = DateTime.Parse(lblDate.Text);
       var d2 = d1.AddDays(1).ToShortDateString();
       lblDate.Text = d2.ToString();
       BindData();
   }


这篇关于根据日期字段将记录绑定到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆