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

Bind Combobox ID to names

3 Answers 202 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 1
Igor asked on 16 Oct 2019, 02:19 PM

Hello 

I am using RadGrid, which is using BatchEditing. I am using RadComboBox in a column, which is using LoadOnDemand.

This column is showing PROJECT_ID value in the grid. What I need to do, is to bind Column to show PROJECT_NAME.

Because of optimization, I cannot load PROJECT_NAME as a hidden column to the grid or something like that.

I need some Data source where I will have combinations PROJECT_ID  -> PROJECT_NAME and bind it to the column.

This is a solution we are using in different systems.

It is this achievable in Telerik RadGrid and RadCombobox?

Stripped demo code:

ASPX:

<%@ Page Title="Test Page" Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="TimeReport.Web.Pages.WebForm2" MasterPageFile="~/MasterPage.master" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"
   <link href="/Styles/mail.css" rel="stylesheet" />
</asp:Content>
 
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent"   runat="Server">
   <asp:Label CssClass="header" ID="Label1" runat="server" Text="content3" />
   <telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True" OnNeedDataSource="messages_NeedDataSource"
    AllowAutomaticInserts="True"
     AllowAutomaticUpdates="True" AllowPaging="True"
    AutoGenerateColumns="False"   PageSize="20"
           
    Width="100%"
    Height="100%"
    Style="border: 0; outline: none" AllowSorting="True">
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="PERSON_ACTIVITY_ID"
      HorizontalAlign="NotSet" EditMode="Batch" AutoGenerateColumns="False"
      TableLayout="Fixed">
      <BatchEditingSettings EditType="Cell" HighlightDeletedRows="true" OpenEditingEvent="DblClick" />
      <SortExpressions>
        <telerik:GridSortExpression FieldName="PERSON_ACTIVITY_ID" SortOrder="Descending" />
      </SortExpressions>
      <CommandItemSettings ShowExportToExcelButton="True" ShowExportToPdfButton="True" />
      <Columns>
         <telerik:GridTemplateColumn HeaderText="Projekt" UniqueName="PROJECT_ID" DataField="PROJECT_ID" SortExpression="PROJECT_ID" HeaderStyle-Width="15%" ItemStyle-Width="15%"  >
          <ItemTemplate>
            <%#DataBinder.Eval(Container.DataItem, "PROJECT_ID")%>
          </ItemTemplate>
          <EditItemTemplate>
            <telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="GCB_Project" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
              OnItemsRequested="PROJECT_ItemsRequested1" EnableLoadOnDemand="true" Width="100%" Height="300" ItemsPerRequest="10"  DataTextField="PROJECT_NAME" DataValueField="PROJECT_ID" >
            </telerik:RadComboBox>
          </EditItemTemplate>
 
<HeaderStyle Width="15%"></HeaderStyle>
 
<ItemStyle Width="15%"></ItemStyle>
        </telerik:GridTemplateColumn>    
 
      </Columns>
      <PagerStyle Position="TopAndBottom" AlwaysVisible="True" />
    </MasterTableView>
    <GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
 
    <ExportSettings>
      <Pdf PageWidth="">
      </Pdf>
    </ExportSettings>
 
    <ClientSettings AllowKeyboardNavigation="true" EnableRowHoverStyle="true">
      <Selecting AllowRowSelect="True" />
      <Scrolling AllowScroll="true" UseStaticHeaders="true" />
    </ClientSettings>
 
    <PagerStyle Position="TopAndBottom" AlwaysVisible="True" />
    <FilterMenu RenderMode="Lightweight"></FilterMenu>
 
    <HeaderContextMenu RenderMode="Lightweight"></HeaderContextMenu>
  </telerik:RadGrid>
 
 
</asp:Content>

CS:

using System;
using System.Data;
using Telerik.Web.UI;
 
 
namespace TimeReport.Web.Pages
{
  public partial class WebForm2 : Api.Page
  {
    private const int ItemsPerRequest = 10;
 
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    protected void PROJECT_ItemsRequested1(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
      DataTable data = new Api.Projects(TRApiSessionContext.CurrentUser.Login).LoadBasic();  //Load DataTable for ComboBox
      int itemOffset = 0;
    int endOffset = 0;
    int count = 0;
      if (data == null)
      {
        e.EndOfItems = true;
      }
      else
      {
        itemOffset = e.NumberOfItems;
        endOffset = Math.Min(itemOffset + ItemsPerRequest, data.Rows.Count);
        e.EndOfItems = endOffset == data.Rows.Count;
        count = data.Rows.Count;
      }
       
      for (int i = itemOffset; i<endOffset; i++)
      {
        ((RadComboBox)sender).Items.Add(new RadComboBoxItem(data.Rows[i]["PROJECT_FULL_NAME"].ToString(), data.Rows[i]["PROJECT_ID"].ToString()));
      }
    //  e.Message = GetStatusMessage(endOffset, count);
    }
 
    protected void messages_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
      RadGrid1.DataSource = new Api.Activities("vrabel").Load();  //Load DataTable for Grid
    }
  }
}

 

Thank you.

3 Answers, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 21 Oct 2019, 08:50 AM

Hi Igor,

The BatchEditing uses the Text property of the Controls, meaning that the Value behind an item does not count. Here is an example from the Grid - Batch Editing online demo.

The following Template column with a DropDownList extracts the CategoryName only:

<telerik:GridTemplateColumn HeaderText="Category" DefaultInsertValue="Beverages" HeaderStyle-Width="150px" UniqueName="CategoryID" DataField="CategoryID">
    <ItemTemplate>
        <%# Eval("CategoryName") %>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadDropDownList RenderMode="Lightweight" runat="server" ID="CategoryIDDropDown" DataValueField="CategoryID"
            DataTextField="CategoryName" DataSourceID="SqlDataSource2">
        </telerik:RadDropDownList>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

 

In case you want to insert values instead of text, in the BatchEditCommand event, when changes are posted to the server, access the Text coming from the grid and use this to search by in the database and extract the values (IDs). Once you have the ID, insert/update the ID field in the database instead of the Text.

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.BatchEditCommandName)
    {
        GridBatchEditingEventArgument argument = e.CommandArgument as GridBatchEditingEventArgument;
        Hashtable oldValues = argument.OldValues;
        Hashtable newValues = argument.NewValues;

        // query the database to find the ID by name
        string PROJECT_ID = "SELECT [ID] FROM [Table] WHERE PROJECT_NAME = '" + newValues["PROJECT_NAME"] + "'";
    
    // insert PROJECT_ID to database as Grid record instead of PROJECT_NAME
    }
}

 

 

Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Igor
Top achievements
Rank 1
answered on 15 Nov 2019, 09:04 AM

Hi, thanks for the reply.

This is not exactly what I asked, but that is okay now.

In the case of a Batch command, it is not optimal to search ID by Name.

I am simply binding ID to tag like this:

 

  <ItemTemplate>
            <label id='GridComboBoxValue' tag='<%#DataBinder.Eval( Container.DataItem, "PROJECT_ID")%>'><%#DataBinder.Eval( Container.DataItem, "PROJECT_NAME")%></label>
          </ItemTemplate>

Then you can get the ID on postback from a tag.

0
Attila Antal
Telerik team
answered on 18 Nov 2019, 04:13 PM

Hi Igor,

Yes, that is also a good approach. Feel free to use it.

Kind Regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Igor
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Igor
Top achievements
Rank 1
Share this question
or