GridView sorting with Template Field and Header Template - sorting

I'm using GridView and there is a dropdownlist in header cell for filtering.
<gm:GridView ID="routePlanGridView" runat="server" AutoGenerateColumns="False"
AllowPaging="true" PageSize="20" GridLines="Both" ShowFooter="true" CssClass="grid"
DataKeyNames="RoutePlanId" OnSorting="routePlanGridView_Sorting"
AllowSorting="true" OnPageIndexChanging="routePlanGridView_PageIndexChanging"
OnSelectedIndexChanging="routePlanGridView_SelectedIndexChanging"
OnRowDataBound="routePlanGridView_RowDataBound"
OnRowEditing="routePlanGridView_RowEditing"
OnRowUpdating="routePlanGridView_RowUpdating">
<HeaderStyle CssClass="gridHeaderFooter" />
<FooterStyle CssClass="gridHeaderFooter" />
<RowStyle CssClass="gridRow" />
<AlternatingRowStyle CssClass="gridRowAlternate" />
<Columns>
<asp:TemplateField SortExpression="SPName">
<HeaderTemplate>
SP Name<br />
<asp:DropDownList ID="spNameFilterDDL" runat="server" CssClass="gridControl" AutoPostBack="true" DataSourceID="SPNameSDS" DataTextField="SPName" OnDataBound="filterDDL_DataBound" OnSelectedIndexChanged="spNameFilterDDL_SelectedIndexChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="spNameLabel" runat="server" Text='<%# Bind("SPName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<...15 more...>
</Columns>
</gm:GridView>
and there are relevant event handlers for sorting and paging.
Now the problem is if I put "SP Name" text in a link button, page crashes when link clicked, otherwise no link produced to sort.
If I remove Header Template fields (all of them) and put a text with HeaderText attribute of TemplateField it works as expected. Is there a way to put them together? I need that dropdown and also sorting.
Thanks.

sorry, forgot to insert the code correctly - here:
<asp:LinkButton runat="server" Text="SP Name" CommandName="Sort" CommandArgument="SPName" ></asp:LinkButton>

You need to use a link button for your header text. So in place of "SP Name", use . It should work if you are handling sorting.

Related

Replacing HeaderText with Headertemplate in TemplateField

I have a gridview with templatefield columns. The TemplateFields are something like this:
<asp:TemplateField HeaderText="Title" SortExpression="name" meta:resourcekey="BoundFieldResource1">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Bind("Name") %>' meta:resourcekey="BoundFieldResource1"></asp:Label>
</ItemTemplate>
</TemplateField>
I have to add a custom attribute for to the header of this columns so I removed HeaderText and added the following:
<Headertemplate>
<asp:Label ID="lblTitleHeading" runat="server" Text="Title" data-custom="tbl-th_title_heading"></asp:Label>
</Headertemplate>
My problem is when I do this, it will break the sorting of that column and I cannot click on the header to sort it anymore, I tried changing the asp:Label to asp:LinkButton but that didn't do anything.
I would appreciate your answers.
Ok I just found out how to do this, I just needed to change the Label to LinkButton and add CommandName="Sort" and CommandArgument="name" (or any SortExpression)
<Headertemplate>
<asp:LinkButton ID="lblTitleHeading" runat="server" Text="Title" data-custom="tbl-th_title_heading" CommandName="Sort" CommandArgument="name"></asp:LinkButton>

Rebind detailtable in radgrid

