Sorting of Gridview data - c#

I have A Grid view and I want to sort the Gridview .I tried severel methods from google and they are not working in my side.
my gridvie code is like
<asp:GridView ID="gridviewShopData" runat="server" DataSourceID="SqlDataSource1" Width="100%"
AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
CssClass="contactList grid" PageSize="30" ShowHeaderWhenEmpty="true">
<Columns>
<asp:TemplateField HeaderText="Fornavn">
<ItemTemplate>
<%# Eval("Fornavn") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Efternavn">
<ItemTemplate>
<%# Eval("Efternavn") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Medarbejder nummer">
<ItemTemplate>
<%# Eval("Medarbejder nummer") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Varenummer">
<ItemTemplate>
<%# Eval("Varenummer") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle CssClass="altrow" />
<PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="50" />
<EmptyDataTemplate>
There is no data available to display!
</EmptyDataTemplate>
<PagerStyle CssClass="pager" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT firstName as 'Fornavn',lastName as 'Efternavn',employeeNumber as 'Medarbejder nummer',productID as 'Varenummer' FROM sydShopOrder where shopID=#pageid">
<SelectParameters><asp:QueryStringParameter Name="pageid" QueryStringField="id"/></SelectParameters>
</asp:SqlDataSource>
how to make it possible anyone help.

set SortExpression in template field and try
<asp:TemplateField HeaderText="Fornavn" SortExpression="Fornavn">
<ItemTemplate>
<%# Eval("Fornavn") %>
</ItemTemplate>
</asp:TemplateField>
do this for all that columns which you want to sort

Related

ASP.NET: Download file from database when clicking on file name on Grid View

I have a grid view that displays all of uploaded files of an employee (data from SQL DB).
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" AllowPaging="true" ShowFooter="false" PageSize="5"
CssClass="table" AlternatingRowStyle-BackColor="WhiteSmoke"
HeaderStyle-BackColor="#6C7A95" HeaderStyle-BorderColor="#666666" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderWidth="2" HeaderStyle-ForeColor="White"
OnPageIndexChanging="OnPaging" EmptyDataText="No Documents">
<Columns>
<asp:BoundField DataField="file_name" HeaderText="File Name" />
<asp:BoundField DataField="upload_date" HeaderText="Date (GMT -7)" />
<asp:BoundField DataField="file_status" HeaderText="Status" />
<asp:TemplateField HeaderText="Employee's Note">
<ItemTemplate>
<a data-original-title='<%# Eval("emp_note")%>' href="#" class="demo-cancel-click" rel="tooltip"><i class="icon-book"></i></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The interface look like this:
I like to archive this: the file name will be a link button (or whatever the best way is) and when click on name it will download the file. So I modified the File Name column:
<asp:TemplateField HeaderText =" File Name">
<asp:ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server"
OnClick="DownloadFile" Text='<%#Eval("file_name") %>'
CommandArgument='<%# Eval("file_id") %>'></asp:LinkButton>
</asp:ItemTemplate>
</asp:TemplateField>
But then the interface became: (file_name disappeared)
How do I get what I need?
You need to remove asp: from ItemTemplate. Your markup should look like below:
<asp:TemplateField HeaderText=" File Name">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server"
OnClick="DownloadFile" Text='<%#Eval("file_name") %>'
CommandArgument='<%# Eval("file_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Hope it helps!

Paging not working in GridView when ajax control is removed

I have a grid view in my aspx page. I have enabled paging also.
Paging is working fine when an ajax control is in page, but when I removed the ajax control paging is not working anymore. ie, on clicking page number 2 grid becomes empty.
What could be the possible reason? Please suggest a solution.
code
<asp:GridView ID="SitesGrid" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SitesDataMgr" AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="3" CellSpacing="3" OnRowDataBound="viewSite_RowDataBound" class="tbl_blck gridtable clearfix" PageSize="4" EmptyDataText="No rows found">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="clientID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White" SortExpression="ClientName">
<ItemTemplate>
<asp:Label ID="clientLabel" runat="server" Text='<%# Bind("ClientName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White" SortExpression="SiteStatus">
<ItemTemplate>
<asp:Label ID="siteLabel" runat="server" Text='<%# Bind("SiteStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-ForeColor="White" >
<ItemTemplate>
<asp:LinkButton ID="viewSite" runat="server" OnClick="viewSite_click" CausesValidation="False" CommandName="Edit"
Text="View"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="SitesDataMgr" runat="server" ConvertNullToDBNull="True" OldValuesParameterFormatString="{0}" SelectMethod="GetAllSitesByUser" FilterExpression="ClientName LIKE '%{0}%'" TypeName="Creation.DataMgr">
<SelectParameters>
<asp:SessionParameter Name="createdUserID" Type="String" SessionField="strUserName" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="ClientName" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:ObjectDataSource>
It looks like you have not added the SelectCount method to your ObjectDataSource.
This is required for the ObjectDatasource to correctly page your data.
Your select method needs the int parameters maximumRows and startRowIndex This is the default parameter names required for paging. if you do have them in. Please use them to filter your data.

Can width of a Gridview's column can be changed in percentage in codebehind?

I have a gridview having 5 columns, and last column is visible to some members only.
I want that when the last columngvMessageList.Columns[4] is inivisible, its width in percentage should be given to first columngvMessageList.Columns[0].
Please, let me know, how is it possible.
My GridView is as follow:
<asp:GridView ID="gvMessageList" runat="server" Width="100%" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true"
DataKeyNames="MESSAGE_ID" CellPadding="4" PageSize="51" EmptyDataText="No Records Found." OnSorting="gvMessageList_Sorting"
OnPageIndexChanging="gvMessageList_PageIndexChanging" OnRowDataBound="gvMessageList_RowDataBound" GridLines="None"
CssClass="table table-bordered table-condensed table-hover table-striped">
<Columns>
<asp:TemplateField HeaderText="Subject" HeaderStyle-Width="30%" SortExpression="MESSAGE_SUBJECT" HeaderStyle-BackColor="#D9EDF7"
HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:HyperLink ID="hlnkMessageSubject" runat="server" Text='<%# ((System.Data.DataRowView)Container.DataItem)["MESSAGE_SUBJECT"] %>'
NavigateUrl='<%# ((System.Data.DataRowView)Container.DataItem)["MESSAGE_URL"] %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="From" HeaderStyle-Width="14%" SortExpression="MESSAGE_FROM" HeaderStyle-BackColor="#D9EDF7"
HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:HyperLink ID="hlinkUser" runat="server" Text='<%#((System.Data.DataRowView)Container.DataItem)["MESSAGE_FROM"] %>'
NavigateUrl='<%#((System.Data.DataRowView)Container.DataItem)["FROM_URL"] %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CREATION_DATE" HeaderText="On" HeaderStyle-Width="15%" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC"
SortExpression="CREATION_DATE" />
<asp:TemplateField HeaderText="To" HeaderStyle-Width="21%" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:Label ID="lblTo" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Broadcast" HeaderStyle-Width="20%" Visible="false" HeaderStyle-BackColor="#D9EDF7" HeaderStyle-ForeColor="#0088CC">
<ItemTemplate>
<asp:Label ID="lblBroadcast" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In C# Codebehind:
I want increased percentage of first column here:
gvMessageList.Columns[4].HeaderStyle.Dispose();
gvMessageList.Columns[4].Visible = false;
Is it possible by using gvMessageList.Columns[0].HeaderStyle.Width = ?? or something like it.
Give the width property a new unit type. the first parameter is the width you want, and the second is what measurement to use. In the sample below, I'm telling the column to be 50% of the table's width.
gvMessageList.Columns[4].HeaderStyle.Width = New Unit(50, UnitType.Percentage);
You can change the width as follows
GrdDynamic.Columns[4].ItemStyle.Width=Unit.Pixel(500);
GrdDynamic.Columns[4].ItemStyle.Width=New Unit(50, UnitType.Percentage);

Populating a gridview column with an object list

I have a gridview of messages that is populated by an object class. There is a list in the object so that for each message, there is a list of recipients. Problem is, right now the gridview is only displaying "Person[]" for each message. I want it to display as a count of the recipients in the list.
Any help would be appreciated.
HTML
<asp:GridView
ID="grdMsgSent"
runat="server"
CssClass="cellSpacing"
OnRowDataBound="grdMsgSent_RowDataBound"
AllowSorting="True"
EmptyDataText="You have not sent any messages."
AllowPaging="True"
PageSize="6"
OnPageIndexChanging="grdMsgSent_PageIndexChanging"
OnSorting="grdMsgSent_Sorting"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Recipients" HeaderText="Recipients" />
</Columns>
</asp:GridView>
C#
grdMsgSent.DataSource = listSentMsg.List;
grdMsgSent.DataBind();
For example, if you have a data column name called UserID, you can populate in grid view using Eval (DataBinding Expression). So your code will be like this in aspx page,
<asp:TemplateField HeaderText="UserID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>
In C# Code Behind,
DataTable dataTable = new DataTable();
DataColumn dataColumn;
dataColumn = new DataColumn("UserID");
You can use a templatefield:
For array[], do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Length") %>' />
</ItemTemplate>
</asp:TemplateField>
For List, do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Count") %>' />
</ItemTemplate>
</asp:TemplateField>

Adding a button to the last column of a GridView

This is how i created a grid view in my ASP.NET C# project. This grid has 4 columns. I want to add a 5th column and add a button on each and every row. How can i do this ?
<asp:GridView ID="gv" runat="server" CellPadding="1" Width="900px"/>
Set AutoGenerateColumns="False" in the gridview markup
Define columns as BoundField (or TemplateField if you wish)
Add aTemplateField for the button in the last column
Sum up:
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1" Width="900px">
<Columns>
<%-- <asp:BoundField /> Definitions here --%>
...
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Click ME" runat="server" ID="btn" OnClick="Clicked" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
suppose you are binding data table having 4 column say col1,col2,col3 and col4
then your gird view in .aspx will be like;
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1" Width="900px" OnRowCommand="gv_RowCommand">
<Columns>
<asp:TemplateField HeaderText="col1">
<ItemTemplate>
<%#Eval("col1")%>// "col1" is field of your database
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col2">
<ItemTemplate>
<%#Eval("col2")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col3">
<ItemTemplate>
<%#Eval("col3")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col4">
<ItemTemplate>
<%#Eval("col4")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnOK" runat="server" Text="OK" CommandName="show" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
*.cs page will be *
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "show")
{
//your code on click event
}
}

Categories