Hello,
I have a RadGrid that has it's columns programatically defined and initial data (via a dataset) loaded in the Page_Init event. The RadGrid is created and the initial data is loaded correctly, however when I try to insert new data into the grid the initial data is lost.
Example: The RadGrid is created with two columns and a data set with five rows of data. When I click "Add record" to add "Paul, Allen" the original five rows of data are removed and only the newest record, e.g. "Paul, Allen" in this example, is retained when I need all six records.
I was using the following documentation as a guide to creating the RadGrid's columns in the code-behing, http://www.telerik.com/help/aspnet-ajax/grdchanginggridstructuredynamically.html, and as such I have set EnableColumnViewState. I am thinking this is part of the problem and I'm having trouble figuring out when and where to store the dataset to a Session or ViewState object.
I have a RadGrid that has it's columns programatically defined and initial data (via a dataset) loaded in the Page_Init event. The RadGrid is created and the initial data is loaded correctly, however when I try to insert new data into the grid the initial data is lost.
Example: The RadGrid is created with two columns and a data set with five rows of data. When I click "Add record" to add "Paul, Allen" the original five rows of data are removed and only the newest record, e.g. "Paul, Allen" in this example, is retained when I need all six records.
I was using the following documentation as a guide to creating the RadGrid's columns in the code-behing, http://www.telerik.com/help/aspnet-ajax/grdchanginggridstructuredynamically.html, and as such I have set EnableColumnViewState. I am thinking this is part of the problem and I'm having trouble figuring out when and where to store the dataset to a Session or ViewState object.
| <telerik:RadGrid ID="RadGrid1" runat="server" > |
| <MasterTableView> |
| <RowIndicatorColumn> |
| <HeaderStyle Width="20px" /> |
| </RowIndicatorColumn> |
| <ExpandCollapseColumn> |
| <HeaderStyle Width="20px" /> |
| </ExpandCollapseColumn> |
| <Columns> |
| </Columns> |
| </MasterTableView> |
| <ClientSettings AllowKeyboardNavigation="True"> |
| <Scrolling AllowScroll="True" UseStaticHeaders="True" ScrollHeight="" /> |
| </ClientSettings> |
| </telerik:RadGrid> |
| Imports Microsoft.ApplicationBlocks.Data |
| Imports System.Configuration |
| Imports System.Data.SqlClient |
| Imports System.Data |
| Imports Telerik.Web.UI |
| Partial Class Usercontrols_Grid |
| Inherits System.Web.UI.UserControl |
| Private _customDataSet As DataSet |
| Private Sub CreateGrid() |
| 'RadGrid1.PageSize = 10 |
| RadGrid1.AllowPaging = True |
| RadGrid1.AllowSorting = True |
| RadGrid1.GridLines = GridLines.None |
| RadGrid1.Width = Unit.Percentage(50) |
| RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric |
| RadGrid1.AutoGenerateColumns = False |
| RadGrid1.ShowStatusBar = False |
| RadGrid1.PageSize = 7 |
| 'RadGrid1.MasterTableView.Width = Unit.Percentage(100) |
| RadGrid1.MasterTableView.EnableColumnsViewState = False |
| RadGrid1.MasterTableView.PageSize = 5 |
| RadGrid1.MasterTableView.EditMode = GridEditMode.PopUp |
| RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top |
| RadGrid1.MasterTableView.DataKeyNames = New String() {"CustomID"} |
| RadGrid1.MasterTableView.EditFormSettings.EditColumn.ButtonType = GridButtonColumnType.ImageButton |
| RadGrid1.MasterTableView.EditFormSettings.PopUpSettings.Modal = True |
| 'RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadGrid1, RadGrid1) |
| End Sub |
| Private Sub CreateColumns() |
| Dim EditColumn As New GridEditCommandColumn |
| EditColumn.ButtonType = GridButtonColumnType.ImageButton |
| EditColumn.UniqueName = System.Guid.NewGuid.ToString |
| Dim DeleteColumn As New GridButtonColumn |
| DeleteColumn.ConfirmText = "Delete this row?" |
| DeleteColumn.ConfirmDialogType = GridConfirmDialogType.RadWindow |
| DeleteColumn.ConfirmTitle = "Delete" |
| DeleteColumn.ButtonType = GridButtonColumnType.ImageButton |
| DeleteColumn.CommandName = "Delete" |
| DeleteColumn.Text = "Delete" |
| DeleteColumn.UniqueName = "DeleteColumn" |
| DeleteColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center |
| With RadGrid1.MasterTableView.Columns |
| .Add(EditColumn) |
| If Not IsNothing(_customDataSet) Then |
| For Each dc As DataColumn In _customDataSet.Tables(0).Columns |
| Dim NewColumn = GetGridColumnType(dc.DataType.ToString) |
| NewColumn.GetType.ToString) |
| 'Dim NewColumn As New GridBoundColumn |
| NewColumn.DataField = dc.ColumnName |
| NewColumn.HeaderText = dc.ColumnName |
| If dc.ColumnName <> "CustomID" Then |
| .Add(NewColumn) |
| End If |
| Next |
| End If |
| .Add(DeleteColumn) |
| End With |
| End Sub |
| Private Property GridSource() As DataSet |
| Get |
| Dim obj As Object = Me.ViewState("_gds") |
| If (Not (obj) Is Nothing) Then |
| Return CType(obj, DataSet) |
| Else |
| _customDataSet = CreateDataSetStructure() |
| Me.ViewState("_gds") = _customDataSet |
| Return _customDataSet |
| End If |
| End Get |
| Set(ByVal value As DataSet) |
| Me.ViewState("_gds") = value |
| _customDataSet = value |
| End Set |
| End Property |
| 'Created initial data set |
| Private Function CreateDataSetStructure() As DataSet |
| 'Internals removed for posting as this part works fine to load the initial data set structure |
| Return CustomDataSet |
| End Function |
| Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init |
| RadGrid1.DataSource = GridSource |
| CreateGrid() |
| CreateColumns() |
| End Sub |
| Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load |
| End Sub |
| Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand |
| Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem) |
| Dim editMan As GridEditManager = editedItem.EditManager |
| Dim CustomTable As DataTable = GridSource.Tables(0) |
| Dim NewRow As DataRow |
| NewRow = CustomTable.NewRow() |
| 'Set new values |
| Dim column As GridColumn |
| For Each column In e.Item.OwnerTableView.Columns |
| If TypeOf column Is IGridEditableColumn Then |
| Dim editableCol As IGridEditableColumn = CType(column, IGridEditableColumn) |
| If (editableCol.IsEditable) Then |
| Dim editor As IGridColumnEditor = editMan.GetColumnEditor(editableCol) |
| Dim editorText As String = "Unknown" |
| Dim editorValue As Object = Nothing |
| If (TypeOf editor Is GridTextColumnEditor) Then |
| editorText = CType(editor, GridTextColumnEditor).Text |
| editorValue = CType(editor, GridTextColumnEditor).Text |
| End If |
| If (TypeOf editor Is GridNumericColumnEditor) Then |
| editorText = CType(editor, GridNumericColumnEditor).Text |
| editorValue = CType(editor, GridNumericColumnEditor).Text |
| End If |
| If (TypeOf editor Is GridDateTimeColumnEditor) Then |
| editorText = CType(editor, GridDateTimeColumnEditor).Text |
| editorValue = CType(editor, GridDateTimeColumnEditor).Text |
| End If |
| If (TypeOf editor Is GridBoolColumnEditor) Then |
| editorText = CType(editor, GridBoolColumnEditor).Value.ToString() |
| editorValue = CType(editor, GridBoolColumnEditor).Value |
| End If |
| If (TypeOf editor Is GridDropDownColumnEditor) Then |
| editorText = CType(editor, GridDropDownColumnEditor).SelectedText + "; " + CType(editor, GridDropDownColumnEditor).SelectedValue |
| editorValue = CType(editor, GridDropDownColumnEditor).SelectedValue |
| End If |
| Try |
| newRow(column.UniqueName) = editorValue |
| Catch ex As Exception |
| 'Label1.Text = (Label1.Text + ("Unable to insert. Reason: " + ex.Message)) |
| e.Canceled = True |
| End Try |
| End If |
| End If |
| Next |
| '_customDataSet.AcceptChanges() |
| CustomTable.Rows.Add(NewRow) |
| Me.GridSource.AcceptChanges() |
| Me.ViewState("_gds") = GridSource |
| 'CustomTable.AcceptChanges() |
| End Sub |
| End Class |