Solved

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

Posted on 2014-10-13
3
504 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now