i have a radgrid with detailtables ,
detailTable has "GridEditCommandColumn" and another "GridTemplateColumn" that contains linkbutton to remove row ,
<MasterTableView DataKeyNames="FEATURE_ID">
<Columns>
<telerik:GridBoundColumn DataField="FEATURE_ID" UniqueName="FeatureID" ></telerik:GridBoundColumn>
</Columns>
<DetailTables>
<telerik:GridTableView ShowHeader="true" CommandItemDisplay="Top"
SkinID="RadGridSkin" Width="100%" runat="server" Name="Fees"
EditMode="InPlace">
<Columns>
<telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
<telerik:GridBoundColumn DataField="FEE_TEMPLATE_ID" UniqueName="FeeID"></telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="DeleteFee">
<ItemTemplate>
<asp:LinkButton ID="RemoveFee" runat="server" CommandName="RemoveFee" Text="RemoveFee" ImageUrl="../../../images/cancel.png" ToolTip="<%$Resources:Strings,remove %>" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</telerik:GridTableView>
</DetailTables>
</MasterTableView>
in code behind in itemCommand event :
dataSource of radgrid is updated and Datable.acceptchanges() is called ;
how to rebind only the detailTable where changes have occurred ??
You didn't supply the name of your RadGrid, but the first thing I would try is:
RadGrid1.MasterTableView.DetailTables(0).Rebind(),
or, although I'm not sure whether this works for you (it compiles in VS for me), you might be able to say:
RadGrid1.MasterTableView.DetailTables("Fees").Rebind().
Obviously, replace 'RadGrid1' with the name of your RadGrid.

AJAX Tab Panel and Binding DataTable

I am trying to find a way to bind a data table to a tab panel (within a tabcontainer and template) without doing it on page_init or page_load. OR Binding a data table to a gridview then binding that gridview to the tab panel. I know how to bind a data table to a gridview so that's the easy part. I have to be able to - on click of a button - add or create multiple tabs in that tab container.
I want the user to be able to click a button, run some queries and then make the tab container visible and bind the data from a data table to a panel at that time (then the panel to the tab container). Do this multiple times.
Can someone provide an example or explain how I can accomplsih this? Is Update Panel my answer and where can I find some good examples?
UPDATE
I found that it was very easy to bind data to a gridview within Ajax tabs. Of course you needed you script manager ajax reference declared in the asp page and web.config file. You also need to add the ajax css style to an existing style sheet. i tried using a seperate one and it didn't work.
ASP HTML
<AjaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0"
Visible="false" ScrollBars="Both"
CssClass="Tab2" Width="1326px" Height="464px" >
<AjaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="Empty" Enabled="true" ScrollBars="Both" CssClass="Tab2">
<ContentTemplate>
<div style="overflow:auto;width:1287px; height: 418px;">
<font color="white" size="1" face="Verdana">
<asp:GridView ID="SalesOrderView1" runat="server" BackColor="White" BorderColor="#DEDFDE"visible="False"BorderStyle="None" BorderWidth="1px"
CellPadding="4" ForeColor="Black"
GridLines="Vertical" HorizontalAlign="Center">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
</div>
</font>
</ContentTemplate>
</AjaxToolkit:TabPanel>
<AjaxToolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="Empty" ScrollBars="Both" CssClass="Tab2">
<ContentTemplate >
</AjaxToolkit:TabContainer>
C# Code to bind data to the gridview on click button event.
SalesOrderView1.Visible = true;
TabPanel1.Visible = true;
TabPanel1.HeaderText = Order_List[multi_order_count];

How do I sort a DataGrid column based on a field in an embedded Repeater?

I have a DataGrid which has one column that derives its contents from a Repeater that is populated during the DataGrid's ItemDataBound event. I need to be able to sort the DataGrid rows based on the value in the Repeater. Here is a shortened sample of the DataGrid:
<DataGrid id="dgResults" Runat="server" AllowSorting="true">
<Columns>
<asp:boundcolumn HeaderText="ID" datafield="group_id" Visible="False" />
<asp:BoundColumn HeaderText="Group" DataField="group_name" SortExpression="group_name" />
<asp:TemplateColumn SortExpression="meeting_start_time">
<HeaderTemplate>
<asp:LinkButton CommandName="Sort" Text="Time" CausesValidation="False" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Repeater ID="Schedule_Repeater" runat="server">
<ItemTemplate>
<%# Eval("meeting_start_time", "{0:t}")%>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</DataGrid>
As you can see, the third column contains the meeting_start_time, whose value is displayed via a Repeater.
How do I tell the DataGrid to sort the "Time" column by the meeting_start_time values in the Repeater?

Binding navigation property to RadGrid while using EntityDataSource control

