How to hide and show column in gridview - c#

I have written below lines of code in order to hide and show column in gridview based on the condition.
<asp:TemplateField HeaderText="FirstName" Visible='<%# Eval("FirstName") != null ? true:false %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
It's not working!

Update in code as below :
<asp:TemplateField HeaderText="FirstName" Visible='<%= !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Edit
<asp:TemplateField HeaderText="FirstName" Visible='<%# !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Grid View
<asp:GridView ID="GridDisplay" runat="server" AutoGenerateColumns="False" BorderStyle="Groove"
BorderWidth="1px" BorderColor="ActiveCaptionText" Width="100%" EmptyDataText="No Record Found"
AllowPaging="True" OnPageIndexChanging="GridDisplay_PageIndexChanging" AllowSorting="True"
OnRowEditing="GridDisplay_RowEditing" OnRowDeleting="GridDisplay_RowDeleting"
CssClass="listbooking ListingTable">
<Columns>
<asp:TemplateField HeaderText="Booking Code">
<ItemTemplate>
<%# Eval("id")%>
</ItemTemplate>
<HeaderStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Property Code">
<ItemTemplate>
<%# Eval("property_id")%>
</ItemTemplate>
<HeaderStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("sales_username")%>
</ItemTemplate>
<HeaderStyle Width="18%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
i am trying to hide 2 columns(name).
During Binding the grid data
this.GridDisplay.Columns[2].Visible = false;

Related

Export Nested Gridview into Excel

I have the following UI design for a nested GridView. According to that I want a solution for exporting a nested Gridview into Excel.
Nested GridView
<asp:GridView ID="Mastergrid" PageSize="20" AllowPaging="true" Width="100%" Height="100%" OnPageIndexChanging="Mastergrid_PageIndexChanging" runat="server" AutoGenerateColumns="false" CssClass="gridview" DataKeyNames="r_id" OnRowDataBound="Mastergrid_RowDataBound" ShowHeaderWhenEmpty="true">
<Columns>
<asp:TemplateField HeaderText="id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblr_id" runat="server" Text='<%# Eval("r_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reg. No">
<ItemTemplate>
<asp:Label ID="lblregid" runat="server" Text='<%# Eval("r_uid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Indos No.">
<ItemTemplate>
<asp:Label ID="lblindosno" runat="server" Text='<%# Eval("indos_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Infomation">
<ItemTemplate>
<asp:GridView ID="subgrid" ShowHeader="true" runat="server" AutoGenerateColumns="false" Width="100%" Height="100%" EmptyDataText="No Record found..!!" CssClass="gridview1">
<Columns>
<asp:TemplateField HeaderText="Course id" Visible="false" HeaderStyle-Width="5%" >
<ItemTemplate>
<asp:Label ID="Course_id" runat="server" Text='<%# Eval("Course_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Name" HeaderStyle-Width="60%">
<ItemTemplate>
<asp:Label ID="lblcoursename" runat="server" Text='<%# Eval("course_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Fees" >
<ItemTemplate>
<asp:Label ID="lblcoursefees" runat="server" Text='<%# Eval("course_fees") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Date">
<ItemTemplate>
<asp:Label ID="lblcoursedate" runat="server" Text='<%# Eval("course_date","{0:dd-MM-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You need to find the nested grid with FindControl:
protected void Mastergrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "exportNestedGrid")
{
//convert the CommandArgument
int rowNumber = Convert.ToInt32(e.CommandArgument);
//find the nested GridView
GridView gridview = GridView1.Rows[rowNumber].FindControl("subgrid") as GridView;
//do stuff with the nested GridView
gridview.Visible = false;
}
}
And for getting the rowNumber, you could send it along with the export button:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" CommandArgument='<%# Container.DataItemIndex %>' CommandName="exportNestedGrid" runat="server" Text="Export nested GridView" />
</ItemTemplate>
</asp:TemplateField>
we can export Nested Gridview same as simple gridview into the excel.

how to adjust width of gridview items

