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?

4

1 回答 1

1

你的是格式问题:

只需在列上设置 DataFormatString 属性,如下所示:

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

完整的上下文:

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

或任何适合您情况的格式,日期将被正确格式化。

于 2012-08-28T13:55:26.920 回答