Click here to Skip to main content
15,887,596 members
Articles / Web Development / ASP.NET
Article

Calendar control in Data Grid in C#

Rate me:
Please Sign up or sign in to vote.
1.00/5 (3 votes)
27 Mar 2007 30.4K   1   15   2
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:

C#
//
 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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat the ?%* Pin
Sacha Barber28-Mar-07 0:06
Sacha Barber28-Mar-07 0:06 
AnswerRe: What the ?%* Pin
kokilaB28-Mar-07 20:45
kokilaB28-Mar-07 20:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.