how can i change the width of the gridview items? i tried adding itemstyle-width but it doesnt change anything. is it because my gridview is too crowded and does not fit into my screen or something is wrong with my codes?
<asp:GridView ID="gvModal" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" EmptyDataText="No Records" AllowPaging="true" CellPadding="4" HeaderStyle-BackColor="CornflowerBlue" BorderWidth="2" BorderColor="CornflowerBlue" Width="100%" CssClass="table table-hover" >
<Columns>
<asp:TemplateField HeaderText ="ID" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Bind ("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Type" HeaderStyle-Width="230px" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri" >
<ItemTemplate>
<asp:Label ID="lbltype" runat="server" Text='<%#Bind ("ItemType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Model" HeaderStyle-Width="240px" ItemStyle-Width="300px" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblModel" runat="server" Text='<%# Bind("ItemModel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Requested Quantity" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblQuan" runat="server" Text='<%#Bind ("ItemQuantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="AMS Balance" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblAMS" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Incoming Stock" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblIncoming" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Unit" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblUnit" runat="server" Text='<%#Bind ("ItemUnit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Date Needed" HeaderStyle-Width="180px" ItemStyle-Width="150px" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%#Bind ("ItemDate" , "{0:yyyy-MM-dd}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Description" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%#Bind ("ItemDesc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Status" ItemStyle-Width="120px" HeaderStyle-ForeColor="White" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%#Bind ("ItemStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="PO Number" HeaderStyle-ForeColor="White" ItemStyle-Width="120px" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblPONumber2" runat="server" Text='<%#Bind ("PO_Num") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="PO Date" HeaderStyle-ForeColor="White" ItemStyle-Width="150px" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblPODate2" runat="server" Text='<%#Bind ("PO_Date", "{0:yyyy-MM-dd}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Delivery Date" HeaderStyle-ForeColor="White" ItemStyle-Width="150px" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblDelDate2" runat="server" Text='<%#Bind ("Delivery_Date", "{0:yyyy-MM-dd}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Issuance Date" HeaderStyle-ForeColor="White" ItemStyle-Width="180px" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblIssDate2" runat="server" Text='<%#Bind ("IssuanceDate", "{0:yyyy-MM-dd}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Issuance Number" HeaderStyle-ForeColor="White" ItemStyle-Width="110px" ItemStyle-Font-Names="Calibri" HeaderStyle-Font-Names="Calibri">
<ItemTemplate>
<asp:Label ID="lblIssNum2" runat="server" Text='<%#Bind ("IssuanceNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Issuance" ItemStyle-Font-Names="Calibri" HeaderStyle-ForeColor="White" HeaderStyle-BackColor="#336699" ItemStyle-Width="170px">
<ItemTemplate>
<asp:Label ID ="lblIssued" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" HeaderStyle-ForeColor="White" HeaderStyle-BackColor="#336699" ItemStyle-Font-Names="Calibri" ItemStyle-Width="80px">
<ItemTemplate>
<asp:Label ID ="lblQuantity" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit" HeaderStyle-ForeColor="White" HeaderStyle-BackColor="#336699" ItemStyle-Font-Names="Calibri" ItemStyle-Width="80px">
<ItemTemplate>
<asp:Label ID ="lblUnits" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The width is specified as 100% in the gridview bit in the columns , you are using px to denote the widths, Either change the width to the actual width(in pixels),and then ensure the sum of all the template fields and bound fields are equal, or change the template field widths to percentages and ensure it totals to 100.
Consider that total width of your Grid view is 100% then you can assign proportional percentages to each columns(Please note, Sum of percentages of the TemplateField will not exceed 100). Another thing have to be noticed is, you have to set both HeaderStyle-Width and ItemStyle-Width while setting the width of the column. For example:
<asp:TemplateField HeaderText ="ID" HeaderStyle-ForeColor="White" HeaderStyle-Width="10%" ItemStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text=" ID" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
By doing like this you are setting the width of the ID column to 10% of the actual width of the Grid
The BoundField displays all content in a table cell and therefore the cell will expand as needed. The TemplateField will also be rendered as a cell BUT it contains a div which limits the width of any contents.
Example with code

how to show edit and delete button at the right side of a gridview not at left in asp.net?

i want edit and delete button on the right side of the gridview not
at left side of grid. for user friendly manipulation of gridview.
how to show buttons at right side of my grid view?
<asp:GridView ID="GvAllTasks" runat="server" AutoGenerateColumns="false"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
CssClass="table table-hover table-bordered"
onrowcancelingedit="GvAllTasks_RowCancelingEdit"
onrowdeleting="GvAllTasks_RowDeleting"
onrowediting="GvAllTasks_RowEditing" onrowupdating="GvAllTasks_RowUpdating" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
you need to set AutoGenerateColumns property false and create the required columns for the grid using property within the html source of the grid in whatever order you want. The columns can be pre defined columns like CheckBoxField, ImageField etc. or templatecolumns. The following example shows creating a grid with edit and delete buttons on right for a table with name DEPT having three columns DEPTNO, DNAME and LOC.
<asp:GridView ID="GvDept" runat="server" AutoGenerateColumns="False"
ShowFooter="true"style="z-index: 1; left: 145px; top: 22px;position:
absolute; height: 191px; width: 756px;font-family: 'Arial Black';
font-size: large" EnableViewState="False">
<Columns>
<asp:TemplateField HeaderText="Deptno">
<ItemTemplate>
<asp:Label ID="LblDno" runat="server"
Text='<%# Eval("Deptno") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dept Name">
<ItemTemplate>
<asp:Label ID="LblDname" runat="server"
Text='<%# Eval("Dname") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="LblLoc" runat="server"
Text='<%# Eval("Loc") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="LnkEdit" runat="server" Text="Edit"
CommandName="Edit" />
<asp:LinkButton ID="LnkDelete" runat="server"
Text="Delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
for adding edit and delete buttons left side of ur gridview add
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
in your columns after your template field
CssClass="table table-hover table-bordered"
onrowcancelingedit="GvAllTasks_RowCancelingEdit"
onrowdeleting="GvAllTasks_RowDeleting"
onrowediting="GvAllTasks_RowEditing" onrowupdating="GvAllTasks_RowUpdating" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
<%-- AutoGenerateEditButton="true"--%>
<%-- AutoGenerateDeleteButton="true" --%>
<Columns>
<asp:TemplateField HeaderText="Task Id">
<ItemTemplate>
<asp:HiddenField ID="HiddTaskid" runat="server" Value='<%# Eval("task_Id") %>' />
<%#Eval("task_Id")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Name">
<ItemTemplate>
<%#Eval("task_Name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned To">
<ItemTemplate>
<%#Eval("task_Employee_Name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Name">
<ItemTemplate>
<%#Eval("task_Project_Title")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Status">
<ItemTemplate>
<%#Eval("task_Status")%>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbl_Status" runat="server" Text='<%#"Status : " + Eval("task_Status") %>'></asp:Label>
<asp:DropDownList ID="Gv_DdlStatus" runat="server">
<asp:ListItem Text="-Change Status-" Value="0"></asp:ListItem>
<asp:ListItem Text="Reviewed" Value="1"></asp:ListItem>
<asp:ListItem Text="InProgress" Value="1"></asp:ListItem>
<asp:ListItem Text="Completed" Value="2"></asp:ListItem>
<asp:ListItem Text="Stopped" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="hiddstatusId" runat="server" Value='<%# Eval("task_Status") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Start dt">
<ItemTemplate>
<%#Eval("task_StartDate")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Completed dt">
<ItemTemplate>
<%#Eval("TaskCompletedDt")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskCompletedDt" runat="server" Text='<%# Eval("TaskCompletedDt") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned By">
<ItemTemplate>
<%#Eval("AssignedBy")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<%#Eval("comment")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtComments" runat="server" Text='<%# Eval("comment") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>

Populated checkbox columns in gridview

I have 8 columns populated as checkboxes in a gridview. I need to know how to get the values from them and store them in a database using C#. Kindly help.
aspx code:
<asp:GridView ID="GridView1" runat="server" CssClass="etable" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="rollnumber" HeaderText="Roll Number" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="IBM">
<ItemTemplate>
<asp:CheckBox ID="chkibm" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="FOL">
<ItemTemplate>
<asp:CheckBox ID="chkfol" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="IS">
<ItemTemplate>
<asp:CheckBox ID="chkis" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="CP">
<ItemTemplate>
<asp:CheckBox ID="chkcp" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="WIM">
<ItemTemplate>
<asp:CheckBox ID="chkwim" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="STTPO">
<ItemTemplate>
<asp:CheckBox ID="chksttpo" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="TDM">
<ItemTemplate>
<asp:CheckBox ID="chktdm" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="PA">
<ItemTemplate>
<asp:CheckBox ID="chkpa" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="ID Card">
<ItemTemplate>
<asp:CheckBox ID="chkid" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If the CheckBox Field is in database use this
<asp:CheckBoxField DataField="IBM" HeaderText="IBM"/>
instead of
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="IBM">
<ItemTemplate>
<asp:CheckBox ID="chkibm" runat="server" />
</ItemTemplate>
</asp:TemplateField>

asp.net using c# gridview sorting

i have a asp.net grid
<asp:GridView ID="GrdUsers" runat="server" AutoGenerateColumns="False"
Width="95%" BorderWidth="1px" BorderColor="Black" PagerStyle-CssClass="pgr" GridLines="Horizontal"
CssClass="DataGridStyle" AllowPaging="True" OnRowCommand="GrdUsers_RowCommand"
OnRowDeleting="GrdUsers_RowDeleting" OnRowEditing="GrdUsers_RowEditing" AllowSorting="true" OnSorting="gridView_Sorting"
OnPageIndexChanging="GrdUsers_PageIndexChanging">
<%--<EmptyDataRowStyle--%>
<EmptyDataRowStyle CssClass="pgr" />
<EmptyDataTemplate>
<asp:Label ID="lblNorecId" CssClass="notification error" runat="server" Text="No records were found using your search criteria."
Font-Bold="true" ForeColor="Brown" Font-Names="Arial" Font-Size="Medium"></asp:Label>
</EmptyDataTemplate>
<Columns>
<%-- <asp:BoundField HeaderText="Name" DataField="User_Fullname" />
<asp:BoundField HeaderText="City" DataField="User_city" />
<asp:BoundField HeaderText="State" DataField="User_state" />
<asp:BoundField HeaderText="Zip" DataField="User_zip" />
<asp:BoundField HeaderText="Joining Date" DataField="User_joiningdate" />--%>
<asp:BoundField DataField="catId" HeaderText="Categery Id" Visible="False" />
<asp:BoundField DataField="MasterCName" HeaderText="Master Categery" />
<asp:BoundField DataField="CatName" HeaderText="Sub Categery" />
<asp:BoundField DataField="GearID" HeaderText="GearID" Visible="False" />
<asp:BoundField DataField="GearName" HeaderText="Gear" />
<asp:TemplateField HeaderText="RentHourly" SortExpression="RentHourly">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text="$"></asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RentHourly") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rentdaily" SortExpression="Rentdaily">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text="$"></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Rentdaily") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RentWeekend" Visible="false">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text="$" Visible="false"></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("RentWeekend") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RentWeekly" Visible="false">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text="$" Visible="false"></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("RentWeekly") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" CommandName="edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "GearID") %>'
ImageUrl="~/img/icons/16/edit_icon.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDel" runat="server" CommandName="delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "GearID") %>'
ImageUrl="~/img/icons/16/cancel.png" />
<cc1:ConfirmButtonExtender ID="btnDel_ConfirmButtonExtender" runat="server" ConfirmText="Do You Want To Delete?"
Enabled="True" TargetControlID="btnDel">
</cc1:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPrevious" NextPageText="Next" PreviousPageText="Last" />
<RowStyle HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#DBDBDB" ForeColor="Black" HorizontalAlign="Center" />
</asp:GridView>
now i want to sort them renthourly and rentdaily in ascending and descending order
Looks like this is an old question, but your best bet is to use an ObjectDataSource to bind your grid.
Once you use the ObjectDataSource, you can have it automatically supply the SortDirection, SortColumn, CurrentPage and PageSize to the SelectMethod you have specified on the ObjectDataSource.
Inside this business method (your SelectMethod) you can cleanly issue the appropriate command to query only the data needed for the current page and apply the appropriate sorting expression.

Categories

Resources