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

Databinding problem

3 Answers 57 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SCT
Top achievements
Rank 1
SCT asked on 19 Jul 2011, 01:45 PM

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;

            }

        }

3 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 22 Jul 2011, 11:03 AM
Hello,

 Can you specify what exact error is thrown when you bind the combo box in the ItemCreated event. You should check whether the selected value is present in the datasource items. I applied this approach for the first grid in the following demo and it was working properly. You can find attached the modified source of the demo.

Best wishes,
Marin
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
SCT
Top achievements
Rank 1
answered on 09 Aug 2011, 10:34 AM
Hello Marin,

thank you very much for your solution. We've tried it, however the items was shown twofold.
Here's the code snippet:

        protected void RadGridLog_ItemCreated(object sender, GridItemEventArgs e)

        {

            if (e.Item is GridFilteringItem)

            {

                GridFilteringItem filterItem = (GridFilteringItem)e.Item;

                RadComboBox combo = (RadComboBox)filterItem["Value"].FindControl("RadComboBoxState");

 

                combo.DataSource = new[] { new { Value = "All"},

                    new { Value = "Connected"},

                    new { Value = "Connecting"},

                    new { Value = "Disconnected"} };

                combo.DataBind();

            }

        }


Mentioned problem roots in a fact that items appear in the DataSource as well as in the >>Items<< property. In such case, the filter's combobox contains the item twofold.

By using of construction like:

protected void RadGridLog_ItemCreated(object sender, GridItemEventArgs e)

        {

            if (e.Item is GridFilteringItem)

            {

                GridFilteringItem filterItem = (GridFilteringItem)e.Item;

                RadComboBox combo = (RadComboBox)filterItem["Value"].FindControl("RadComboBoxState");

 

                combo.Items.Add(new RadComboBoxItem("All"));

                combo.Items.Add(new RadComboBoxItem("Connected"));

                combo.Items.Add(new RadComboBoxItem("Connecting"));

                combo.Items.Add(new RadComboBoxItem("Disconnected"));

 

            }

        }


the items are shown correctly, however:
1. The filtering does not consider these items. (Such ignoration was the original problem).
2. Filter does not remember the selected item.


We'll be grateful for any suggestions or ideas relating to the issue.
SCT
0
Pavlina
Telerik team
answered on 12 Aug 2011, 10:13 AM
Hi Sct,

I tested the project which my colleague Marin sent you in the previous post and it is working as expected on my side. Can you please verify that the issue is also replicated in the attached project. If you manage to reproduce what you are getting in this project, please let me know what steps to follow, so that we can test it too.

Kind regards,
Pavlina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
SCT
Top achievements
Rank 1
Answers by
Marin
Telerik team
SCT
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or