I want to sort the listview data when click at the column header. Found this article with the code. http://msdn.microsoft.com/en-us/library/ms229643%28v=vs.90%29.aspx
but when run it, the first line
ColHeader clickedCol = (ColHeader)this.listView_ChkInOut.Columns[e.Column];
already generate error invalid cast exception was unhandled.
public class ColHeader : ColumnHeader
{
public bool ascending;
public ColHeader(string text, int width, HorizontalAlignment align, bool asc)
{
this.Text = text;
this.Width = width;
this.TextAlign = align;
this.ascending = asc;
}
}
I don't understand the code, can some explain why it had that error?
ColumnHeader lickedCol = (ColumnHeader)this.listView1.Columns[e.Column];
MessageBox.Show(lickedCol.Text);
As far as i can see this should work properly. At least i get the right text.
In the example you provided this happens when he added the Columnheaders
// parameter specifies true for an ascending sort order.
listView1.Columns.Add(new ColHeader("Name", 110, HorizontalAlignment.Left, true));
listView1.Columns.Add(new ColHeader("Region", 50, HorizontalAlignment.Left, true));
listView1.Columns.Add(new ColHeader("Sales", 70, HorizontalAlignment.Left, true));
He added ColHeaders and not ColumnHeaders. I think you havent done this and then you get the error, at least this is what happend to me ;)
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.