Controlling C # DataGridView with Arraylist in VS2008

I am having problems with the datagridview I am using in VS2008. This DataGridView is actually a tab of the TabControl.

I gave it 5 columns to fill with items from costum Object i.

It is basically a small library application that contains a main class and several classes derived from it. They all have a ToString () method that presents the data as a string of keywords containing the values โ€‹โ€‹I need to populate the datagridview.

I only need the first 5, although some objects will contain up to 12 keywords. Currently, when I add an object, the datagrid doesn't populate itself, instead it adds the number of columns equal to the number of keywords that the particular object has.

What I am doing now is:

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 an ArrayList, which contains all the objects. ArrayList can contain elements of all derived classes, but because they are all related, the string view will always contain the elements needed to populate the grid.

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

One more thing. Currently I can refresh the DataGridView by clicking it. It should change when I switch to it, switching to its specific tab in the Tabcontrol I mean.

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

I mean, 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 updates it every time the tab is changed, no matter which one. I had to delete the if-statement as it gave me an exception. Probably because the length of the ArrayList is not set on initialization.

+1


source to share


3 answers


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



  • DataGridView

    has the property AutoGenerateColumn

    s
    ; if you don't want it to create its own columns set this to false
  • To bind to existing columns DataPropertyName

    must be set on each
  • DataGridView

    (in cmomon with any list control using TypeDescriptor

    ) would be highly preferred List<T>

    (for some T

    ! = object

    ) before ArrayList

    , as it can get metadata even for an empty list. In general, in 2.0 using ArrayList

    is a bug.
+3


source


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

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

      



doesn't work because you need to add this line where tabControl1 is initialized. I had this problem where VS won't do it by itself.

tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);

      

0


source


If I understand your problem, this is similar to the problem I recently encountered on this thread in DataGridViews in C # /. NET2.0

Try to call:

libDataGrid.Invalidate();

      

This will force Windows to redraw your control. No need to reconnect the data source and update. (I think you can feel free to comment on these 2 lines.)

See also: What was the Exception you received?

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

0


source







All Articles