问题
I am showing a Totals Row as last row. I want to exclude that row from sorting when the user clicks on the column header. By using sql union
I am adding total column to my result. I am using SQL, C# and DataGridView
control. I am not able to expose ColumnHeader_Click
event. I am only using TableStyle[0].AllowSorting = false
. How can I apply that custom sorting on the control?
Thanks
回答1:
Thanks TaW, your answer helped me. My needs were a little different, I needed the Total to appear at the top and also retain the sort column throughout as my grid is highly interactive with loads of filtering and changes in the data being presented.
My sorting is done via
protected void ReportGridView_Sorting(object sender, GridViewSortEventArgs e)
Here's what I ended up using in my method to populate the GridView:
if (!myDataTable.Columns.Contains("SortLevel"))
{
myDataTable.Columns.Add("SortLevel", typeof(Int16));
foreach (DataRow dr in myDataTable.Rows)
{
dr["SortLevel"] = 0;
}
dt.Rows[0]["SortLevel"] = 1;
}
if ((Session["SortDirection"] != null) && (Session["SortExpression"] != null))
{
myDataTable.DefaultView.Sort = "SortLevel DESC, " + Session["SortExpression"] + " " + Session["SortDirection"];
}
MyGridView.DataSource = myDataTable;
MyGridView.AllowSorting = true;
MyGridView.DataBind();
Side note: I had to use Sessions to hold the custom sorting instead of ViewState as this wasn't working properly with the dynamically created buttons in my gridview
回答2:
This solution is based on @T.S.'s suggestion but works directly in the DataSet, not in SQL.
I have tested it in VS2013; I don't know if it will work in .Net 1.1 and would have to revive a very old machine to test that..
I don't know what you mean by
I am not able to expose columnheader_click event.
I have split the solution into a function and the ColumnHeaderMouseClick
event; if you really can't use that event you will have to find another way; but one way or the other you need to trigger the sort and decide by which column(s) to sort.
The heart of the solution are the setting of the new column values and the expression by which you identify your 'TotalsRow'. I have used my test table's PK column 'ID' and push the 'ID=1 record' to the bottom. After setting the sort column to 0 on all rows the first row to fit the expression is set to maxInt.
You will have to adapt that to an expression that works for your result set.
I am adding and setting the custom sort column dynamically and remove it after the sort; the DGV is suspending its layout until the whole affair is completed.
DataTable yourTable = ..
private void dataGridView1_ColumnHeaderMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
string col = dataGridView1.Columns[e.ColumnIndex].Name;
if (col != "") sortDGV (col );
}
private void sortDGV(string col)
{
dataGridView1.SuspendLayout();
yourTable.Columns.Add("sortMe", typeof(Int32));
yourTable.DefaultView.Sort = col;
DataRow[] dr = yourTable.Select("ID='1'");
for (int r = 0; r < yourTable.Rows.Count; r++) yourTable.Rows[r]["sortMe"] = 0;
dr[0]["sortMe"] = int.MaxValue;
yourTable.DefaultView.Sort = "sortMe," + col;
yourTable.Columns.Remove("sortMe");
dataGridView1.ResumeLayout();
}
来源:https://stackoverflow.com/questions/24294658/exclude-the-totals-row-from-sorting-a-bound-datagridview