I'm new to Entity Framework and I got stuck in an issue while trying to bind a navigation property (foreign key reference) to a dropdownlist. I have Telerik RadGrid control which gets the data using a EntityDataSource control. Here is the model description:
Applications: AppId, AppName, ServerId
Servers: ServerId, ServerName
The Applicaitons.ServerId is a foreign key reference to Servers.ServerId. The RadGrid lists the applications and allows the user to insert/update/delete an application. I want to show the server names as a dropdownlist in edit mode which I'm not able to. . Here is my aspx code:
<telerik:RadGrid ID="gridApplications" runat="server" Skin="Sunset"
AllowAutomaticInserts="True" AllowAutomaticDeletes="True"
AllowPaging="True" AllowAutomaticUpdates="True"
AutoGenerateColumns="False" OnItemCreated="gridApplications_ItemCreated"
DataSourceID="applicationsEntityDataSource" Width="50%"
OnItemInserted="gridApplications_ItemInserted"
OnItemUpdated="gridApplications_ItemUpdated"
OnItemDeleted="gridApplications_ItemDeleted" GridLines="None">
<MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="False" DataKeyNames="AppId" DataSourceID="applicationsEntityDataSource">
<RowIndicatorColumn>
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderText="Edit" ItemStyle-Width="10%">
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" ConfirmText="Are you sure you want to delete this application?" ConfirmTitle="Confirm Delete" ConfirmDialogType="Classic" ItemStyle-Width="10%" HeaderText="Delete">
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="AppId" UniqueName="AppId" Visible="false" HeaderText="Application Id" ReadOnly="true">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="AppName" UniqueName="AppName" HeaderText="Application Name" MaxLength="30" ItemStyle-Width="40%">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="ServerId" UniqueName="ServerId" HeaderText="Server Hosted" EditFormColumnIndex="1">
<EditItemTemplate>
<asp:DropDownList ID="ddlServerHosted" runat="server" DataTextField="Servers.ServerName" DataValueField="ServerId" Width="40%">
</asp:DropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings ColumnNumber="2" CaptionDataField="AppId" InsertCaption="Insert New Application" EditFormType="AutoGenerated">
<EditColumn InsertText="Insert record" EditText="Edit application id #:" EditFormColumnIndex="0" UpdateText="Application updated" UniqueName="InsertCommandColumn1" CancelText="Cancel insert" ButtonType="ImageButton"></EditColumn>
<FormTableItemStyle Wrap="false" />
<FormTableStyle GridLines="Horizontal" CellPadding="2" CellSpacing="0" Height="110px" Width="110px" />
<FormTableAlternatingItemStyle Wrap="false" />
<FormStyle Width="100%" BackColor="#EEF2EA" />
<FormTableButtonRowStyle HorizontalAlign="Right" />
</EditFormSettings>
</MasterTableView>
</telerik:RadGrid>
<asp:EntityDataSource ID="applicationsEntityDataSource" runat="server"
ConnectionString="name=AnalyticsEntities" EnableDelete="True"
EntityTypeFilter="Applications" EnableInsert="True" EnableUpdate="True" EntitySetName="Applications"
DefaultContainerName="AnalyticsEntities" Include="Servers">
</asp:EntityDataSource>
I tried another approach where I replaced the GridTemplateColumn with the following code
<telerik:RadComboBox ID="RadComboBox1" DataSourceID="serversEntityDataSource" DataTextField="ServerName" DataValueField="ServerId" AppendDataBoundItems="true" runat="server" >
<Items>
<telerik:RadComboBoxItem />
</Items>
and using a separate EntityDataSource control as follows:
<asp:EntityDataSource ID="serversEntityDataSource" runat="server"
ConnectionString="name=AnalyticsEntities" EnableDelete="True"
EntityTypeFilter="Servers" EnableInsert="True" EnableUpdate="True" EntitySetName="Servers"
DefaultContainerName="AnalyticsEntities">
</asp:EntityDataSource>
but, I get the following error.
Application cannot be inserted. Reason: Entities in 'AnalyticsEntities.Applications' participate in the 'FK_Servers_Applications' relationship. 0 related 'Servers' were found. 1 'Servers' is expected.
My question is, how do you bind the navigation property and load the values in the DropDownList/RadComboBox control?
I believe you bind your dropdownlist to that second datasource instead of the grid's datasource (as you did in the second example). Then you set the SelectedValue property on the dropdown/combobox using the <%# BIND("ServerId") %> syntax within the template.

Resources