1

I am following below link for Load On Demand functionality to a RadComboBox which is inside RadGrid.
Link: http://demos.telerik.com/aspnet-ajax/combobox/examples/populatingwithdata/autocompletesql/defaultcs.aspx
The binding in Inside RadComboBox depends upon the Item selected from Outside RadComboBox which is outside of RadGrid. i.e., If Outside RadComboBox has Item: A, B, C, D, E etc then the binding in Inside RadComboBox will be as: A1 to An, B1 to Bn, C1-Cn, etc depending upon the item selected from Outside RadComboBox.

Now I am having 3 issues:
1) How to write default text as "Select Item" in Inside ComboBox using c# code at 0th index, under ItemRequested event ?
2) In Edit/Update button click, RadComboBox selected value is not showing at run time. I am able to see the value in debug mode but at run time it never show.
3) The search functionality in the RadComboBox in above Line, is not working as expected at my side.
i.e., in Demo, search is among A1 to A100 (total records) and return msg Items A1-A8 out of A8 .... i.e., among all items (A100) it got only till A8.
and my side, search is among A1 to A50 (ItemsPerPage) and return msg Items A1 to A50 out of A2000 (total records) .... i.e., among all items (A2000) it got batch/page/ItemPerPage wise, then when i scroll down it shows more search result.
I dont want this to happen, I want it should work same as working in Demo/Link for search.

Below is my current code:
C# Code:

protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        //bind dropdwon while "Add" 
        string CompanyCode = ddlCompany.SelectedValue.ToString();
        GridEditableItem item = (GridEditableItem)e.Item;

        if (!(e.Item is IGridInsertItem))
        {
            RadComboBox rcb = (RadComboBox)item.FindControl("ddlAccountCode");
            //Select particular dropdown value while "Edit"
            Label lblAcCode2 = item.FindControl("lblAcCode2") as Label;
            if (!string.IsNullOrEmpty(lblAcCode2.Text))
            {
                rcb.SelectedValue = Session["AccountDescription"].ToString();
                string selVal = rcb.SelectedValue;
            }
        }
    }        
}    
public DataTable GetAccCode(string CompanyCode)
{
    SqlConnection con = new SqlConnection(strcon);
    SqlCommand cmd = new SqlCommand("[Invoice].[usp_tbl_AccountCode_DL_Test]", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@CompanyCode", CompanyCode);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    try
    {
        con.Open();
        da.Fill(dt);
        con.Close();
    }
    catch (Exception ex)
    {
    }
    return dt;
}    
//#Load on Demand
private const int ItemsPerRequest = 50;

private static string GetStatusMessage(int offset, int total)
{
    if (total <= 0)
        return "No matches";

    return String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", offset, total);
}    
protected void ddlAccountCode_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
    string c = ddlCompany.SelectedValue.ToString();

    DataTable dt = new DataTable();
    dt = GetAccCode(c);

    RadComboBox combo = (RadComboBox)sender;

    int itemOffset = e.NumberOfItems;
    int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
    e.EndOfItems = endOffset == dt.Rows.Count;

    for (int i = itemOffset; i < endOffset; i++)
    {    
        combo.Items.Add(new RadComboBoxItem(dt.Rows[i]["AccountDescription"].ToString(), dt.Rows[i]["AccountDescription"].ToString()));
    }

    e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
}
protected void ddlAccountCode_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    string a = e.Value;
    Session["AccountCodeID"] = a;

    string b = e.Text;
    Session["AccountDescription"] = b;
}

HTML Code:

