473,464 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

An equivalent to JTable/AbstractTableModel paradigm

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
Nov 15 '05 #1
1 2570
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[i][j] = r.Next(1000);
}
}
System.Threading.Thread.Sleep(500);
}
}
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bernard Koninckx | last post by:
Hi everybody, The following code (putted in a inherited object from AbstractTableModel object) make some errors : public void deleteRow(int rowToDelete){ try{ Object dataObject =...
7
blazedaces
by: blazedaces | last post by:
Hi guys, what I'm I have is a jtable with boolean variables in the first column and text in the next two (checkboxes). I want my tableChanged method to make it so when I check one of the checkboxes...
5
by: moizpalitanawala | last post by:
Hello friends, How to add data to JTable from a .txt file. I had seen this code from one of the tutorial from java.com. This code doesnt take any input. But shows what is written in the array....
1
by: emekadavid | last post by:
can anyone help me flesh out the problem? I wanted to insert a combo box into one of the columns of a JTable. although the combo box is drawn, the list of the data model fails to render. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.