65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 中的 Data Grid 日历控件

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (3投票s)

2007年3月28日

viewsIcon

31037

downloadIcon

1

触发 Datagrid 中的日历

引言

本文将解释如何在 Data Grid 中触发日历。该方法只需在单击按钮时显示日历,并在选择日期时隐藏日历。在按钮单击事件中,记录的关键值存储在会话变量中。该变量用于在表中更新日期期间,并在更新后立即刷新网格。

使用代码

一个名为 dgCalendar 的 DataGrid,包含三个绑定列和一个模板列。模板列用于在网格中显示所选日期,并且应该在 item ttemplate 模式下是一个文本框。

BoundColumn DataField="Sno"

BoundColumn DataField="Name"

BoundColumn DataField="Date1"

TemplateColumn HeaderText="Date2"

<ItemTemplate> TextBox id=TextBox2 runat="server" Text=''<%# Data Binder.Eval(Container, "DataItem.date2") %>"

代码如下

//
 private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
   if(!Page.IsPostBack)
    BinddgCalendar();
  }
#region Bind Grid
  public void BinddgCalendar()
  {
   con.Open();
   SqlDataAdapter  adp = new SqlDataAdapter("SELECT Sno,Name," + 
                         "Date1,Date2 from TCalendar",con);
   DataSet ds = new DataSet();
   adp.Fill(ds,"Cal");
   dgCalendar.DataSource = ds.Tables["Cal"];
   dgCalendar.DataBind();
   con.Close();
  }
 #endregion
private void Calendar2_SelectionChanged(object sender, System.EventArgs e)
  {
   //Label1.Text = Session["SNO"].ToString();
   con.Open();
   SqlCommand  cmd = new SqlCommand("UPDATE TCalendar SET Date1 = '"+
                     Calendar2.SelectedDate+"', Date2 = '"+
                     Calendar2.SelectedDate.ToString("dd-MMM-yyyy")+
                     "' where Sno = '"+
                     Session["SNO"].ToString()+"'",con);
   cmd.ExecuteNonQuery();
   con.Close();
   BinddgCalendar();
   Calendar2.Visible=false;
  }
  private void dgCalendar_SelectedIndexChanged(object sender, 
                                               System.EventArgs e)
  {
   Session["SNO"] = dgCalendar.SelectedItem.Cells[0].Text;
   if( Calendar2.Visible == false)
    Calendar2.Visible = true;
   else
    Calendar2.Visible = false;
  }

语言:C#, ASP.net

© . All rights reserved.