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

[Solved] RadGrid vs GridView

2 Answers 634 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 03 Dec 2009, 05:38 PM
I am trying to convert a standard asp.net GridView to a RadGrid.

My set up is Windows XP Pro SP3, ASP.NET AJAX controls 2009 Q1 and Visual Studio 2008 3.5 Framework.

In the GridView I have code that will allow me to edit/update/cancel a record.

Please see below the asp.net code with standard GridView
protected void Page_Load(object sender, EventArgs e) 
    { 
        //Load the categories grid only the first tim the page is loaded 
        if (!IsPostBack) 
        { 
            //Load the categories grid 
            BindGrid(); 
 
        } 
    } 
 
    //Populate the GrdView with data 
    private void BindGrid() 
    { 
        //Get category from query string 
        string categoryId = Request.QueryString["CategoryID"]; 
 
        //Get a DataTable object containing the categories 
        grid.DataSource = CatalogAccess.CatalogGetCategories(); 
 
        //Bind the data to the grid 
        grid.DataBind(); 
    } 
 
    //Enter row into edit mode 
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
        //set the row for which to enale edit mode 
        grid.EditIndex = e.NewEditIndex; 
 
        //set status message 
        statusLabel.Text = "Editing row " + e.NewEditIndex.ToString(); 
 
        //Reload the grid 
        BindGrid(); 
    } 


protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
        //retrieve the updated data 
        string id = grid.DataKeys[e.RowIndex].Value.ToString(); 
        string name = ((TextBox)grid.Rows[e.RowIndex].Cells[0].Controls[0]).Text; 
        string description = ((TextBox)grid.Rows[e.RowIndex].FindControl("descriptionTextBox")).Text; 
 
        //execute the update command 
        bool success = CatalogAccess.UpdateCategory(id, name, description); 
 
        //cancel edit mode 
        grid.EditIndex = -1; 
 
        //display status message 
        statusLabel.Text = success ? "Update successful" : "Update failed"
 
        //reload the grid 
        BindGrid(); 
 
    } 
 


Is there a way to achieve the same thing with the RadGrid?  I would appreciate it if anyone could point me in the right direction.

Many thanks

Simon

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 Dec 2009, 07:01 AM
Hi,

Check out the help document link below which will give you an idea on how to work out the  desired scenario:
You can use the GridEditCommandColumn to put your grid in edit mode and the AdvancecDataBinding technique to bind your grid

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

Thanks,
Princy

0
Simon
Top achievements
Rank 1
answered on 04 Dec 2009, 07:19 PM
Many thanks for the link Princy.  Works like a dream.

This was my solution for anyone else who is wondering!

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" Width="97%" OnNeedDataSource="RadGrid1_NeedDataSource" 
        OnDeleteCommand="RadGrid1_DeleteCommand" OnInsertCommand="RadGrid1_InsertCommand" 
        OnUpdateCommand="RadGrid1_UpdateCommand"
        <MasterTableView DataKeyNames="CategoryID" GridLines="None" Width="100%" CommandItemDisplay="Top"
            <Columns> 
                <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="Delete"
                </telerik:GridButtonColumn> 
                <telerik:GridBoundColumn DataField="CategoryID" HeaderText="CategoryID" UniqueName="CategoryID" 
                    ReadOnly="True"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Name" HeaderText="Cat. Name" UniqueName="CatName"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Description" HeaderText="Cat. Description" UniqueName="CatDescription"
                </telerik:GridBoundColumn> 
                <telerik:GridEditCommandColumn> 
                </telerik:GridEditCommandColumn> 
            </Columns> 
            <EditFormSettings ColumnNumber="2" CaptionFormatString="Edit details for category with ID {0}" 
                CaptionDataField="CategoryID"
                <FormTableItemStyle Wrap="False"></FormTableItemStyle> 
                <FormCaptionStyle></FormCaptionStyle
                <FormMainTableStyle CellSpacing="0" CellPadding="3" Width="100%" /> 
                <FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" Height="110px" 
                    Width="100%" /> 
                <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle> 
                <FormStyle Width="100%" BackColor="#EEF2EA"></FormStyle> 
                <EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit"
                </EditColumn> 
                <FormTableButtonRowStyle HorizontalAlign="Right"></FormTableButtonRowStyle> 
            </EditFormSettings> 
            <ExpandCollapseColumn Visible="False"
                <HeaderStyle Width="19px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <RowIndicatorColumn Visible="False"
                <HeaderStyle Width="20px" /> 
            </RowIndicatorColumn> 
        </MasterTableView> 
    </telerik:RadGrid> 

ASPX.CS

protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        RadGrid1.DataSource = CatalogAccess.CatalogGetCategories(); 
 
         
    } 
 
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        //Get the GridEditableItem of the RadGrid      
        GridEditableItem editedItem = e.Item as GridEditableItem; 
        //Get the primary key value using the DataKeyValue.      
        string id = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["CategoryID"].ToString(); 
        //Access the textbox from the edit form template and store the values in string variables.      
        string name = (editedItem["CatName"].Controls[0] as TextBox).Text; 
        string description = (editedItem["CatDescription"].Controls[0] as TextBox).Text; 
         
 
        try 
        { 
            CatalogAccess.UpdateCategory(id, name, description); 
 
        } 
        catch (Exception ex) 
        { 
            RadGrid1.Controls.Add(new LiteralControl("Unable to update Employee. Reason: " + ex.Message)); 
            e.Canceled = true
        }      
    } 


Simon
Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Simon
Top achievements
Rank 1
Share this question
or