Introduction
This article will explain how to trigger a Calendar in a Data grid. The method is just showing the calendar when the button is clicked and hiding it when the date was selected. On the button click event the key value of record is stored in a session variable. This variable is used during the updation of date in the table and after the updation the grid will be refreshed immediately.
Using the code
A Datagrid named dgCalendar with three bound columns and one template column. The template column is used to show the selected date in the grid and it should be a textbox in item ttemplate mode.
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") %>"
The code is as follows:
private void Page_Load(object sender, System.EventArgs e)
{
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)
{
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;
}
Language : C#,ASP.net