Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-13
3
Medium Priority
?
530 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

Assisted Solution

by:Shaun Kline
Shaun Kline earned 2000 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

730 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