簡體   English   中英

有關WinForms和ListView的簡單問題

[英]Simple question about WinForms and ListView

我的WinForms應用程序中有一個ListView。 ListWiew有4列。 所以我想在每個LisViewItem的第四列中寫入字符串。 當我嘗試。

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

我有一個例外

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

怎么了?

調用堆棧:

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字節
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字節
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字節
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd,int msg = 514,System.IntPtr wparam,System.IntPtr lparam)+ 0x57字節
[從本地過渡到托管過渡]
[管理到本地過渡]
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字節
Suggester.exe!Suggester.Program.Main()第17行+ 0x1d字節C#[本地到托管轉換]
[管理到本地過渡]
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字節
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(對象狀態)+ 0x66字節
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executeContext,System.Threading.ContextCallback回調,對象狀態)+ 0x6f字節
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()+ 0x44字節

出現此錯誤的原因是,第4列中沒有任何項。這就像您正在嘗試調用未創建的對象一樣。

假設您已經像這樣向前三列添加了數據.....

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

然后在特定事件(例如單擊按鈕)之后,要在所有項目的第4列中添加一些內容,然后可以使用以下代碼。

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

列表視圖中的每個項目實際上是否都有三個子項目? 您不僅可以設置列數,實際上還需要將必需的子項目添加到每個添加的項目中。 即使它們為空,您仍然需要添加它們才能訪問它們。

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