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

GridView needs more plumbing code to support Calendar controls

0.00/5 (No votes)
27 Nov 2007 2  
GridView needs more plumbing code to support Calendar controls

Download TaskManager.zip - 55.4 KB

GridView needs more plumbing code to support Calendar controls

Introduction

I needed to create a TaskManager application to manage tasks assigned to programmers in my team and used calendar controls to provide the StartDate and EndDate for each task created. I am able provide two way binding for SelectedDate and also retain the VisibleDate as documented on MSDN, but I had problems retaining the SelectedDate in the GridView during edits.

Background

Although I have EnableViewState set to True, I had to store the StartDate and EndDate in ViewState collection as ViewState ["StartDate"] and ViewState ["EndDate"] and assign them to SelectedDate during page refresh. The code to do this is straight forward and is shown below.

Screenshot - TaskManager.jpg

Using the Code

<code> 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (ViewState["StartDate"] != null) 
    { 
        ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[5].Controls[1]).SelectedDate = (DateTime)ViewState["StartDate"]; 
    } 
    if (ViewState["EndDate"] != null) 
    { 
        ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[6].Controls[1]).SelectedDate = (DateTime)ViewState["EndDate"]; 
    } 
} 
</code>
<code> 
protected void Calendar1_SelectionChanged(object sender, EventArgs e) 
{ 
    ViewState["StartDate"] = ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[5].Controls[1]).SelectedDate; 
} 
</code>
<code> 
protected void Calendar2_SelectionChanged(object sender, EventArgs e) 
{ 
    ViewState["EndDate"] = ((Calendar)grdTaskManager.Rows[grdTaskManager.EditIndex].Cells[6].Controls[1]).SelectedDate; 
} 
</code>
<code> 
protected void odsTaskManager_Updating(object sender, SqlDataSourceCommandEventArgs e) 
{ 
    if (ViewState["StartDate"] != null) 
    { 
        e.Command.Parameters["@StartDate"].Value = ((DateTime)ViewState["StartDate"]).ToString(); 
        ViewState.Remove("StartDate"); 
    } 
    if (ViewState["EndDate"] != null) 
    { 
        e.Command.Parameters["@EndDate"].Value = ((DateTime)ViewState["EndDate"]).ToString(); 
        ViewState.Remove("EndDate"); 
    } 
    if (txtTaskDescription.Text.Length >

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