I tried this code to filter radgrid from the external radcombox located outside the Grid. And i have a problem with it. Visual Studio debugger and SQL Profiler show that this code works and send to radgrid correct filter expression with the correct filter value. But radgrid doesn't filtrate data with this expression while rebinding. It shows all data without any filtering after rebinding. How can I solve it? This is my code C#:
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
radgrid.MasterTableView.FilterExpression = "[service_id] LIKE '%" + ddlService.SelectedValue + "%' ";
GridColumn column = radgrid.MasterTableView.GetColumnSafe("serv_id");
column.CurrentFilterFunction = GridKnownFunction.EqualTo;
column.CurrentFilterValue = ddlService.SelectedValue;
column.AndCurrentFilterFunction = GridKnownFunction.EqualTo;
radgrid.MasterTableView.Rebind();
}
This is my ASPX code with combox setting:
<telerik:RadComboBox ID="ddlService" runat="server" Height="200" Width="240"
DropDownWidth="310" DataSourceID="dsServices"
EmptyMessage="- Select Product -"
HighlightTemplatedItems="true" CausesValidation="false"
Filter="Contains" AppendDataBoundItems="true"
onselectedindexchanged="RadComboBox1_SelectedIndexChanged"
SelectedValue='<%# radgrid.MasterTableView.GetColumn("serv_id").CurrentFilterValue %>'
AllowCustomText="true" AutoPostBack="true" DataTextField="service_name" DataValueField="id"
>
You will have to customize the below code to match your requirements. I have given you a how-to here.
In my case I need add the list of name from the combo box to the grid.
The names are stored in emp table. The data for the grid is stored in the contacts table. When I select a the names and hit add, the emp name is added as contact.
Following is my aspx.
<telerik:RadComboBox runat="server" ID="comboNames" SelectMethod="getContactName"
DataTextField="name" DataValueField="id"
EnableCheckAllItemsCheckBox="true" CheckedItemsTexts="FitInInput" CheckBoxes="true" Width="100%">
</telerik:RadComboBox>
<telerik:RadButton runat="server" ID="btnAdd" OnClick="btnAdd_Click" Text="Add"></telerik:RadButton>
<telerik:RadGrid runat="server" ID="gridContacts" Width="100%" AutoGenerateColumns="false"
AllowSorting="true" AllowMultiRowSelection="false" AllowPaging="true" PageSize="20"
GridLines="None" ShowGroupPanel="true">
<PagerStyle Mode="NextPrevNumericAndAdvanced" />
<MasterTableView Width="100%" DataKeyNames="cont_id" AllowMultiColumnSorting="true"
CommandItemDisplay="Top"
GroupLoadMode="Server" NoMasterRecordsText="No persons saved">
<CommandItemSettings AddNewRecordText="Add New"
SaveChangesText="Save Contacts"
ShowSaveChangesButton="false"
CancelChangesText="Cancel Changes"
ShowCancelChangesButton="false"
ShowExportToCsvButton="false" ShowExportToExcelButton="false" ShowExportToPdfButton="false" ShowExportToWordButton="false" />
<Columns>
<telerik:GridTemplateColumn HeaderText="Name" SortExpression="name" UniqueName="name">
<ItemTemplate>
<asp:Label runat="server" ID="lblContName" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox runat="server" ID="txtContName"></telerik:RadTextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
In the C# code do this.
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
foreach (RadComboBoxItem item in comboNames.CheckedItems)
{
int contID = Convert.ToInt32(item.Value);
tbl_contacts cont = db.tbl_contacts
.Where(c => c.cont_id == contID)
.FirstOrDefault();
cont.cont = true;
db.SaveChanges();
}
gridContacts.Rebind();
}
catch (Exception ex)
{
//Add your exception logic here
}
}
Add a needs data source for you grid.
protected void gridContacts_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
gridContacts.DataSource = db.tbl_contacts
.Where(c => c.is_cont == true)
.ToList();
}