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

Make RadGrid Work With Unbound Datasource

7 Answers 351 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy Yoder
Top achievements
Rank 1
Jeremy Yoder asked on 12 Apr 2010, 10:15 PM

I created a RadGrid with predefined columns. At first when I ran the webpage, the grid was blank, not even showing the headers. But by creating a blank dataset in a Session variable and assigning it to the RadGrid, I get it to display. However, the "Add Record", "Edit", and "Delete" buttons don't work.

The online demos are all bound to a datasource, but we don't want that. How can the RadGrid work with a dataset in memory? I want the MasterTableView to load and display values, which I'll save later, all at once. My dummy HTML and code-behind are below...


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager> 
          
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" GridLines="None" 
            AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True">  
            <HeaderContextMenu EnableAutoScroll="True">  
            </HeaderContextMenu> 
            <MasterTableView CommandItemDisplay="Top">  
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="Col1" HeaderText="Col1" UniqueName="Col1" SortExpression="Col1">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Col2" HeaderText="Col2" UniqueName="Col2" SortExpression="Col2">  
                    </telerik:GridBoundColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
          
    </div> 
    </form> 
</body> 
</html> 
 


Imports System.Data  
 
Partial Class _Default  
    Inherits System.Web.UI.Page  
 
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
 
        If Not IsPostBack Then 
 
            Dim ds As New DataSet  
            Dim dt As New DataTable  
            dt.Columns.Add("Col1")  
            dt.Columns.Add("Col2")  
 
            ' Create dummy rows to delete/edit  
            Dim vals(1) As String 
            vals = New String() {"100""Desc100"}  
            dt.Rows.Add(vals)  
            vals = New String() {"200""Desc200"}  
            dt.Rows.Add(vals)  
 
            ds.Tables.Add(dt)  
            Session("myDS") = ds  
            RadGrid1.DataSource = Session("myDS")  
            RadGrid1.MasterTableView.DataSource = Session("myDS")  
 
        End If 
 
    End Sub 
 
End Class 

7 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 16 Apr 2010, 06:44 AM
Hi Jeremy,

To make sure that your setup works as expected, you can use the NeedDataSource event handler, and pass the session datasource there.
Setting the datasource for the grid anywhere else, or calling databind directly, will break its default functionalities.

Greetings,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 23 Apr 2010, 09:07 PM

Thanks for the response. I changed my code to use NeedDatasource, and turned on the 3 "AllowAutomatic..." options, but the Insert/Delete/Edit functions aren't working. What am I missing? I've been poking around the site, but I can't find an example working with a dataset like I'm doing. My updated code is below...


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> 
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager> 
          
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" GridLines="None" 
            AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"   
            AllowAutomaticDeletes="True" AllowAutomaticInserts="True"   
            AllowAutomaticUpdates="True">  
            <HeaderContextMenu EnableAutoScroll="True">  
            </HeaderContextMenu> 
            <MasterTableView CommandItemDisplay="Top">  
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="Col1" HeaderText="Col1" UniqueName="Col1" SortExpression="Col1">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Col2" HeaderText="Col2" UniqueName="Col2" SortExpression="Col2">  
                    </telerik:GridBoundColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
          
    </div> 
    </form> 
</body> 
</html> 
 


Imports System.Data  
 
Partial Class _Default  
    Inherits System.Web.UI.Page  
 
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
 
        If Not IsPostBack Then 
 
            Dim ds As New DataSet  
            Dim dt As New DataTable  
            dt.Columns.Add("Col1")  
            dt.Columns.Add("Col2")  
 
            ' Create dummy rows to delete/edit/insert  
            Dim vals(1) As String 
            vals = New String() {"100""Desc100"}  
            dt.Rows.Add(vals)  
            vals = New String() {"200""Desc200"}  
            dt.Rows.Add(vals)  
 
            ds.Tables.Add(dt)  
            Session("myDS") = ds  
 
        End If 
 
    End Sub 
 
    Protected Sub RadGrid1_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
        RadGrid1.DataSource = Session("myDS")  
    End Sub 
 
End Class 
 
0
Yavor
Telerik team
answered on 28 Apr 2010, 02:43 PM
Hi Jeremy,

To use automatic operations, you will need to employ a Datasource control, such as AccessDataSource, or SQLDataSource.
If you want to use NeedDataSource, you will need to manually handle the CRUD operations, as shown in this article:

http://www.telerik.com/help/aspnet-ajax/grdupdatinginplaceandeditforms.html

I hope this information helps.

Regards,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 29 Apr 2010, 08:16 PM

Thanks for the link. I got both the 2nd and 3rd options to work. Is either method more recommended than the other?

However, in both cases, when I click "Edit", "Update", "Add Record", "Insert", or "Cancel", the page refreshes. That makes sense for Insert, Delete, and Update, given the server-side code I've added. But I'd rather do all these client-side. How can that be done now that I'm manually adding CRUD operations via NeedDataSource?

But let me back up the truck a bit and give you a big picture of what we want to see if I'm even going about this right...

We want a grid to do all CRUD operations to a datable in memory (either in a session variable or on the RadGrid's data -- whichever is doable) without any PostBacks. Below the grid, we'll have save and cancel buttons. Both will do a Postback to either save all changes or reload the original datatable. (I don't know if it makes a difference, but we later want comboboxes and search buttons for some columns.)

So are my examples above on the right track, or am I way off since the code you pointed me to is all server-side? Is what I'm asking for even possible with the RadGrid? Thanks.
0
Yavor
Telerik team
answered on 04 May 2010, 11:43 AM
Hello Jeremy,

In such a setup, avoiding a postback is not possible.
You can use client side databinding for the control, as shown in this example:

http://demos.telerik.com/aspnet-ajax/grid/examples/client/insertupdatedelete/defaultcs.aspx

the inserts/updates/deletes are handled with an outside editform, because of which no postback is required.
I hope this information helps.

Kind regards,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 04 May 2010, 10:13 PM

It seems the answers to my latest questions were easier than expected. None of your online examples refreshed the page, so I didn't understand how your server code for the CRUD functions ran. Then I discovered your RadAjaxManager, which selects which controls post back. Once I understood that, the rest made sense.

Do you have some type of tutorial I should go through that tells how to link all your various controls together? Otherwise, I had never stumbled across that control, and I feel like I should have known about it somewhere along the way.

Also, could you answer my initial question in the previous post, which was, "I got both the 2nd and 3rd options to work. Is either method more recommended than the other?" Thanks.
0
Mira
Telerik team
answered on 10 May 2010, 12:38 PM
Hello Jeremy,

To make the most of our products, I recommend that you examine the Step-by-step Tutorial – RadControls for ASP.NET AJAX.

For the Insert/Update/Delete operations techniques: you can use the one you prefer, they all implement the same functionality.

Regards,
Mira
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Jeremy Yoder
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Jeremy Yoder
Top achievements
Rank 1
Mira
Telerik team
Share this question
or