Hi,
I am able to create and bind columns at run time into radgrid. But when I click "Edit" link for any record, error message is displayed like this.
Please find below the code for ASP page and C# page.
Thanks,
Chocks
ASP Page
<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Matman.Master"
CodeBehind="MaterialView.aspx.vb" Inherits="Matman_WF.MaterialView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function RowDblClick(sender, eventArgs) {
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="grdMaterial">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="grdMaterial" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
<div>
<div style="width:100%; background-color:transparent; font-family:Segoe UI; font-size:medium; font-weight:bold; padding-top:30px;
margin-bottom:5px; margin-left:3px" >Material View</div>
<telerik:RadGrid ID="grdMaterial" runat="server" AutoGenerateColumns="false" Height="600px"
AllowPaging="true" AllowSorting="false" PageSize="200" ShowStatusBar="True" AlternatingItemStyle-BackColor="AliceBlue"
CellSpacing="0" OnNeedDataSource="grdFieldConfig_NeedDataSource"
OnItemDataBound="grdMaterial_OnItemDataBound" >
<PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
<GroupingSettings CaseSensitive="false" />
<AlternatingItemStyle BackColor="AliceBlue"></AlternatingItemStyle>
<MasterTableView Width="100%" TableLayout="Auto" DataKeyNames="material"
EditMode="InPlace" AllowMultiColumnSorting="false" HeaderStyle-Wrap="true" >
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditColumn" HeaderStyle-Width="50px" ItemStyle-Width="50px" ></telerik:GridEditCommandColumn>
<telerik:GridBoundColumn UniqueName="material" DataField="material"
HeaderText = "SAP Material / SKU">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="120px" />
<ItemStyle HorizontalAlign="Left" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="matDescription" DataField="matDescription"
HeaderText = "Material Desc">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="250px" />
<ItemStyle HorizontalAlign="Left" />
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
<ClientSettings EnableRowHoverStyle="true" AllowColumnsReorder="true" >
<Resizing AllowColumnResize="true" EnableRealTimeResize="false" AllowResizeToFit="false" />
<Scrolling AllowScroll="true" UseStaticHeaders="true" EnableVirtualScrollPaging="false" SaveScrollPosition="false" />
<Selecting AllowRowSelect="false" />
<ClientEvents OnRowDblClick="RowDblClick" />
</ClientSettings>
</telerik:RadGrid>
</div>
</asp:Content>
VB Code (Code Behind)
Imports Telerik.Web.UI
Public Class MaterialView
Inherits System.Web.UI.Page
Dim Lst_Columns As List(Of SP_MaterialView_ColumnList_Result) = New List(Of SP_MaterialView_ColumnList_Result)
Dim Lst_MaterialView As List(Of SP_MaterialView_Result) = New List(Of SP_MaterialView_Result)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not (Page.IsPostBack) Then
Dim oDB As New MATMAN_Entities()
'Get list of columns from the database
Lst_Columns = oDB.SP_MaterialView_ColumnList(1).ToList()
'Get list of Materials from the database. This is the data the will be displayed in the grid.
Lst_MaterialView = oDB.SP_MaterialView.ToList()
Session.Add("Lst_MaterialView", Lst_MaterialView)
'Bind the list to the grid.
grdMaterial.DataSource = Lst_MaterialView
grdMaterial.DataBind()
If (oDB.Connection.State = ConnectionState.Open) Then
oDB.Connection.Close()
End If
End If
Catch ex As Exception
'Throw ex
Finally
End Try
End Sub
Protected Sub grdFieldConfig_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdMaterial.NeedDataSource
Try
Lst_MaterialView = Session("Lst_MaterialView")
grdMaterial.DataSource = Lst_MaterialView
Catch ex As Exception
End Try
End Sub
Protected Sub grdMaterial_OnItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdMaterial.ItemDataBound
Try
For Each element As SP_MaterialView_ColumnList_Result In Lst_Columns
Dim Column As GridBoundColumn = New GridBoundColumn()
'Check for 1 value in IsEditable field. If present then display that column in grid
If (element.IsEditable = 1) Then
Column.HeaderText = element.Column_DisplayName
Column.Display = True
Column.DataField = element.ColumnNames
Column.UniqueName = element.ColumnNames
'Get the value for width property and assign it to the radcolumn.
If (element.Width Is Nothing = False) Then
Column.HeaderStyle.Width = Unit.Pixel(element.Width)
Column.ItemStyle.Width = Unit.Pixel(element.Width)
Column.HeaderStyle.VerticalAlign = VerticalAlign.Top
Column.ItemStyle.HorizontalAlign = HorizontalAlign.Left
End If
grdMaterial.MasterTableView.Columns.Add(Column)
End If
Next element
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
I am able to create and bind columns at run time into radgrid. But when I click "Edit" link for any record, error message is displayed like this.
Please find below the code for ASP page and C# page.
Thanks,
Chocks
ASP Page
<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Matman.Master"
CodeBehind="MaterialView.aspx.vb" Inherits="Matman_WF.MaterialView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function RowDblClick(sender, eventArgs) {
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="grdMaterial">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="grdMaterial" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
<div>
<div style="width:100%; background-color:transparent; font-family:Segoe UI; font-size:medium; font-weight:bold; padding-top:30px;
margin-bottom:5px; margin-left:3px" >Material View</div>
<telerik:RadGrid ID="grdMaterial" runat="server" AutoGenerateColumns="false" Height="600px"
AllowPaging="true" AllowSorting="false" PageSize="200" ShowStatusBar="True" AlternatingItemStyle-BackColor="AliceBlue"
CellSpacing="0" OnNeedDataSource="grdFieldConfig_NeedDataSource"
OnItemDataBound="grdMaterial_OnItemDataBound" >
<PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
<GroupingSettings CaseSensitive="false" />
<AlternatingItemStyle BackColor="AliceBlue"></AlternatingItemStyle>
<MasterTableView Width="100%" TableLayout="Auto" DataKeyNames="material"
EditMode="InPlace" AllowMultiColumnSorting="false" HeaderStyle-Wrap="true" >
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditColumn" HeaderStyle-Width="50px" ItemStyle-Width="50px" ></telerik:GridEditCommandColumn>
<telerik:GridBoundColumn UniqueName="material" DataField="material"
HeaderText = "SAP Material / SKU">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="120px" />
<ItemStyle HorizontalAlign="Left" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="matDescription" DataField="matDescription"
HeaderText = "Material Desc">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="250px" />
<ItemStyle HorizontalAlign="Left" />
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
<ClientSettings EnableRowHoverStyle="true" AllowColumnsReorder="true" >
<Resizing AllowColumnResize="true" EnableRealTimeResize="false" AllowResizeToFit="false" />
<Scrolling AllowScroll="true" UseStaticHeaders="true" EnableVirtualScrollPaging="false" SaveScrollPosition="false" />
<Selecting AllowRowSelect="false" />
<ClientEvents OnRowDblClick="RowDblClick" />
</ClientSettings>
</telerik:RadGrid>
</div>
</asp:Content>
Imports Telerik.Web.UI
Public Class MaterialView
Inherits System.Web.UI.Page
Dim Lst_Columns As List(Of SP_MaterialView_ColumnList_Result) = New List(Of SP_MaterialView_ColumnList_Result)
Dim Lst_MaterialView As List(Of SP_MaterialView_Result) = New List(Of SP_MaterialView_Result)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not (Page.IsPostBack) Then
Dim oDB As New MATMAN_Entities()
'Get list of columns from the database
Lst_Columns = oDB.SP_MaterialView_ColumnList(1).ToList()
'Get list of Materials from the database. This is the data the will be displayed in the grid.
Lst_MaterialView = oDB.SP_MaterialView.ToList()
Session.Add("Lst_MaterialView", Lst_MaterialView)
'Bind the list to the grid.
grdMaterial.DataSource = Lst_MaterialView
grdMaterial.DataBind()
If (oDB.Connection.State = ConnectionState.Open) Then
oDB.Connection.Close()
End If
End If
Catch ex As Exception
'Throw ex
Finally
End Try
End Sub
Protected Sub grdFieldConfig_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdMaterial.NeedDataSource
Try
Lst_MaterialView = Session("Lst_MaterialView")
grdMaterial.DataSource = Lst_MaterialView
Catch ex As Exception
End Try
End Sub
Protected Sub grdMaterial_OnItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdMaterial.ItemDataBound
Try
For Each element As SP_MaterialView_ColumnList_Result In Lst_Columns
Dim Column As GridBoundColumn = New GridBoundColumn()
'Check for 1 value in IsEditable field. If present then display that column in grid
If (element.IsEditable = 1) Then
Column.HeaderText = element.Column_DisplayName
Column.Display = True
Column.DataField = element.ColumnNames
Column.UniqueName = element.ColumnNames
'Get the value for width property and assign it to the radcolumn.
If (element.Width Is Nothing = False) Then
Column.HeaderStyle.Width = Unit.Pixel(element.Width)
Column.ItemStyle.Width = Unit.Pixel(element.Width)
Column.HeaderStyle.VerticalAlign = VerticalAlign.Top
Column.ItemStyle.HorizontalAlign = HorizontalAlign.Left
End If
grdMaterial.MasterTableView.Columns.Add(Column)
End If
Next element
Catch ex As Exception
Throw ex
End Try
End Sub
End Class