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
In the Code-Behind of ASPX
ASCX
In the code-behind of Ascx
Your help is very appericated very well.
Thanks,
Chi Ming
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*/ } #endregionYour help is very appericated very well.
Thanks,
Chi Ming