I'm encountering a strange ListView problem and will do my best to explain it. I have a ListView with 4 columns, the last one being a message string of varying length. I have some functionality that will change the ListView item red if it contains certain keywords (fail, exception, ect).
I first noticed this issue when one item was red and I didn’t see any word in the column that would trigger the red coloring code. So, I had the Length of the incoming string prepending to the item and added a textbox that would display that column's text when selected. What I found was that the prepending length (actual length of incoming string) would be like 953, the extracted ListViewItem's Text length would be 960 (str length + prepended length info), but the text that would be in the text box's length was 253...
What's going on here? Its like all the text made it into the ListViewItem but it can't/won't show it all (and no, its not column width, I had it set to over 1000 in the above case).
Adding the ListViewItem and checking for error strings:
ListViewItem listItem = new ListViewItem(msg.Date);
// Add sub-items for Details view.
listItem.SubItems.Add(msg.Time);
listItem.SubItems.Add(msg.Thread);
listItem.SubItems.Add("L: " + msg.Message.Length + " " + msg.Message);
if (!msg.Message.Contains("FA_FAILCNT"))
{
if (msg.Message.Contains("fail", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("exception", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("db q", StringComparison.OrdinalIgnoreCase))
{
listItem.Font = new Font(listItem.Font, FontStyle.Bold);
listItem.ForeColor = Color.Red;
}
else
listItem.ForeColor = Color.Black;
}
Obviously its the last subitem thats giving me the issues (the one that gets msg.Message)
EDIT: Well crap, this explains it.... any ways around this?
You have already found the reason why not all of the text is displayed.
The best solution that I've found so far is to put the information in the tooltip so that the entire string is visible when the user hovers their mouse over the column - see Listview subitem text is not shown fully in the UI eventhough the length of the text is correct.
Another way I've seen this work is allowing users to copy the value of a cell. Although the displayed text is truncated, copying and pasting the cell value into another application allows you to view the full text.
I imagine that the only other "workaround" would involve writing your own control - alternatively I don't think the ListView control in WPF has this same limitation.