Click here to Skip to main content
Licence 
First Posted 27 Mar 2007
Views 17,101
Bookmarked 15 times

Calendar control in Data Grid in C#

By | 27 Mar 2007 | Article
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

About the Author

kokilaB

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat the ?%* PinmemberSacha Barber0:06 28 Mar '07  
AnswerRe: What the ?%* PinmemberkokilaB20:45 28 Mar '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 28 Mar 2007
Article Copyright 2007 by kokilaB
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid