帮助使用C#将项目添加到列表视图 [英] Help with adding items to a listview using C#

查看:58
本文介绍了帮助使用C#将项目添加到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是初学者,只需要一些帮助来解决这个问题。



我认为它很简单,但试图搞清楚,但我没有在哪里。

我目前有ArrayList arry_eventss

[0] 1998397003

[1] 2



我将数据添加到列表视图中。

但此行中的错误

错误:System.InvalidOperationException:'跨线程操作无效:控制'listView1'从其创建的线程以外的线程访问on。'



listView1.Items.Add(ftrs.ToString());





i想要像那样输出



TYPE |状态

1998397003 | 2

4548484541 | 5



我尝试了什么:



Hi,

I'm a beginner and just need some help solving this problem.

I think its simple but have tried to figure it out but im getting no where.
I currently have ArrayList arry_eventss to which
[0] 1998397003
[1] 2

I add data to the listview.
but error in this line
error : System.InvalidOperationException: 'Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on.'

listView1.Items.Add(ftrs.ToString());


i want to output like that

TYPE | STATUS
1998397003 | 2
4548484541 | 5

What I have tried:

public void CB_SubscribeEvent(PLAT_SUBSCRIBE_EVENT_V20 pstEvent, IntPtr pUser)
        {
           ArrayList arry_eventss = new ArrayList();

           arry_eventss.Add(pstEvent.iState);
           arry_eventss.Add(pstEvent.iEventType);
           int c_arry_events = arry_eventss.Count;

           string[] ftrs = new string[c_arry_events];
           for (int j = 0; j < c_arry_events; j++)
           {

               listView1.Items.Clear();

               ftrs[j] = arry_eventss[j].ToString();

           }
           listView1.Items.Add(ftrs.ToString());
         }

推荐答案

如果您使用的是Windows Forms

,您应该阅读本文:

使用匿名方法调用设计模式 [ ^ ]



例如

If you are using Windows Forms
you should read this article:
Using the Invoke Design Pattern with Anonymous Methods[^]

e.g.
Invoke(new MethodInvoker(
        delegate { listView1.Items.Add(ftrs.ToString()); }
    ));
}

如果您使用WPF,您应该阅读:

从另一个线程更新WPF UI - Stephen Haunts {在战壕中编码} [ ^ ]



eg

if you are using WPF you should read this:
Update a WPF UI from Another Thread – Stephen Haunts { Coding in the Trenches }[^]

e.g.

Application.Current.Dispatcher.Invoke(() =>
{
    listView1.Items.Add(ftrs.ToString());
});


这篇关于帮助使用C#将项目添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