1
votes

I have a ListView in my WinForms application. ListWiew has 4 columns. So i want to write string in fourth column on every LisViewItem. When i try it.

foreach (ListViewItem item in lvData.Items)
                {
                    item.SubItems[3].Text ="something";
                }

i get an exception

InvalidArgument=Value of '4' is not valid for 'index'.
Parameter name: index

What's wrong?

Call stack:

Suggester.exe!Suggester.MainForm.btnSend_Click(object sender = {Text = "Отправить"}, System.EventArgs e = {X = 45 Y = 15 Button = Left}) Line 316 C# System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) + 0x70 bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) + 0x4a bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent = {X = 45 Y = 15 Button = Left}) + 0xac bytes System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) + 0x28f bytes System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x885 bytes System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) + 0x127 bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) + 0x20 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x10 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg = 514, System.IntPtr wparam, System.IntPtr lparam) + 0x57 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = -1, int pvLoopData = 0) + 0x24e bytes System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) + 0x177 bytes System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes
Suggester.exe!Suggester.Program.Main() Line 17 + 0x1d bytes C# [Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x3a bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2b bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes

2
> Suggester.exe!Suggester.MainForm.btnSend_Click(object sender = {Text = "Отправить"}, System.EventArgs e = {X = 45 Y = 15 Button = Left}) Line 316 C# [External Code] Suggester.exe!Suggester.Program.Main() Line 17 + 0x1d bytes C# [External Code]Alex
Right-click on it, then select "Show External Code". Then you'll see the real call stack. Paste it here.Fyodor Soikin
Ok then. Can you paste the full text of the btnSend_Click method?Fyodor Soikin
btnSend_Click method is about 300 lines. It's bad legacy code :) Do you know any reasons why this might doesnt work? I am very newbie in WinForms.Alex
Well, you see, there's something really strange going on here. First of all, your exception mentions index 4 instead of 3. Therefore, it could not have been thrown by the .SubItems[3] accessor. That's why I needed to see your call stack: it would indicate what method threw the exception. But then it turns out that your call stack shows that the exception has been thrown by the btnSend_Click method itself! Otherwise, there would be other methods on top. So the only real possibility here is that you have a throw new ArgumentException() code somewhere in the btnSend_Click method.Fyodor Soikin

2 Answers

0
votes

The reason you are getting this error is because, there is no item present in the Column 4. It's like you are trying to call a object which is not created.

Suppose let's say you have added data to first 3 columns like this.....

 ListViewItem item1 = new ListViewItem("Col 1", 0);
 item1.SubItems.Add("Col 2");
 item1.SubItems.Add("Col 3");

and after a particular event, for Example, a button click, you want to add a some thing to column 4 of all items, then you can use the following code.

foreach (ListViewItem l in listView1.Items)
{
  l.SubItems.Add("Col 4");
}
0
votes

Does each item in your listview actually have three subitems? You can't just set the number of columns, you actually need to add the required subitems to each added item. Even if they are empty, you still need to add them to access them.

foreach (ListViewItem item in lvData.Items) 
            { 
                while(item.SubItems.Count() < 3)
                {
                     item.SubItems.Add("");
                }
                item.SubItems[3].Text ="something"; 
            }