All,
I am having problems getting my GridDropDownColumn to bind with the current value of my Grid data row. I can get the drop down to bind to it's source but I can't seem to get it to set the correct selected value automatically.
Here is my ASPC Code:
Here is my ASPX C# code:
I am having problems getting my GridDropDownColumn to bind with the current value of my Grid data row. I can get the drop down to bind to it's source but I can't seem to get it to set the correct selected value automatically.
Here is my ASPC Code:
<%@ Page Language="C#" MasterPageFile="~/MasterPages/Popup.Master" AutoEventWireup="true" CodeBehind="CompanyItemReference.aspx.cs" Inherits="NTE.NTEx.Web.App.ItemMaster.CompanyItemReference" Title="Item Reference" %> <asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server"> <center><asp:Label ID="Label1" CssClass="NormalTextBold" runat="server" ></asp:Label></center> <telerik:RadGrid ID="RadGrid1" Skin="Web20" runat="server" Width="100%" AutoGenerateColumns="False" HorizontalAlign="Right" BorderStyle="None" BorderWidth="0px" CellPadding="0" CellSpacing="0" ItemStyle-Height="10px" HeaderStyle-Height="10px" OnNeedDataSource="RadGrid1_NeedDataSource" OnDeleteCommand="RadGrid1_DeleteCommand" OnInsertCommand="RadGrid1_InsertCommand" OnUpdateCommand="RadGrid1_UpdateCommand" OnItemDataBound="RadGrid1_ItemDataBound" PageSize="20" AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True" GridLines="Horizontal" ShowStatusBar="false"> <PagerStyle Mode="NumericPages"></PagerStyle> <ClientSettings AllowExpandCollapse="True"> <Resizing AllowColumnResize="true" ResizeGridOnColumnResize="false" AllowRowResize="true" /> </ClientSettings> <MasterTableView CommandItemDisplay="Top" AllowFilteringByColumn="True" DataKeyNames="item_id" AllowMultiColumnSorting="True" HierarchyLoadMode="ServerOnDemand" Width="100%" HierarchyDefaultExpanded="False" runat="server" Name="Item"> <DetailTables> <telerik:GridTableView AllowPaging="false" CommandItemDisplay="Top" DataKeyNames="id" Width="80%" runat="server" Name="Reference"> <ParentTableRelation> <telerik:GridRelationFields DetailKeyField="item_id" MasterKeyField="item_id" /> </ParentTableRelation> <Columns> <telerik:GridEditCommandColumn Resizable="false" HeaderStyle-Width="30px" ItemStyle-Width="30px" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn> <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="id" HeaderText="Detail ID" DataField="id"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="Item_ID" HeaderText="Item ID" DataField="item_id"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="HoldReferenceType" DataField="reference_type"> </telerik:GridBoundColumn> <telerik:GridDropDownColumn UniqueName="DDReferenceType" SortExpression="display_name" ListTextField="display_name" EnableEmptyListItem = "true" EmptyListItemText="--Choose an option--" EmptyListItemValue="" ListValueField ="Reference_Type" HeaderText="Reference Type" DataField="reference_type"> </telerik:GridDropDownColumn> <telerik:GridBoundColumn ReadOnly="true" UniqueName="reference_name" HeaderText="Name" DataField="reference_name"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="reference_value" HeaderText="Value" DataField="reference_value"> </telerik:GridBoundColumn> <telerik:GridButtonColumn ConfirmText="Delete this Item?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1"> <HeaderStyle Width="20px" /> <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> </telerik:GridButtonColumn> </Columns> <SortExpressions> <telerik:GridSortExpression FieldName="reference_name"></telerik:GridSortExpression> </SortExpressions> </telerik:GridTableView> </DetailTables> <Columns> <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="item_id" HeaderText="item_ID" DataField="item_id"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="MemNo" HeaderText="Member" DataField="Mem_No"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="GroupID" HeaderText="Community" DataField="Group_Id"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" visible="false" UniqueName="type" HeaderText="Type" DataField="owner_reference_type"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="reference" HeaderText="Product ID" FilterControlWidth="50px" HeaderStyle-Width="100px" ItemStyle-Width="100px" DataField="owner_reference_value"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="Commodity" HeaderText="Commodity" ReadOnly="true" ShowFilterIcon="false" FilterControlAltText="Enter a commodity and hit return" FilterControlWidth="20px" AutoPostBackOnFilter="true" HeaderStyle-Width="30px" ItemStyle-Width="30px" DataField="Commodity_code"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" UniqueName="UPC" HeaderText="UPC" Resizable="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" DataField="UPC"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" UniqueName="GTIN" HeaderText="GTIN" HeaderStyle-Width="120px" ItemStyle-Width="120px" DataField="GTIN"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" UniqueName="Branch" HeaderText="Branch" ShowFilterIcon="false" FilterControlAltText="Enter a branch and hit return" FilterControlWidth="20px" AutoPostBackOnFilter="true" HeaderStyle-Width="30px" ItemStyle-Width="30px" DataField="Branch"> </telerik:GridBoundColumn> <telerik:GridBoundColumn ReadOnly="true" UniqueName="Description" HeaderText="Description" AllowFiltering="false" DataField="Description"> </telerik:GridBoundColumn> <telerik:GridButtonColumn ConfirmText="Delete this Item?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1"> <HeaderStyle Width="20px" /> <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> </telerik:GridButtonColumn> </Columns> <SortExpressions> <telerik:GridSortExpression FieldName="owner_reference_value"></telerik:GridSortExpression> </SortExpressions> </MasterTableView> </telerik:RadGrid> </asp:Content> Here is my ASPX C# code:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NTE.NTEx.Web.Common; using NTE.NTEx.BusinessLogic; using Telerik.Web.UI; using SubSonic; //Query Object using NTE.NTEx.Data; using NTE.NTEx.Web.Session; using NTE.NTEx.DataAccessObjects; using NTE.NTEx.Security; using System.Text; namespace NTE.NTEx.Web.App.ItemMaster { [Page("Item References", Description = "Item References")] public partial class CompanyItemReference : BasePage { private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack)//first time hit page { SetData("MemNo", Request.QueryString["MemNo"]); SetData("GroupID", Request.QueryString["GroupID"]); SetData("ReadOnly", Request.QueryString["ReadOnly"]); if (string.IsNullOrEmpty(Data["WHERE"]) == true) { SetWhereClause(); } } } protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) { LoadData(); } private void SetWhereClause() { StringBuilder strSQL = new StringBuilder(); DbParams parameters = new DbParams(); strSQL.Append(" Select get_mem_group_filter (:1, :2, 'mem_no','group_id') where_clause from ntex_session" ); parameters.Add("1", Data["MemNo"]); parameters.Add("2", Data["GroupId"]); DataSet ds = DataHelper.FetchData(strSQL.ToString(), parameters); SetData("WHERE", (string) ds.Tables[0].Rows[0]["where_clause"]); } private void LoadData() { StringBuilder strSQL = new StringBuilder(); //Category Query DbParams parameters = new DbParams(); strSQL.Append(" select * from company_item2"); strSQL.Append(" where mem_no = :1 "); strSQL.Append(Data["Where"]); parameters.Add("1", Data["MemNo"]); DataSet ds = DataHelper.FetchData(strSQL.ToString(), parameters); RadGrid1.AutoGenerateColumns = false; RadGrid1.DataSource = ds.Tables[0]; RadGrid1.MasterTableView.DataSource = ds.Tables[0]; strSQL.Remove(0, strSQL.Length); parameters.Clear(); strSQL.Append(" select * from company_item"); strSQL.Append(" where mem_no = :1 "); strSQL.Append(Data["Where"]); parameters.Add("1", Data["MemNo"]); ds = DataHelper.FetchData(strSQL.ToString(), parameters); RadGrid1.AutoGenerateColumns = false; RadGrid1.MasterTableView.DetailTables[0].DataSource = ds.Tables[0]; } protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) { string tableName = e.Item.OwnerTableView.Name.ToString(); if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode && tableName=="Reference") { GridEditableItem editedItem = e.Item as GridEditableItem; GridEditManager editMan = editedItem.EditManager; StringBuilder strSQL = new StringBuilder(); strSQL.Append(" select a.reference_type, a.display_name from reference_master_type a where reference_master_id = to_number(getcontextattribute('"+Data["MemNo"]+"',"+Data["GroupId"]+",'REFMSTRID'))"); DataSet ds = DataHelper.FetchData(strSQL.ToString()); // GridDropDownListColumnEditor editor = editMan.GetColumnEditor("DDReferenceType") as GridDropDownListColumnEditor; //in case you have RadComboBox editor for the GridDropDownColumn (this is the default editor), //you will need to use ComboBoxControl below instead of DropDownListControl //Then you can modify the list control as per your custom conventions editor.DataSource = ds.Tables[0]; editor.DataBind(); //editor.Visible = true; //editor.ComboBoxControl.SelectedValue = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id"].ToString(); //And so on } } protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) { //Get the GridEditableItem of the RadGrid string tableName = e.Item.OwnerTableView.Name.ToString(); GridEditableItem editedItem = e.Item as GridEditableItem; //try //{ switch (tableName) { case "Item": { string str_Reference = (editedItem["reference"].Controls[0] as TextBox).Text; ItemCollection colIC = new ItemCollection(); Query qry = new Query(Item.Schema); qry.AddWhere("owner_reference", str_Reference); colIC.LoadAndCloseReader (qry.ExecuteReader()); if (colIC.Count == 1) { ItemCorporationUsageCollection colICU = new ItemCorporationUsageCollection(); qry = new Query(ItemCorporationUsage.Schema); qry.AddWhere("item_id", colIC[0].Id).AND("related_corporation_id", int.Parse(Data["MemNo"])); colICU.LoadAndCloseReader (qry.ExecuteReader()); if (colICU.Count == 0) { DataAccessObjects.ItemCorporationUsage oICU; oICU = new ItemCorporationUsage(); oICU.RelatedCorporationId = int.Parse(Data["MemNo"]); oICU.ItemId = colIC[0].Id; oICU.Save(); } } LoadData(); DisplayMessage("Add Successfull."); break; } case "Reference": { int itemid = int.Parse(editedItem.OwnerTableView.ParentItem.OwnerTableView.DataKeyValues[editedItem.OwnerTableView.ParentItem.ItemIndex]["item_id"].ToString()); DropDownList list = editedItem.FindControl("DDReferenceType") as DropDownList; Response.Write(list.Items.Count +"<BR>"); Response.Write(editedItem["DDReferenceType"].Controls[0].ToString()); DataAccessObjects.ItemCorporationReference oICR; oICR = new ItemCorporationReference(); oICR.RelatedCorporationId = int.Parse(Data["MemNo"]); oICR.ItemId = itemid; //oICR.ReferenceType = (editedItem.FindControl("DDReferenceType") as DropDownList).SelectedItem.Value; oICR.Reference = (editedItem["reference_value"].Controls[0] as TextBox).Text; //oICR.Save(); //LoadData(); DisplayMessage("Insert Successfull."); break; } } //} //catch //{ // Response.Write("Error: Parameter could not be saved."); //} } protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) { if (e.CommandName == RadGrid.EditCommandName) { Response.Write("Primary key for the clicked item from ItemCommand: " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["item_id"] + "<br>"); Response.Write("Item Index for the clicked item from ItemCommand: " + e.Item.ItemIndex + "<br>"); DisplayMessage("Table Name clicked item from ItemCommand: " + e.Item.OwnerTableView.Name.ToString() + "<br>"); } } protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) { //Get the GridEditableItem of the RadGrid GridEditableItem editedItem = e.Item as GridEditableItem; string tableName = e.Item.OwnerTableView.Name.ToString(); try { switch (tableName) { case "Item": { int ItemID = int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["item_id"].ToString()); Query qry = new Query(ItemCorporationReference.Schema); qry.QueryType = QueryType.Delete; qry.AddWhere("item_id", ItemID).AND("related_corporation_id", int.Parse(Data["MemNo"])); qry.Execute(); qry = new Query(ItemCorporationUsage.Schema); qry.QueryType = QueryType.Delete; qry.AddWhere("item_id", ItemID).AND("related_corporation_id", int.Parse(Data["MemNo"])); qry.Execute(); LoadData(); DisplayMessage("Delete Successfull."); break; } case "Reference": { int DeleteID = int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id"].ToString()); DataAccessObjects.ItemCorporationReference.Delete(DeleteID); DisplayMessage("Delete Successfull."); break; } } } catch { DisplayMessage("Error: Parameter could not be saved."); } } protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) { //Get the GridEditableItem of the RadGrid GridEditableItem editedItem = e.Item as GridEditableItem; string tableName = e.Item.OwnerTableView.Name.ToString(); try { switch (tableName) { case "Item": { break; } case "Reference": { int UpdateID = int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id"].ToString()); DataAccessObjects.ItemCorporationReference oICR; oICR = new ItemCorporationReference(UpdateID); oICR.ReferenceType = (editedItem["DDReferenceType"].Controls[0] as DropDownList).SelectedItem.Value; oICR.Reference = (editedItem["reference_value"].Controls[0] as TextBox).Text; oICR.Save(); DisplayMessage("Delete Successfull."); break; } } } catch { DisplayMessage("Error: Parameter could not be saved."); } } private void DisplayMessage(string text) { RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text))); } } }