Prevent default Telerik sort for RadGrid - asp.net

I am using Telerik RadGrid and I have a data source where one of my columns look like hh:mm:ss. When I load the datasource of the RadGrid from the database I order the results (on SQL level) based on a DateTime column (which contains year, month and day too), but in the grid representation I am showing just hh:mm:ss. My custom sort on SQL level is working well, in a given page I get exactly those elements which should be there. However, when I sort a column which is essentially a DateTime but is represented as hh:mm:ss, default telerik sort also occurs which sorts the rows in the given page. This is incorrect, because '09-12-2012 20:20:20' < '09-13-2012 10:10:10', but '20:20:20' > '10:10:10'. How can I prevent default telerik sort for my RadGrid? I want everything else to work in the same way, I just want to tell Telerik "do not sort my column, I've already handled the sort event, thanks". How can I achieve this?
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" AllowCustomPaging="true" AllowMultiRowSelection="true">
<MasterTableView DataKeyNames="ID" ClientDataKeyNames="ID" InsertItemPageIndexAction="ShowItemOnCurrentPage">
<CommandItemTemplate>
<div class="cmdItem">
<asp:LinkButton ID="btnManageColumns" runat="server" CommandName="Manage Columns" data-link="manage-columns">
<img alt="" src="../../Images/Icons/Columns.png" />Manage Columns</asp:LinkButton>
<asp:HiddenField ID="hdnSpacer" runat="server" />   
<asp:LinkButton ID="btnExportPDF" runat="server" CommandName="Export PDF"
OnClientClick="return exportGrid('PDF');"><img alt="" src="../../Images/Icons/ExportPDF.png" />Export to PDF</asp:LinkButton>   
<asp:LinkButton ID="btnSendEmail" runat="server" CommandName="SendEmail" OnClientClick="return exportGrid('PDFEmail');"><img alt="" src="../../Images/Icons/PasswordSetup.png"/>Email</asp:LinkButton>
</div>
</CommandItemTemplate>
</MasterTableView>
<ClientSettings>
<ClientEvents OnCommand="gridCommand" />
<Selecting AllowRowSelect="True" UseClientSelectColumnOnly="true" />
</ClientSettings>
</telerik:RadGrid>

AllowCustomSorting="true"
This attribute should be added to the MasterTableView tag.

You should be able to turn off the sort for a column, or the whole grid if that's what you want
per column
<Columns>
<tr:GridBoundColumn DataField="field" HeaderText="Description"
AllowSorting="false" />
</Columns>
whole grid
<telerik:RadGrid ID="RadGridData" runat="server" AllowSorting="false" AllowPaging="true" PageSize="50" OnNeedDataSource="RadGridData_NeedDataSource" OnItemDataBound="RadGridData_ItemDataBound">
For Custom Sorting
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" AllowCustomPaging="true" AllowMultiRowSelection="true" OnSortCommand="RadGrid1_SortCommand" >

Related

Display an image from a link in an ASP.Net GridView

