I have a class:
public class CustomInvoice
{
public string InvoiceId { get; set; }
public string Date { get; set; }
public string RecpDate { get; set; }
public string Category { get; set; }
public string TotalValue { get; set; }
public string InvoiceRef { get; set; }
public string Client { get; set; }
public string Status { get; set; }
}
I populate this class with data and then bind it to my gridview:
List<CustomInvoice> invoices = GetInvoices(url);
InvoiceGrid.DataSource = invoices;
InvoiceGrid.DataBind();
This displays all of the information correctly, but now i want to be able to sort it, so ive added a button with the following method:
protected void Button1_Click(object sender, EventArgs e)
{
string expression = "InvoiceId";
SortDirection direction = SortDirection.Ascending;
InvoiceGrid.Sort(expression,direction);
}
Ive also changed its html to allow sorting:
<asp:GridView ID="InvoiceGrid" runat="server" AllowSorting="True" OnSorting="gridView_Sorting"></asp:GridView>
However when this runs I get the following exception
System.Web.HttpException (0x80004005): The GridView 'InvoiceGrid' fired event Sorting which wasn't handled.\r\n
Can someone show me how to sort my gridview by using the onclick of the button?
Thanks :)
edit:
I have tried adding this to the column headers (ignoring the button)
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = InvoiceGrid.DataSource as DataTable;
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + e.SortDirection;
InvoiceGrid.DataSource = dataView;
InvoiceGrid.DataBind();
}
however the problem here is the DataTable is null even though i can see the invoiceGrid has data.
The solution to this problem was mentioned to me by @hamed shams and that is to use linq (which I had completely forgotten about).
So I use linq on my reference to the customInvoice:
var result = from i in invoices
orderby i.Category ascending
select i;
Then I bind the result to the gridview:
InvoiceGrid.DataSource = result;
InvoiceGrid.DataBind();
Try this below:
protected void SearchResults_Sorting(object sender, GridViewSortEventArgs e)
{
var searchedStudents = this.SearchResults();
var sortedData = searchedStudents.DefaultView;
var sortDirection = !this.SortDirections.ContainsKey(e.SortExpression) || !this.SortDirections[e.SortExpression] ? "desc" : "asc";
var sortDirections = new Dictionary<string, bool>();
sortDirections.Add(e.SortExpression, sortDirection == "desc");
this.SortDirections = sortDirections;
sortedData.Sort = e.SortExpression + " " + sortDirection;
this.SearchResults.DataSource = sortedData.ToTable();
this.SearchResults.DataBind();
}
private Dictionary<string, bool> SortDirections
{
get
{
var sortDirection = new Dictionary<string, bool>();
if (this.ViewState["SortDirection"] != null)
{
sortDirection = (Dictionary<string, bool>)this.ViewState["SortDirection"];
}
return sortDirection;
}
set
{
this.ViewState["SortDirection"] = value;
}
Let me know if you have any questions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.