Tutorial :Controlling C# DataGridView with an Arraylist in VS2008



Question:

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.


Solution:1

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.


Solution:2

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);  


Solution:3

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?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »