Hello,
We have certain problem by RadGrid filter. We need to bind data using DataTable class. We have prepared data for RadGrid by combination of data from database with computed data. Therefore,the SqlDataSource is not useful for us. We have found example regarding mentioned scenario in your forum. The example uses ItemCreated event, but it doesn’t work. Data is binding correctly, however the filtering doesn’t work.
We consider the solution based on DataTable (stored
in a session) shared by controls inherited from a GridTemplateColumn.
However, this approach seems to be complicated for described aim which appears
to be trivial.
So, is there any way to accomplish the DataTable binding as a data source
for the RadGrid without harm the filtering?
Thank yout very much for your advice.
<telerik:RadGrid ID="RadGridData" runat="server" Width="100%" AutoGenerateColumns="false"
AllowPaging="true" PageSize="10" ShowGroupPanel="false" ShowFooter="true" AllowFilteringByColumn="true"
OnPreRender="RadGridData_PreRender" AllowSorting="true" AllowMultiRowSelection="false"
GridLines="None" EnableAjaxSkinRendering="true" EnableAJAX="true"
onneeddatasource="RadGridData_NeedDataSource" OnItemCreated="RadGridData_ItemCreated" ViewStateMode="Disabled">
<ExportSettings ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true" />
<MasterTableView AutoGenerateColumns="false" EditMode="InPlace" AllowFilteringByColumn="True"
ShowFooter="True" TableLayout="Auto" DataKeyNames="Id" CommandItemDisplay="Top">
<CommandItemSettings ShowRefreshButton="false" ShowAddNewRecordButton="false" ShowExportToWordButton="true" ShowExportToExcelButton="true"
ShowExportToCsvButton="true" />
<Columns>
<telerik:GridBoundColumn UniqueName="Caption" DataField="Caption" HeaderText="Caption"
AllowFiltering="true" DataType="System.String">
<HeaderStyle Width="25%" />
<FilterTemplate>
<telerik:RadComboBox ID="RadComboBoxCaption" DataTextField="Caption" DataValueField="Caption" Height="200px" AppendDataBoundItems="true"
SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("Caption").CurrentFilterValue %>'
runat="server" OnClientSelectedIndexChanged="CaptionChanged">
<Items>
<telerik:RadComboBoxItem Text="All" />
</Items>
</telerik:RadComboBox>
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<script type="text/javascript">
function CaptionChanged(sender, args) {
var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
tableView.filter("Caption", args.get_item().get_value(), "EqualTo");
}
</script>
</telerik:RadScriptBlock>
</FilterTemplate>
</telerik:GridBoundColumn>
<telerik:GridDateTimeColumn DataField="DateTime" HeaderText="RTU Date and time" SortExpression="DateTime"
UniqueName="DateTime" PickerType="DateTimePicker" AllowFiltering="false">
<HeaderStyle Width="25%" />
</telerik:GridDateTimeColumn>
<telerik:GridBoundColumn UniqueName="FormatedValue" DataField="FormatedValue" HeaderText="Value"
AllowFiltering="true" DataType="System.String">
<HeaderStyle Width="25%" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn UniqueName="State" DataField="State" HeaderText="State"
AllowFiltering="true" DataType="System.String">
<HeaderStyle Width="25%" />
</telerik:GridBoundColumn>
</Columns>
<PagerStyle Mode="NextPrevAndNumeric" />
</MasterTableView>
</telerik:RadGrid>
protected void RadGridData_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridFilteringItem)
{
GridFilteringItem filterItem = (GridFilteringItem)e.Item;
RadComboBox combo = (RadComboBox)filterItem["Caption"].FindControl("RadComboBoxCaption");
combo.DataSource = dataTable; // this datatable is also used for binding data for table
combo.DataBind();
combo.AutoPostBack = true;
}
}