An equivalent to JTable/AbstractTableModel paradigm

  • Thread starter Thread starter The Garbage Collector
  • Start date Start date
T

The Garbage Collector

Hey guys,
Heres something I used to do very commonly in java, and I'm having a
hard time coming up with a good paradigm for how to manage this in c#.

In Java, to show data on a grid, you need to extend
AbstractTableModel,
and simply define three methods:
object getValueAt(int i, int j);
int getColCount();
int getRowCount();

(or override some more if you wanted headers and stuff)

then you did:
new JTable(new MyTableModel());

and that was it, however you wanted to throw your data back was how it
got displayed in the JTable.

now, lets say you had a thread running somewhere away from your awt
thread, which modified the data in your MyTableModel object. In order
to notify the gui that the data had changed all you had to do was:
MyTableModel.this.fireTableDataChanged();
which would inform the gui that it had to request new data to display
when it was ready.

------------------------- .net ---------------------------

my situation: i have a class which is a wrapper around a list of
objects.
I want to show a grid where each row is an object, and the columns are
hand picked properties of those objects.

when I first started using c# i would create a datatable object, add
columns and rows and fill it in. however, this will not work anymore,
because I realized that this means keeping two copies of the data, and
that I dont have a good solution for updating the datatable syncronsly
without breaking the ui.
(You can't modify a DataTable object from a thread other than the UI
thread to which the table is Bound.)

So, todays attempt was to make my List class implement the IList
interface, (pretty easy since it already wraps an arraylist object),
and then bind it to the datagrid. Assuming all the elements of the
list are the same, the data grid will automatically create columns for
every public property of the element class.

This was cool, but not what I wanted, because I want to have
properties for things that dont need columns and vice versa.

Plus! i still didnt have a solution to notifying the UI that the data
changed, so it could update itself on its own thread.

So, anybody have an elegant solution to these issues?

Missing Java,
Eric Weinschenk
 
To follow up just a bit, I have written the smallest possible C#
windows form app you could ever have to illustrate the issue.

you should create a new project, dump this code in (i think you know
where),
run the app, and just click around on the datagrid a little, sort it,
click on it again, and then BAM - the app goes down.

Anybody got a solution?

// Code Here -------------------------------------------
DataTable theTable;

public Form1(){
InitializeComponent();

theTable = new DataTable();
for(int i = 0; i < 10; i++)
theTable.Columns.Add();
for(int i = 0; i < 10; i++)
theTable.Rows.Add(theTable.NewRow());

dataGrid1.DataSource = theTable;
new System.Threading.Thread(
new System.Threading.ThreadStart(EnterData)).Start();
}


private void EnterData(){
while(true){
Random r = new Random((int)DateTime.Now.Ticks);
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
theTable.Rows[j] = r.Next(1000);
}
}
System.Threading.Thread.Sleep(500);
}
}
 
Back
Top