473,803 Members | 3,479 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 2591
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
5513
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
17225
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
9861
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
5844
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
9565
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
10550
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...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
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...
0
6844
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
5501
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...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.