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

[Solved] How to add new items to RadGrid in aspx from usercontrol asxc

1 Answer 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chi Ming
Top achievements
Rank 1
Chi Ming asked on 19 Apr 2013, 10:12 PM
Hi,

I am currently developing RadGrid control in the web form (aspx) and also develop usercontrol (asxc) which has RadTreeView and two buttons (Add and Cancel).

How can I add new items to the RadGridView in the web form when I checked some boxes of the treeview and then clicked "Add" button in the usercontrol? 

I have source code below.

.ASPX 

<telerik:RadGrid ID="rgRequestLocations" runat="server" CellSpacing="0" GridLines="None" AutoGenerateColumns="False"
                                  OnNeedDataSource="rgRequestLocations_NeedDataSource" OnPreRender="rgRequestLocations_PreRender" OnItemInserted="rgRequestLocations_ItemInserted">
                                  <MasterTableView InsertItemDisplay="Top" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add Locations to Request">
                                      <NoRecordsTemplate>
                                          <table>
                                              <tr>
                                                  <td>
                                                      <asp:Label ID="lblNoRecords" runat="server" Style="color: red; font-weight: bold" TabIndex="14">You have not added any locations to the request.</asp:Label></td>
                                              </tr>
                                          </table>
                                      </NoRecordsTemplate>
                                      <Columns>
                                          <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                                          </telerik:GridEditCommandColumn>
                                          <telerik:GridBoundColumn SortExpression="location_pk" HeaderText="Location ID" HeaderButtonType="TextButton"
                                              DataField="location_pk" Visible="false">
                                          </telerik:GridBoundColumn>
                                          <telerik:GridBoundColumn HeaderText="Location Name" HeaderButtonType="TextButton"
                                              DataField="location_name">
                                          </telerik:GridBoundColumn>
                                          <telerik:GridEditCommandColumn ButtonType="PushButton" UniqueName="modify" EditText="Modify">
                                          </telerik:GridEditCommandColumn>
                                          <telerik:GridButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Remove Location" UniqueName="removelocation">
                                          </telerik:GridButtonColumn>
                                      </Columns>
                                      <EditFormSettings EditFormType="WebUserControl" UserControlName="~/Registration/Controls/PO_Registration.ascx">
                                          <EditColumn UniqueName="EditCommandColumn1">
                                          </EditColumn>
                                      </EditFormSettings>
                                  </MasterTableView>
 
                              </telerik:RadGrid>

In the Code-Behind of ASPX

protected void rgRequestLocations_ItemInserted(object sender, GridInsertedEventArgs e)
{
    UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
    //insert a new values
 
    Hashtable newVlaues = new Hashtable();
    newVlaues["Grid"] = (userControl.FindControl("rtvlocation") as RadTreeView).GetAllNodes();
 
}


ASCX
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PO_Registration.ascx.cs" Inherits="Registration_Controls_PO_Registration" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<div style="padding: 10px">
    <telerik:RadDropDownList ID="ddlAdministrations" runat="server" AutoPostBack="true"
        OnSelectedIndexChanged="ddlAdministrations_SelectedIndexChanged">
    </telerik:RadDropDownList>
</div>
<div id="displayTreeViewAdmin" runat="server" visible="false" style="padding: 10px">
    <div>
        <telerik:RadTreeView ID="rtvLocations" runat="server" CheckBoxes="True"
            CausesValidation="False" TriStateCheckBoxes="False" DataFieldID="location_pk"
            DataFieldParentID="parent_fk" DataTextField="location_name" DataValueField="location_pk" />
    </div>
    <div style="padding: 10px">
        <telerik:RadButton ID="btnAddLocations" runat="server" Text="Request Checked Locations" CommandName="Add" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>' OnClick="btnAddLocations_Click"></telerik:RadButton>
        <telerik:RadButton ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click"></telerik:RadButton>
    </div>
 
</div>

In the code-behind of Ascx

private object _dataItem = null;
 
    public object DataItem
    {
        get
        {
            return this._dataItem;
        }
        set
        {
            this._dataItem = value;
        }
    }
 
#region Control Methods
    override protected void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    }
 
    private void InitializeComponent()
    {
        btnAddLocations.Visible = true;
        PopulateAdminstrations();
    }
    #endregion
 
 
 
#region Button Events
    protected void btnAddLocations_Click(object sender, EventArgs e)
    {
        /*loop through tree, create datatable with appropriate columns, and bind the datatable to radgrid*/
        //create new properties
        const string locationid = "location_id";
        const string locationname = "location_name";
 
        //initizlize new object of datatable level 1
        DataTable dt = new DataTable();
 
        dt.Clear();
 
        //create a new colum for table level 1
        DataColumn dcol = new DataColumn(locationid, typeof(System.Int32));
        dt.Columns.Add(dcol);
 
        dcol = new DataColumn(locationname, typeof(System.String));
        dt.Columns.Add(dcol);
 
        foreach (RadTreeNode row in rtvLocations.GetAllNodes())
        {
            if (row.Checkable)
            {
                if (row.Checked)
                {
                    //Create a new row
                    DataRow drow = dt.NewRow();
                    drow[locationid] = row.Value;
                    drow[locationname] = row.Text;
                    //add row to the datatable
                    dt.Rows.Add(drow);
                }
            }
        }
 
        Session["RequestAdded"] = dt;
 
         
 
    }
 
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        /*Close radgrid edit form*/
    }
    #endregion


Your help is very appericated very well.

Thanks,
Chi Ming 

1 Answer, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 24 Apr 2013, 11:30 AM
Hi Chi Ming,

I would suggest you to review the following live example which demonstrates how you can update RadGrid with User Control edit form. Basically you need to get the selected items from your RadTreeList, create rows equal to the number of items and populate them. Then you have to add/update them in your DataTable and call AcceptChanges(). If you want to do this on button click you could follow the aforementioned steps and call Rebind() in order to refresh the grid with the new changes.

I hope this information proves helpful.

Regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Chi Ming
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Share this question
or