Sir good morning
My sort expression is not working in GridView, please help
<%-- Start Generator List --%>
<div class="container mb-0 mt-0" style="font-size: 12px; font-family: Arial;">
<asp:Panel ID="PnlGenList" runat="server">
<div class="row g-0 pt-0 " style="">
<div class="col col-12 justify-content-center">
<asp:GridView ID="GvGenList" runat="server"
GridLines="Both"
EmptyDataText="No Record Found"
EmptyDataRowStyle-ForeColor="Red"
EmptyDataRowStyle-Font-Bold="true"
EmptyDataRowStyle-BorderStyle="None"
CssClass=" MyDataGridViewListOne"
HorizontalAlign="Center"
AllowSorting="true"
OnSorting="OnSorting"
RowStyle-CssClass="rows"
AutoGenerateColumns="false">
<EmptyDataRowStyle BorderStyle="None" Font-Bold="True" ForeColor="Red"></EmptyDataRowStyle>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle CssClass="rows"></RowStyle>
<Columns>
<asp:TemplateField HeaderText="Sr.No" ItemStyle-Width="30" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LnkSelectCustomer" runat="server" Text="Select" OnClick="ViewInvoiceDetail" ForeColor="Blue" Font-Size="14px" Font-Bold="true"
CommandArgument='<%# Eval("CustomerCode") + ";" + Eval("RegNo") + ";" + Eval("ModelCode") + ";" + Eval("CustomerCode") + ";" + Eval("NetAmount") + ";" + Eval("Paid") + ";" + Eval("Balance") + ";" + Eval("JobNoDetail") + ";" + Eval("InvoiceDate") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LnkSelectCustomer" runat="server" Text="Select" OnClick="ViewGenDetail" ForeColor="Blue" Font-Size="14px" Font-Bold="true"
CommandArgument='<%# Eval("GenCode") + ";" + Eval("GenName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="GenCode"
HeaderText="Gen Code"
SortExpression="GenCode"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="GenName"
HeaderText="Gen Name"
SortExpression="GenName"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="KVA"
HeaderText="KVA"
SortExpression="KVA"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="ManualCode"
HeaderText="Manual Code"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="Make"
HeaderText="Make"
SortExpression="Make"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="EngineNo"
HeaderText="Engine No"
SortExpression="EngineNo"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="AltSerialNo"
HeaderText="Alternator No"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="SoundType"
HeaderText="Sound Type"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="Health"
HeaderText="Health"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="ActiveStatus"
HeaderText="Active" Visible="false"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="AllocationStatus"
HeaderText="Allocated"
Visible="false"
ItemStyle-Wrap="False"></asp:BoundField>
<asp:BoundField DataField="LastReading"
HeaderText="Last Reading" ItemStyle-HorizontalAlign="Right"
Visible="false"
ItemStyle-Wrap="False"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
</div>
</asp:Panel>
<br />
<br />
</div>
<%-- End Generator List --%>
using System.Data;
using System.Data.SqlClient;
public partial class GeneratorEntry : System.Web.UI.Page
{
SqlConnection SqlCn;
SqlCommand SqlCm;
SqlDataReader SqlDr;
SqlDataAdapter SqlDa;
DataTable SqlDt;
DataSet SqlDs;
ConnectionState ConState;
static string SqlQry;
static string ConPath;
static Boolean SmtpContectionStatus;
// End Connection
private string SortDirection
{
get { return ViewState["SortDirection"] != null ? ViewState["SortDirection"].ToString() : "ASC"; }
set { ViewState["SortDirection"] = value; }
}
protected void OnSorting(object sender, GridViewSortEventArgs e)
{
this.LoadGenList(e.SortExpression);
}
protected void LoadGenList(string sortExpression = null)
{
ConnectionStringFunction();
SqlCn = new SqlConnection(ConPath);
SqlCn.Open();
MainClass.ProcNameClass = "GeneratorList";
try
{
SqlCm = new SqlCommand(MainClass.ProcNameClass, SqlCn);
SqlCm.CommandType = CommandType.StoredProcedure;
SqlCm.Parameters.AddWithValue("Action", ActionForProcess);
SqlCm.Parameters.AddWithValue("GenCode", TxtSearchValue.Text.Trim().ToUpper());
SqlCm.Parameters.AddWithValue("AltNo", TxtSearchValue.Text.Trim().ToUpper());
SqlCm.Parameters.AddWithValue("EngineNo", TxtSearchValue.Text.Trim().ToUpper());
SqlDa = new SqlDataAdapter();
SqlDa.SelectCommand = SqlCm;
SqlDt = new DataTable();
SqlDa.Fill(SqlDt);
if (sortExpression != null)
{
DataView dv = SqlDt.AsDataView();
this.SortDirection = this.SortDirection == "ASC" ? "DESC" : "ASC";
dv.Sort = sortExpression + " " + this.SortDirection;
GvGenList.DataSource = dv;
}
else
{
GvGenList.DataSource = SqlDt;
}
GvGenList.DataBind();
TotalRowCount = GvGenList.Rows.Count;
TotalColCount = GvGenList.Columns.Count;
if (TotalRowCount == 0)
{
GvGenList.Visible = false;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert",
"swal('Warning!','No Record Found' , 'warning')", true);
return;
}
GvGenList.Visible = true;
//for (int i = 0; i <= TotalColumnCount - 1; i++)
//{
// GvStaffList.Columns[i].ItemStyle.Wrap = false;
//}
}
// End Try
catch (Exception ex)
{
TransactionStatus = false;
}
// Catch
finally
{
SqlCn.Close();
}
// End Finally
}
// End Load Gen List
}