I've saw ways to display binary images in a gridView - using repeaters, for example. I'm curious - how would you display an image which is stored as a link (i.e: https://s-media-cache-ak0.pinimg.com/736x/ee/dc/cb/eedccb62388bb15b8ba6564372c71bac.jpg) in a database? Is there any efficient, simple way to do it if I put the dataset source into the gridview (not using an imageField)?
You can try below code to display image in GridView which stored as a link in database. I have used simple HTML img tag to display image.
<asp:GridView ID="gvImages" CssClass="Gridview" runat="server" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">
<Columns>
<asp:BoundField HeaderText = "Image Name" DataField="ImageURL" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img src='<%# Eval("ImageURL") %>' Height="150" Width="150" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Dynamic population in gridview

I have a grid view, in which I have 1 column as template field, while other 8 columns are dynamically created.
This template field has a checkbox, while trying to retrieve this checkbox in code-behind file, we get it as null.
When columns are not dynamically created, the checkbox is pretty fine and has a value in code behind.
Below is my code:
<asp:GridView ID="gridResultSet" runat="server" AutoGenerateColumns="false" AllowSorting="true"
OnRowCreated="GridResultSet_RowCreated" OnLoad="GridResultSet_Load" CssClass="reportGrid"
CellPadding="4" OnSorting="GridResultSet_Sorting" OnRowDataBound="GridResultSet_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Reclass">
<ItemTemplate>
<acesec:CheckBox ID="chkReclass" CssClass="CheckBoxListStyle" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="gridrow" />
<AlternatingRowStyle CssClass="gridrow" />
<HeaderStyle CssClass="gridheader" />
</asp:GridView>
System.Web.UI.WebControls.CheckBox chbReclass = gridResultSet.Rows[i].FindControl("chkReclass") as System.Web.UI.WebControls.CheckBox;
Do I need to check something in order to access a template field while creation of dynamic columns?
Any body faced same kind of situation?
Pointers will be highly appreciated.
Regards

ASP.NET Dynamic Data Sorting on IDForeign Key

In my application we are using ASP.NET Dynamic Data for data binding. Everything works fine.
If you see the code snipped below, it has Dynamic Field DepartmentID which is a foreign key to Department table and gets the department name from there (this is specified in DynamicData/FieldTemplates/IDForeignKey.ascx.cs).
Our requirement is to sort (order by) the records on the department name as they are visible on the grid view. The only option that I have is to sort on DepartmentID which does not serve our purpose.
How can we sort on data that is retrieved from ForeignKey?
List.aspx
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" OnPreRender="GridView1_PreRender"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="EditHyperLink" runat="server"
NavigateUrl='<%# table.GetActionPath(PageAction.Edit, GetDataItem()) %>'
Text="Edit" /> | <asp:LinkButton ID="DeleteLinkButton" runat="server" CommandName="Delete"
CausesValidation="false" Text="Delete"
OnClientClick='return confirm("Are you sure you want to delete this item?");'
/>
</ItemTemplate>
</asp:TemplateField>
<asp:DynamicField DataField="DepartmentID" HeaderText="Department" />
<asp:DynamicField DataField="Description" HeaderText="Description"/>
</Columns>
<PagerStyle/>
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
</div>
<asp:EntityDataSource OnSelecting="GridDataSource_Selecting" ID="GridDataSource" runat="server" EnableDelete="true" OnDeleted="GridDataSource_Deleted" >
<WhereParameters>
<asp:DynamicControlParameter ControlID="DynamicFilter1" />
</WhereParameters>
</asp:EntityDataSource>
Unfortunately it is not entirely clear from your question how are describe your metadata,
but anyway you can try to use QueryExtender control and SearchExpression class. More detail information you can find by link QueryExtender and SearchExpression with examples.
The whole idea of this is use custom LINQ.

Asp.Net GridView order data bound columns before declarative ones

I have a gridview that is databound to a XmlDataSource. I also want to add a <columns> section with a buttonfield at the end of the gridview. When I add a columns section it prepends it (before xml columns). How could I order
<asp:XmlDataSource ID="xmlDataSource1" runat="server"
datafile="some.xml" TransformFile="some.xslt" />
<asp:GridView ID="linksGrid" runat="server"
DataSourceID="xmlDataSource1"
CssClass="adminGrid"
AllowPaging="True"
AlternatingRowStyle-BackColor="WhiteSmoke">
<Columns>
<asp:ButtonField runat="server" ButtonType="Button" Text="details" />
</Columns>
</asp:GridView>
this produces a table like this:
I want the order to be reversed: Title, Url, Button column
bones question, how could I space the columns so the modify button has no weight (fits to column) and the remaining cells to get even space?
The code below will surely help you.
<asp:GridView ID="MyGridView" runat="server" ShowFooter="True">
<Columns>
<asp:TemplateField >
<FooterTemplate >
<asp:Button ID="MyButton" runat="server" Text="Click Me"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The trick was
<asp:GridView AutoGenerateColumns="false .. />
I could simply use a asp:BoundField in my < columns > section to specify the order.
I will leave this open a bit if someone can provide an answer on how to CSS format individual columns.

Hyperlink in datagrid view

I want to set hyperlink field in datagrid view. When user clicks on that link, a query string should be generated and user should be directed to another page. So how can I set hyperlink to generate query string?
<asp:GridView ID="Griddata" runat="server" AutoGenerateColumns="False" CellPadding="1"
GridLines="Horizontal" Width="1000px" ShowFooter="True" CssClass="grid" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:HyperLinkField HeaderText="ID" DataTextField="rec_id" DataNavigateUrlFields="rec_id"
DataNavigateUrlFormatString="followme.aspx?record={0} " />
<asp:BoundField HeaderText="Login" DataField="LoginName"></asp:BoundField>
</Columns>
</asp:GridView>
This is a sample GridView defined in ASP.NET
You need to specify the <asp:Hyperlinkfield> in the column definition.
In that field, you need to specify the DataTextfield (is what will be displayed on screen in that column), your URL (DataNavigateUrlFormatString) and your parameter that you want to use in that URL (DataNavigateUrlFields)
Note: I'm binding to this grid from code-behind, not through a SqlDatAdaptor but the result is the same.
You will get something like this:
you can do like...
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
<a href='page.aspx?id=<#Eval("ID")>'>click</a>

Resources