Asp.Net GridView order data bound columns before declarative ones - asp.net

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.

Related

How to add if statement logic in ASP.Net GridView Column

I have a GridView with the following column:
<asp:HyperLinkField DataNavigateUrlFields="agent_level" DataNavigateUrlFormatString="agent_production.aspx?agentlevel={0}" DataTextField="agent_id" HeaderText="View Agents" DataTextFormatString="View" Text="View"/>
I have been looking for a way to do the following: if the agent_level = 'b' (this value is from the database) then the "View" is not clickable.
You can use TemplateField for small business logic. If you want complex business logic, you want to consider using OnRowDataBound event.
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="View Agents">
<ItemTemplate>
<asp:HyperLink
NavigateUrl='<%# Eval("agent_level").ToString() == "b"
? "javascript:void(0)"
: "agent_production.aspx?agentlevel=" + Eval("agent_id") %>'
runat="server"
ID="AgentHyperlink" Text="View" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Prevent default Telerik sort for RadGrid

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" >

I want to put table in asp.net grid view

this is my grid view code given below>
<asp:GridView ID="gvReport" runat="server" AutoGenerateColumns="False" GridLines="None"
ShowHeader="true" HorizontalAlign="Left" OnRowCommand="gvReport_RowCommand">
<Columns>
<asp:TemplateField HeaderText="User" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Image id="img" text=<%#Eval("Coupon_Image")%>/>
</ItemTemplate>
<HeaderStyle Font-Bold="false" HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="getCoupon" runat="server" CssClass="btn" Width="145" Height="34"
CommandName="getCoupon" CommandArgument='<%#Eval("Coupon_Id")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In this it showing like this
Image 1 Button
Image 2 Button
actually i want like this type in grid view
Image1 Image2 Image3
Button Button Button
Please help me ....
advance thanks
A gridview won't work for this. Use the Table control for that, and programmably build the rows and columns. The challenge is the gridview isn't meant to render horizontally. You may be able to make it work with a repeater or ListView too, as you can have the ListView or repeater render the columns, and the layout template be the table and header row.

I cannot make the headers of my gridview click able for sorting

I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField

asp.net gridview: How can I have multiple button fields in one column?

I need to create multiple actions for a gridview - say, "Approve", "Deny", and "Return".
I can do this by creating a button field for each action:
<asp:ButtonField ButtonType="Link" CommandName="Approve" Text="Approve" />
<asp:ButtonField ButtonType="Link" CommandName="Deny" Text="Deny />
<asp:ButtonField ButtonType="Link" CommandName="Return" Text="Deny" />
However, this creates one column for each button.
Is there a way to have the same functionality, but combine them into a single column?
Have you considered using an TemplateField? Here is an example:
<asp:GridView ID="grdTest" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" CommandName="Approve" Text="Approve" />
<asp:LinkButton ID="btnDeny" runat="server" CommandName="Deny" Text="Deny" />
<asp:LinkButton ID="btnReturn" runat="server" CommandName="Return" Text="Return" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can then capture the commands the exact same way using OnRowCommand or do whatever you like. You have full control to make it behave how you need and not be bound by the built in functionality of using the regular predefined column types.
Try to put buttons into the <asp:TemplateField> instead:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Approve" Text="Approve" />
<asp:LinkButton CommandName="Deny" Text="Deny />
<asp:LinkButton CommandName="Return" Text="Deny" />
</ItemTemplate>
</asp:TemplateField>
The solution is not to use ButtonField elements.
To accomplish what you want you will need to create a column as a TemplateField and define buttons as regular ASP.NET <asp:Button id="myButton" /> within TemplateField's ItemTemplate or EditItemTemplate as appropriate for your UI.
You can handle Click events in the GridView_OnItemCommand() handler, where you can check e.CommandName to figure out exactly which button caused the event.

Resources