<telerik:RadComboBox ID="ddlCompany" runat="server" Height="200" Width="240"
          DropDownWidth="310" EmptyMessage="- Select Product -" HighlightTemplatedItems="true" CausesValidation="false"
          Filter="Contains" AppendDataBoundItems="true" AllowCustomText="true" AutoPostBack="true"
          DataTextField="Title" DataValueField="Code" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
        </telerik:RadComboBox>

        <br />

        <telerik:RadAjaxPanel ID="RadAjaxPanel4" runat="server">
                <telerik:RadGrid ID="RGGSTAcCode" runat="server"
                   ShowFooter="True" GroupingEnabled="False" ShowStatusBar="true" EmptyDataText="No record available." 
                   AllowAutomaticInserts="False" AllowAutomaticUpdates="False" AllowAutomaticDeletes="true" 
                   OnNeedDataSource="RGGSTAcCode_NeedDataSource" OnItemDataBound="RGGSTAcCode_ItemDataBound" 
                   OnInsertCommand="RGGSTAcCode_InsertCommand" OnDeleteCommand="RGGSTAcCode_DeleteCommand" 
                   OnUpdateCommand="RGGSTAcCode_UpdateCommand" OnItemCommand="RGGSTAcCode_ItemCommand">
                  <mastertableview ShowHeadersWhenNoRecords="true" autogeneratecolumns="false" datakeynames="AccountCodeID" InsertItemDisplay="Top"
                    insertitempageindexaction="ShowItemOnCurrentPage" ShowFooter="True" CommandItemDisplay="Top" ClientIDMode="Static">                                   
                         <Columns> 
                             <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn> 

                             <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
                                <ItemTemplate>
                                  <asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                   <asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>' Visible="false"></asp:Label>

                                   <telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="240" DropDownWidth="310" HighlightTemplatedItems="true" CausesValidation="true"       
                                       OnItemsRequested="ddlAccountCode_ItemsRequested" ItemsPerRequest="10"
                                       EnableLoadOnDemand="True" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
                                       AutoPostBack="true" OnSelectedIndexChanged="ddlAccountCode_SelectedIndexChanged"

                                       Filter="Contains" AppendDataBoundItems="true" DataTextField="AccountDescription" DataValueField="AccountCodeID">
                                   </telerik:RadComboBox>                                   

                                </EditItemTemplate>
                             </telerik:GridTemplateColumn>

                             <telerik:GridBoundColumn DataField="AccountDescription" HeaderText="Description" UniqueName="AccountDescription" SortExpression="AccountDescription" InsertVisiblityMode="AlwaysHidden" ReadOnly="true" ></telerik:GridBoundColumn>
                             <telerik:GridBoundColumn aggregate="SUM" DataField="Amount" HeaderText="Amount" FooterAggregateFormatString="Total : {0:###,##0.00}" DataFormatString="{0:n}" FooterStyle-BackColor="#ffc04c" UniqueName="Amount" SortExpression="Amount"></telerik:GridBoundColumn>
                             <telerik:GridBoundColumn DataField="Remark" HeaderText="IFCA Remark" UniqueName="Remark" SortExpression="Remark">

                             </telerik:GridBoundColumn>    

                             <telerik:GridButtonColumn ConfirmTextFormatString="Are you sure you want to Delete {0} Account Code?" ConfirmTextFields="AccountCodeID"
                             ConfirmDialogType="RadWindow" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"></telerik:GridButtonColumn>                                                                            
                      </Columns>
                      <EditFormSettings>
                         <EditColumn ButtonType="ImageButton" />
                      </EditFormSettings>
                      <CommandItemSettings AddNewRecordText="Add new record" RefreshText="Refresh"></CommandItemSettings>
                  </mastertableview>
                </telerik:RadGrid>      
        </telerik:RadAjaxPanel>

I am unable to solve the above 3 issues. Please reply.
Please note that I am very new in Telerik. Thanks in advance

timz_123
  • 435
  • 1
  • 9
  • 47

1 Answers1

0

Below code resolved my issues:
2nd issue (RadComboBox not showing SelectedValue at run time while Edit):

protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            //bind dropdwon while "Add" 
            string CompanyCode = ddlCompany.SelectedValue.ToString();
            GridEditableItem item = (GridEditableItem)e.Item;

            if (!(e.Item is IGridInsertItem))
            {
                RadComboBox rcb = (RadComboBox)item.FindControl("ddlAccountCode");

                //bind combobox in Edit mode from DB when using LoadOnDemand
                RadComboBoxItem rcbi = new RadComboBoxItem();
                rcbi.Text = ((DataRowView)e.Item.DataItem)["AccountCode"].ToString() + '-' + ((DataRowView)e.Item.DataItem)["AccountDescription"].ToString();
                rcbi.Value = ((DataRowView)e.Item.DataItem)["AccountCodeID"].ToString();
                rcb.Items.Add(rcbi);
                rcbi.DataBind();
            }
        }
    }

3rd Issue solution:

    //#Load on Demand
        private const int ItemsPerRequest = 50;

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "No matches";

            return String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", offset, total);
        }

    public DataTable GetAccCode(string CompanyCode)
    {
        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("[Invoice].[usp_tbl_AccountCode_DL_Test]", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@CompanyCode", CompanyCode);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        try
        {
            con.Open();
            da.Fill(dt);
            con.Close();
        }
        catch (Exception ex)
        {
        }
        return dt;
    }
    protected void ddlAccountCode_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            string c = ddlCompany.SelectedValue.ToString();

            DataTable dt = GetAccCode(c);
            DataView dv = new DataView(dt);
            string txt = e.Text;
            dv.RowFilter = string.Format("AccountDescription LIKE '%{0}%'", txt);       
            int a = dv.Count;
            if (dv.Count > 0)
            {
                dt = dv.ToTable();
            }

            RadComboBox combo = (RadComboBox)sender;

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = endOffset == dt.Rows.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                combo.Items.Add(new RadComboBoxItem(dt.Rows[i]["AccountDescription"].ToString(), dt.Rows[i]["AccountDescription"].ToString()));
            }

            if (!string.IsNullOrEmpty(e.Text))
            {
                int num = dv.Count;
                endOffset = dv.Count;
            }

            e.Message = GetStatusMessage(endOffset, dt.Rows.Count); 
        }
        //#End of 'Load on Demand'
timz_123
  • 435
  • 1
  • 9
  • 47