I am creating dynamic column the following way:
Dim TotalColumn As New GridNumericColumn()
grdContingentDevelopment.MasterTableView.Columns.Add(TotalColumn)
TotalColumn.DataField = "Total"
TotalColumn.HeaderText = "Total"
TotalColumn.SortExpression = "Total"
TotalColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
TotalColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Left
TotalColumn.DataFormatString = "{0:N2}"
TotalColumn.HeaderStyle.Width = 90
TotalColumn.ItemStyle.Width = 90
TotalColumn.ItemStyle.Font.Bold = True
That works fine, however if i add: TotalColumn.ReadOnly = True, most of formats such as DataFormatString or HorizontalAlign stop working
Any ideas?
7 Answers, 1 is accepted
Please create the grid structure during Page_Init event handler and add the properties before the grdContingentDevelopment.MasterTableView.Columns.Add(TotalColumn) line:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/creating-a-radgrid-programmatically#creating-a-radgrid-on-page_init
I hope this will prove helpful.
Regards,
Eyup
Telerik by Progress
Eyup,
Unfortunately it made no difference
Can you please send me working example with ReadOnly setup as "true"?
I am sending a sample RadGrid web site with programmatic creation. Please run the attached web site sample and verify that the properties of the numeric column are properly working even with ReadOnly set to true.
Regards,
Eyup
Telerik by Progress
Hi Eyup,
Thank you for example, unfortunately i didn't provide you enough details to create the most suitable code. In this particular case i implemented "paste from excel" ability, because of that i had to keep all records in "edit" state. I attached picture and code.
<telerik:RadGrid ID="grdContingentDevelopment" runat="server"
AllowMultiRowEdit="true" AllowAutomaticUpdates="true"
OnItemUpdated="grdContingentDevelopment_ItemUpdated"
OnUpdateCommand="grdContingentDevelopment_UpdateCommand"
OnItemDataBound="grdContingentDevelopment_ItemDataBound"
OnNeedDataSource="OnNeedDataSource"
onclick="gridFocus(event)"
style="margin-top:0px;" Width="1px"
ShowFooter="True"
Skin="WebBlue" ShowGroupPanel="false" ShowStatusBar="True" GroupsDefaultExpanded="False"
AllowSorting="false" AllowPaging="false" AllowFilteringByColumn="false"
GridLines="None" AutoGenerateColumns="False">
<GroupingSettings CaseSensitive="false" />
<HeaderStyle HorizontalAlign="Center" />
<PagerStyle Position="Bottom" AlwaysVisible="true"
Mode="NextPrevNumericAndAdvanced"/>
<ClientSettings>
<ClientEvents OnKeyPress="grdContingentDevelopment_OnKeyPress">
</ClientEvents>
</ClientSettings>
<MasterTableView DataKeyNames="Period"
EditMode="InPlace" CommandItemDisplay="Top"
GroupLoadMode="Client" GroupsDefaultExpanded="False"
AutoGenerateColumns="False" PageSize="15">
<HeaderStyle HorizontalAlign="Center" />
<CommandItemTemplate>
<div style="float: left">
<div style="float: right; margin-top:1px; margin-bottom:3px; margin-right:5px;">
<asp:Button ID="Button2"
OnClientClick="clearCellsCED()"
runat="server" class="rgCancel" />
<a id="A4" onmouseover="this.style.cursor='pointer'"
onclick="clearCellsCED()">
Reset
</a>
</div>
<div style="float: right; margin-top:1px; margin-bottom:3px; margin-right:5px;">
<asp:Button ID="Button1"
OnClientClick="pasteFromExcel()"
runat="server" class="rgUpdate" />
<a id="A2" onmouseover="this.style.cursor='pointer'"
onclick="pasteFromExcel()">
Paste from Excel
</a>
</div>
</div>
<div style="float: right; margin-top:1px; margin-right:5px; display:none;" >
<asp:Button ID="btnCEDUpdateEditedButton"
CommandName="UpdateEdited"
runat="server" class="rgSave"/>
<a id="A1" onmouseover="this.style.cursor='pointer'"
onclick="btnCEDUpdateEditedButton_Click();">
Update
</a>
</div>
</CommandItemTemplate>
<HeaderStyle Height="30px" Font-Bold="true" />
<Columns>
<telerik:GridBoundColumn Visible="false"
DataField="PeriodID" HeaderText="PeriodID"/>
<telerik:GridBoundColumn ReadOnly="true"
DataField="Period" HeaderText="Period"
HeaderStyle-Width="200px"
ItemStyle-Width="200px" ItemStyle-HorizontalAlign="left" />
</Columns>
<NoRecordsTemplate>
<div style="padding: 5px">
No records available.
</div>
</NoRecordsTemplate>
</MasterTableView>
<FilterMenu EnableTheming="True">
<CollapseAnimation Duration="200" Type="OutQuint" />
</FilterMenu>
<ClientSettings ReorderColumnsOnClient="True" AllowDragToGroup="false" EnableRowHoverStyle="true">
</ClientSettings>
</telerik:RadGrid>
Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
'--------------------------------------------------------------------------------------------
' grdContingentDevelopment
'--------------------------------------------------------------------------------------------
Dim totalCount As Integer = grdContingentDevelopment.MasterTableView.Items.Count - 2
Dim currentCount As Integer = 0
For Each item As GridItem In grdContingentDevelopment.MasterTableView.Items
If currentCount < totalCount Then
If item.GetType().ToString() = "Telerik.Web.UI.GridDataItem" Then
Dim editableItem As GridEditableItem = DirectCast(item, GridDataItem)
editableItem.Edit = True
End If
End If
currentCount = currentCount + 1
Next
grdContingentDevelopment.Rebind()
Dim rowIndex As Integer = 0
For Each item As GridDataItem In grdContingentDevelopment.Items
item.CssClass = If(System.Math.Max(System.Threading.Interlocked.Increment(rowIndex), rowIndex - 1) Mod 2 = 0, "rgRow customEditRow", "rgAltRow customAltEditRow")
Next
End Sub
First, let me start by saying that mixing declarative with programmatically created columns is not supported scenario and you will have to create the entire columns structure in the code-behind.
Furthermore, could you please elaborate which version of the controls you are using, because setting the DataFormatString on ReadOnly column displays the formatted values correctly with our latest versions.
Regards,
Konstantin Dikov
Telerik by Progress