在 VS2008 中使用 Arraylist 控制 C# DataGridView

发布于 2024-07-09 03:55:48 字数 1309 浏览 3 评论 0原文

我在 VS2008 中使用的 datagridview 元素遇到一些问题。 此 DataGridView 实际上是 TabControl 元素中的一个选项卡。

我给了它 5 列,需要用我制作的服装对象中的元素填充。

它基本上是一个小型库应用程序,其中包含一个主类和几个从它派生的类。 它们都有一个 ToString() 方法,它将数据表示为一串关键字,其中包含填充 datagridview 所需的值。

我只需要前 5 个关键字,有些对象最多有 12 个关键字。 目前,每当我添加对象时,数据网格不会填充自身,而是添加与特定对象具有的关键字数量相等的列数量。

我目前正在做的是:

public void libDataGrid_Click(object sender, EventArgs e)
        {
            if(this.manager.Lib.LibList[0] != null)
            {
                libDataGrid.DataSource = this.manager.Lib.LibList;
                libDataGrid.Refresh();
            }
        }

this.manager.Lib.LibList 返回 ArrayList,其中存储所有对象。 ArrayList 可以包含所有派生类的元素,但由于它们都是连接的,因此字符串表示形式将始终包含填充网格所需的元素。

我不明白如何只过滤前五个并将它们放入正确的列中。

还有一件事。 目前我只能通过单击来刷新 DataGridView。 当我切换到它时,它应该改变,切换到我的意思是 Tabcontrol 上的特定选项卡。

我尝试为 SelectedIndexChanged 添加一个参数,但这实际上没有任何作用...... 或者至少,它似乎没有做任何事情。

我的意思是我注释掉了上面的代码并添加了以下代码:

public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
                libDataGrid.DataSource = this.manager.Lib.LibList;
                libDataGrid.Refresh();
        }

每次更改选项卡时都会刷新它,无论更改为哪一个。 我必须删除 if 语句,因为它给了我一个异常。 可能是因为 ArrayList 的长度未在初始化时设置。

I'm having some problems with a datagridview element I'm using in VS2008.
This DataGridView is actually a tab in a TabControl element.

I gave it 5 colums which need to be filled up with elements from a costum Object i made.

It's basically a small library application which contains a main class and several classed derived from it. They all have a ToString() method which represents the data as a string of keywords containing the values needed for me to fill up the datagridview.

I only need the first 5 though, some objects will have up to 12 keywords.
Currently, Whenever I add an object, the datagrid doesn't fill itself, instead it adds an amount of columns equall to the amount of keywords the specific object has.

What i'm currently doing is this:

public void libDataGrid_Click(object sender, EventArgs e)
        {
            if(this.manager.Lib.LibList[0] != null)
            {
                libDataGrid.DataSource = this.manager.Lib.LibList;
                libDataGrid.Refresh();
            }
        }

this.manager.Lib.LibList returns and ArrayList, in which all objects are stored. The ArrayList can contain elements of all derived classes, but since they are all connected, the string representation will always contain the elements I need to fill up the grid.

I don't see how I can filter only the first five and them have them put in the correct colums.

And another thing. Currently I can only refresh the DataGridView by clicking it. It should change on when I switch to it switch to its specific tab on the Tabcontrol I mean.

I tried adding an argument for SelectedIndexChanged, but that does nothing really...
Or at least, it doesn't appear to do anything.

What I mean is I commented out the code above and added this instead:

public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
                libDataGrid.DataSource = this.manager.Lib.LibList;
                libDataGrid.Refresh();
        }

This refreshes it everytime the tab is changed, no matter to which one.
I had to remove the if-statement, since it gave me an Exception. Probably because the length of the ArrayList isn't set on initialisation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以为你会在 2024-07-16 03:55:48

我对这个问题有点困惑,但这里有一些想法:

  1. DataGridView 有一个 AutoGenerateColumns 属性; 如果您不希望它创建自己的列,请将其设置为 false
  2. 要绑定到现有列,DataPropertyName 必须在每个 DataGridView 上设置
  3. (在 cmomon 中,任何使用 的列表控件TypeDescriptor)将非常喜欢 List (对于某些 T != object)而不是 ArrayList,因为即使是空列表它也可以获取元数据。 一般来说,在 2.0 中使用 ArrayList 是一个错误。

I'm a little confused by the question, but here are some thoughts:

  1. DataGridView has an AutoGenerateColumns property; if you don't want it to create its own columns, set this to false
  2. To bind to existing columns, the DataPropertyName must be set on each
  3. DataGridView (in cmomon with any list control using TypeDescriptor) will hugely prefer List<T> (for some T != object) to ArrayList, since it can get meta-data even for an empty list. In general, in 2.0 using ArrayList is a mistake.
要走就滚别墨迹 2024-07-16 03:55:48

我只能给出部分答案,但我认为

public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
                libDataGrid.DataSource = this.manager.Lib.LibList;
                libDataGrid.Refresh();
        }

不起作用的原因是因为您需要在初始化 tabControl1 的位置添加这一行。 我遇到过这个问题,VS 本身不会这样做。

tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);

I can only give a partial answer but I think the reason that

public void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
                libDataGrid.DataSource = this.manager.Lib.LibList;
                libDataGrid.Refresh();
        }

isn't working, is because you need to add this line where tabControl1 is being initialized. I've had this problem where VS won't do this itself.

tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
青朷 2024-07-16 03:55:48

如果我理解你的问题,它似乎与我最近在 C#/.NET2.0 中的 DataGridViews

尝试调用:

libDataGrid.Invalidate();

这应该会强制 Windows 重绘您的控件。 无需重新连接数据源并刷新。 (我认为您可以安全地注释掉这两行。)

另外:您遇到的异常是什么?

您是否使用“数据源配置向导”来帮助您使用dataGridView?

If I am understanding your problem, it seems similar to a problem that I was struggling with recently in this thread on DataGridViews in C#/.NET2.0

Try calling:

libDataGrid.Invalidate();

This should force Windows to redraw your control. No need to reattach the datasource and refresh. (I think you can safely comment out those 2 lines.)

Also: What was the Exception that you were getting?

And did you use the "Data Source Configuration Wizard" to help you with the dataGridView?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文