473,834 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An equivalent to JTable/AbstractTableMo del 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
AbstractTableMo del,
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.th is.fireTableDat aChanged();
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 2592
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(){
InitializeCompo nent();

theTable = new DataTable();
for(int i = 0; i < 10; i++)
theTable.Column s.Add();
for(int i = 0; i < 10; i++)
theTable.Rows.A dd(theTable.New Row());

dataGrid1.DataS ource = theTable;
new System.Threadin g.Thread(
new System.Threadin g.ThreadStart(E nterData)).Star t();
}
private void EnterData(){
while(true){
Random r = new Random((int)Dat eTime.Now.Ticks );
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
theTable.Rows[i][j] = r.Next(1000);
}
}
System.Threadin g.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
5514
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 = datas.get(rowToDelete); String idName = idField.getColumnName(); Method method = dataObjectClass.getMethod("get" +
7
17228
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 then it would change whether that third column is editable or not. I have made a short array that would hold values of either 0 or 1, basically whether or not each cell is editable or not. Along with that is obviously a setEditable(int row, int...
5
9868
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. But i want the table to display the data from a .txt file in which i had stored information in this format. Can anyone please modify the codes and explain me. Name:Mr.xyz Phone:111111222222
1
5845
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. import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class TableAvecRenderer extends JPanel {
0
9643
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10786
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10544
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9326
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4425
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 we have to send another system
2
3973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.