This is a migrated thread and some comments may be shown as answers.

Problem with server side set DataFormatString on GridBoundColumn

2 Answers 366 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Murray
Top achievements
Rank 1
Murray asked on 03 Feb 2010, 10:58 PM
in aspx file:
...
<telerik:RadGrid
        ID="grdList" runat="server"
        EnableViewState="false"
        Height="328px"
        AllowPaging="true"
        AllowSorting="true"
        AllowFilteringByColumn="true"
        AutoGenerateColumns="False"
        PageSize="12"
        Skin="WebBlue">
    <MasterTableView ClientDataKeyNames="Col1" EnableViewState="False">
        <Columns>
            <telerik:GridBoundColumn DataField="Col1"
                HeaderText="COL1" Visible="false">
                <HeaderStyle Width="80px" HorizontalAlign="left" />
                <ItemStyle Width="80px" HorizontalAlign="left" />
            </telerik:GridBoundColumn>                        
...

in aspx.cs file => Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ...
        GridBoundColumn col;
        col = (GridBoundColumn)grdList.MasterTableView.Columns[i];
        col.HeaderText = colHdrs[i];
        // colDataTypes[i].Equals array containing "date" or "string" or ...
        if (colDataTypes[i].Equals("date"))
        {
            col.DataType = Type.GetType("System.DateTime");
            col.DataFormatString = "{0:MM/dd/yyyy}";
        }
        col.Visible = true;
        ...
    }
}

I tried different values for DataFormatString and it always displays long date time format
when i set to bogus value: [0:MM/dd/yyyy] => all rows displayed the string [0:MM/dd/yyyy]

So i know the DataFormatString is being set for date column.

2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 08 Feb 2010, 04:34 PM
Hello Murray,

In order to set a date-time format string to your column you should ensure that it is bound to a DateTime field. The following code performed as expected on my side:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <telerik:RadGrid ID="grdList" runat="server" EnableViewState="false" Height="328px"
        AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="true" AutoGenerateColumns="False"
        PageSize="12" Skin="WebBlue" OnNeedDataSource="grdList_NeedDataSource">
        <MasterTableView ClientDataKeyNames="Col1" EnableViewState="False">
            <Columns>
                <telerik:GridBoundColumn DataField="Col1" HeaderText="COL1" Visible="false">
                    <HeaderStyle Width="80px" HorizontalAlign="left" />
                    <ItemStyle HorizontalAlign="left" />
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
</telerik:RadGrid>

Code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridBoundColumn col;
            col = (GridBoundColumn)grdList.MasterTableView.GetColumn("Col1");
            col.DataType = Type.GetType("System.DateTime");
            col.DataFormatString = "{0:MM/dd/yyyy}";
            col.Visible = true;
        }
    }
 
    protected void grdList_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Col1",typeof(DateTime));
         
        for (int i = 0; i < 5; i++)
        {
            table.Rows.Add(DateTime.Now.AddDays(i));
        }
        grdList.DataSource = table;
    }

I hope this helps,
Martin
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Murray
Top achievements
Rank 1
answered on 08 Feb 2010, 10:57 PM
I created a user control that creates a datasource of up to 10 columns but the data type is not known until runtime. My datasource always contains all columns as System.String.

I now format the data during get datasource and that works.

Thanks for your response.
Tags
Grid
Asked by
Murray
Top achievements
Rank 1
Answers by
Martin
Telerik team
Murray
Top achievements
Rank 1
Share this question
or