Solved

set default value for asp.net datagrid field in edit mode (c#)

Posted on 2014-10-13
3
513 Views
Last Modified: 2014-10-19
I have a simple c# asp.net datagrid being populated from a SQL table.

I have a a template field that displays a date value from the database.
When the user clicks the edit button, the date field goes in to edit mode as expected.

What I am trying to figure out is how to default the date field to current date if the underlying data field is null.

I can't seem to figure out the correct event to watch.

Thanks, Kevin.
0
Comment
Question by:kevinvw1
  • 2
3 Comments
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 500 total points
ID: 40379474
You can do it in your SQL statement using the ISNULL function. It would look like ISNULL(<your date field>, GETDATE())
0
 

Accepted Solution

by:
kevinvw1 earned 0 total points
ID: 40379772
Hi, thanks for the tip.  I actually wanted it to remain null until the user clicks edit on the grid.  Then the edit field would default to today's date.  

I ended up scraping the datagrid and going to a a gridview and I was able to get it all working as desired.

Here is how I did it -

protected void grdJobProduction_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdJobProduction.EditIndex = e.NewEditIndex;
            BindData();
            if (((TextBox)grdJobProduction.Rows[grdJobProduction.EditIndex].Cells[7].Controls[1]).Text == "")
            {
                ((TextBox)grdJobProduction.Rows[grdJobProduction.EditIndex].Cells[7].Controls[1]).Text = DateTime.Now.ToString("M/d/yyyy");
            }
        }
0
 

Author Closing Comment

by:kevinvw1
ID: 40389801
My comment fully explains the desired need.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question