0

I am using the Scott Gu Dynamic Linq class and I am trying to convert a datetime to return a short datetime before databinding the result set to a gridview in ASP.net. Now, I know that I can go through the gridview row databound event and check each cell to see if it is a date, and if it is, convert the date to the shortdate, however that is very cumbersome and I do not feel it is very efficient.

I am building my select statement in DynamicLinq as follows:

Filter gets its information as follows:

 foreach (KeyValuePair<string,string> kvp in dictFilters)
        {
            filter += kvp.Key + " as " + kvp.Value.Replace("-", "_").Replace(" ", "_") + ",";
        }


        var result = db.ViewADHOCContractInfos.Select("new(" + filter + ")");

which gives me the result set of:

    {SELECT [t0].[dtmAward] AS [Award_Date], [t0].[guidFromId],  [t0].[strFundingNumber] AS [Funding_Number], [t0].[dtmCertified] AS [Certified_Date], [t0].[strFundingNumberStatus] AS [Funding_Status]
FROM [dbo].[ViewADHOCInfo] AS [t0]
}

Now what I want is this:

   {SELECT Convert(VarChar, [t0].[dtmAward], 101) AS [Award_Date], [t0].[guidFromId],  [t0].[strFundingNumber] AS [Funding_Number], [t0].[dtmCertified] AS [Certified_Date], [t0].[strFundingNumberStatus] AS [Funding_Status]
FROM [dbo].[ViewADHOCInfo] AS [t0]
}

Is it possible to add a SQL convert to by select statement using Dynamic Linq?

Alex
  • 7,901
  • 1
  • 41
  • 56
EvanGWatkins
  • 1,427
  • 6
  • 23
  • 52

1 Answers1

1

Yours is a formatting issue:

Simply set the DataFormatString property on the column as so:

DataFormatString="{0:MM/dd/yyy}"

The full context:

<asp:BoundField DataField="DateColumn" 
                    HeaderText="Date" 
                    DataFormatString="{0:MM/dd/yy}" />

Or to any suitable format for your case and the date will be formatted properly.

Icarus
  • 63,293
  • 14
  • 100
  • 115