简体   繁体   English

有关WinForms和ListView的简单问题

[英]Simple question about WinForms and ListView

I have a ListView in my WinForms application. 我的WinForms应用程序中有一个ListView。 ListWiew has 4 columns. ListWiew有4列。 So i want to write string in fourth column on every LisViewItem. 所以我想在每个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 Suggester.exe!Suggester.MainForm.btnSend_Click(对象发送者= {Text =“Отправить”},System.EventArgs e = {X = 45 Y = 15 Button = Left})316行C#System.Windows.Forms.dll!System .Windows.Forms.Control.OnClick(System.EventArgs e)+ 0x70字节
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) + 0x4a bytes System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e)+ 0x4a字节
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.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent = {X = 45 Y = 15 Button = Left})+ 0xac字节System.Windows.Forms.dll! System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m,System.Windows.Forms.MouseButtons按钮,多次单击)+ 0x28f字节System.Windows.Forms.dll!System.Windows.Forms.Control .WndProc(ref System.Windows.Forms.Message m)+ 0x885字节System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m)+ 0x127字节
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) + 0x20 bytes System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m)+ 0x20字节
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.OnMessage(ref System.Windows.Forms.Message m)+ 0x10字节
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.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)+ 0x31字节
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg = 514, System.IntPtr wparam, System.IntPtr lparam) + 0x57 bytes System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd,int msg = 514,System.IntPtr wparam,System.IntPtr lparam)+ 0x57字节
[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.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID,int原因= -1,int pvLoopData = 0)+ 0x24e字节System.Windows .Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1,System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext})+ 0x177字节System.Windows.Forms .dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(原因,System.Windows.Forms.ApplicationContext上下文)+ 0x61字节
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm)+ 0x31字节
Suggester.exe!Suggester.Program.Main() Line 17 + 0x1d bytes C# [Native to Managed Transition] Suggester.exe!Suggester.Program.Main()第17行+ 0x1d字节C#[本地到托管转换]
[Managed to Native Transition] [管理到本地过渡]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x3a bytes mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,System.Security.Policy.Evidence assemblySecurity,string [] args)+ 0x3a字节
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2b bytes Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()+ 0x2b字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)+ 0x66字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executeContext,System.Threading.ContextCallback回调,对象状态)+ 0x6f字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()+ 0x44字节

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. 出现此错误的原因是,第4列中没有任何项。这就像您正在尝试调用未创建的对象一样。

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. 然后在特定事件(例如单击按钮)之后,要在所有项目的第4列中添加一些内容,然后可以使用以下代码。

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

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"; 
            } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM