Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Calendar control in Data Grid in C#

0.00/5 (No votes)
28 Mar 2007 1  
Trigerring a Calendar in a Datagrid

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)
  {
   // 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;
  }

Language : C#,ASP.net

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here