ListView grouping problem in Large Icon View
Hello,
I'ld like to group items in a large icon view listview (winforms .net 4.0, vs2010). But when I do, I don't see anything! Although I set showgroups to true. Here's my code:
private void ReadData()
{
//Clear nodes
lvEcTopology.Items.Clear();
lvEcTopology.Groups.Clear();
lvEcTopology.ShowGroups = true;
//Read for each master, it's state and slavestate
for (int i = 1; i <= _canMasterArraySize; i++)
{
CanDiagnosticData data = new CanDiagnosticData();
string structName = _canMasterArrayName + "[" + i.ToString() + "]" + _hmiEl6751DataSuffix;
PlcVariable item = new PlcVariable(_plcName, "", PlcVariable.EnumUpdateType.None, 0, PlcType.Tc_BOOL);
object itemValue;
//Read the canstate
item.VariableName = structName + _canStateSuffix;
item.VariableType = _canStateType;
itemValue = item.ReadValueSync();
data.CanState = itemValue != null ? (UInt16)itemValue : (ushort)0;
//Read the RxErrorCounter
item.VariableName = structName + _rxErrCntrSuffix;
item.VariableType = _rxErrCntrType;
itemValue = item.ReadValueSync();
data.RxErrorCounter = itemValue != null ? (byte)itemValue : (byte)0;
//Read the TxErrorCounter
item.VariableName = structName + _txErrCntrSuffix;
item.VariableType = _txErrCntrType;
itemValue = item.ReadValueSync();
data.TxErrorCounter = itemValue != null ? (byte)itemValue : (byte)0;
//Read the NodeswithErrorCounter
item.VariableName = structName + _nodesWithErrSuffix;
item.VariableType = _nodesWithErrType;
itemValue = item.ReadValueSync();
data.NodeswithError = itemValue != null ? (byte)itemValue : (byte)0;
//Add the master
lvEcTopology.Groups.Add("EL6751 - " + i.ToString(), "EL6751 - " + i.ToString());
lvEcTopology.Groups[i-1].Items.Add(new ListViewItem("EL6751 - " + i.ToString(), 2) { Tag = data, ForeColor = (data.CanState == 0 ? Color.Green : Color.Red) });
//Read the NodeStates
item.VariableName = structName + _nodeStatesSuffix;
item.VariableType = _nodeStatesType;
item.IsArray = true;
item.Elements = _canSlaveArraySize;
byte[] nodes = item.ReadValueSync() as byte[];
if (nodes != null)
{
for (int j = 0; j < nodes.Length; j++)
{
if (nodes[j] != 255)
{
CanDiagnosticData slave = new CanDiagnosticData() { NodeState = (CanSlaveState)nodes[j] };
//Add the node
lvEcTopology.Groups[i-1].Items.Add(new ListViewItem("Node - " + (j+1).ToString(), nodes[j] == 2 ? 0 : 1) { Tag = slave, ForeColor = (nodes[j] == 0 ? Color.Green : Color.Red) });
}
}
}
}
}
I really don't know what I'm doing wrong (and yes, it adds 2 groups, each group containing about 10 items). Why is my listview still empty?
Hoping for an answer (really, I'm getting hopeles).
Re: ListView grouping problem in Large Icon View
Rather than adding items to the group, you should add items to the listview and set the item's group property appropriately.
http://msdn.microsoft.com/en-us/libr...8VS.90%29.aspx
While it seems adding the item to the group itself is logical and should work, I have run into the same issue... when I use groups, I normally follow this sort of pattern...
* for each item to add...
determine the group key it should be in
if the group doesn't exist, Groups.Contains(), then add the new group.
create the new listviewitem
set the listviewitem's group property to the appropriate group.
add the item to the listview.items...