943,677 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 55771
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 12th, 2006
0

Datagrid to draw a table

Expand Post »
Hi all...

I wish to draw a table showing 3 columns of values and many many rows.
However, i cant seem to find any other functions other than datagrid in the design toolbox interface of C#. And since my data source is not from any serverbased source (My data are only from .dat files located in my hdd), I do not know if I can use Datagrid to draw the table. Is there other ways to draw a table in C# without using datagrid? Or if datagrid is necessary or more convenient, is it able to draw table with source not from any database server?

Thanks for any help rendered...
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 12th, 2006
0

Re: Datagrid to draw a table

You can bind the Datagrid to arrays and other similar collections.
Which version of VS are you using? If you are using VS2005 try the DataList control, it is much better for your use and you can code tables in the templates
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 12th, 2006
0

Re: Datagrid to draw a table

Hi... thanks for the prompt reply. I am using VS 2003. I always hear ppl talking about binding arrays to datagrid. But what does it mean? My interpretation is as follow:

1) Read the lines from a .dat file, and array them in a for loop.
2) Bind the array to datagrid.

Are the interpretations correct?
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 12th, 2006
0

Re: Datagrid to draw a table

no need to for loop them, if you know the delimiter then you should be able to put them straight into an array.

But yes you are correct. Once you have an array just set the grid datasource to the array and then call Databind on the grid.

Just as a thought, i assume the dat file is using a table like structure but you want to save it to disk not a database? If so i would recommend creating a dataset or strongly typed dataset in memory and then call the dataset.save method. Then you can call the dataset.read method when you want it and just bind to the grid without the for loop array. It makes searching and filtering much better. You dont need a back end database with ADO.net that is why they split the datasets from the dataadapters.
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 13th, 2006
0

Re: Datagrid to draw a table

Yar... This is the part where I still dun understand... The data in my dat file looks something like this... not sure if its considered tabulated in ur sense...

0.200000000000000 -2.37875208125654 8.89149068490176
-1.54197025525003 16.2138152889587
0.400000000000000 -4.70208506970166 4.29899513347179
-0.122473657392517 5.26563417238388
0.600000000000000 -3.03187141119321 2.43126498291564
2.31215252446100 2.36115158336993
0.800000000000000 -2.03440037807500 1.81884834226502
3.02219205908151 1.69348030208479
1.00000000000000 -1.38619954085957 1.66793974796465
3.12514005981756 1.56289427464707
1.20000000000000 -0.912740905303422 1.65747262601114
2.99337573569932 1.57345864038550
1.40000000000000 -0.541662311537761 1.67911333342461
2.75455516251473 1.61842316014211

There are many many rows to continue... These are only part of it. So does this affect what you mentioned?
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 13th, 2006
0

Re: Datagrid to draw a table

it looks tabular to me... it has to be to go into a grid or the data you show wont make sense. Create a data table with the columns you want (there appears to be 5 in yours) Create a dataset and add the table to the dataset. Then you just need to bind the grid to the datatable. To save it then call dataset.writexml(datfilename.dat) as it doesnt have to be an xml extension though i would recommend it unless there are other requirements why you have to have a .dat file. You can also encrypt it if you want but that is outside of this post.

When you need the data just create a dataset again and call dataset.readxml(datfilename.dat) and bind it to your grid again.

Very simple for offline storage and other non database storage.
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 13th, 2006
0

Re: Datagrid to draw a table

Erm sorry... I know my data needs to be go into a grid before it makes sense... But the .dat files are generated in runtime. How can I transfer them to a tabulated format (Data table) in runtime as well? This is the step I am confused. Initially I wanted to make the DataSource of the DataGrid as an array, since I already got a 2-D array out from the data from the .dat file. But then, seems that DataSource can only be of the following types:

A DataSource for a DataGrid can comprise of seven different types of Data sources. These are as follows:

• DataTable

• DataView

• DataSet

• DataViewManager

• Single dimensional array

• IList interface

• An object that implements the IListSource

Thus, I have a major problem here since my array is 2-D. That eliminates the possibility of using array to be used as the DataSource. How should I use other types as the DataSource then?
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 13th, 2006
0

Re: Datagrid to draw a table

Sorry but i am a bit confused with your requirements. You say you have a 2d array but your example earlier shows a 1d array. There are thousands of different scenarios so instead of me guessing could you tell me what you are trying to achieve (which is different from saying how you are implementing it). I think you are taking data from somewhere, storing it, then reading it later to display it. I can then give you the best solution to implement it. I am still thinking the best way will be datasets and a grid but i am not sure of the information and how it got there in the first place.

Hope it makes sense.
Who inputs the data?
What is the data (in general not specifics)
Why were you using a 2d array? Was the data 2 tiered?

Let me know and i will get back to you later
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 14th, 2006
0

Re: Datagrid to draw a table

Oh sorry sorry. Huge miscommunication... hehe...

1) The data is input by the user who keys in some variables that will generate the data in the .dat files in runtime.

2) The data in the .dat files are some variables used in Fluids dynamics. They consist of time, lift and drag coefficient. All are doubles in C sense. Another .dat file consists of X, Y values of the coordinate system for plotting points, and also the Vorticity and Stream Function value. All are in double too. This will be the .dat example I am reading and trying to put in DataGrid in the following program.

3) Well... I am not really sure is it 2-D array. I all along thought its a 2-D array, because that guy who developed the code for this .dat files said so. Thats for convenience in plotting the data on a graph. In fact, according to him, two of such 2-D arrays are required. I havent get to this yet... So ignoring this at the moment. But however, I just read on a MSDN book on a similar case to mine, data that is similar to those being in a .txt format (Mine can open using notepad too). It uses the ReadLine method to read lines, split the numbers and immediately adds the new data row. Seems that this is what you mean by 1-D array? So, I followed the code to implement the thing. It should be correct, compilation no error, execute with no error too. But however, nothing appeared in the display window. Its totally blank.(My whole datagrid is maximized to fill the whole Form1 window). What can be the problem?

Below is the code relating the DataTable and DataGrid binding I did in my program:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//Creates a new DataSet
DataSet myDataSet = new DataSet();

//Creates a new DataTable and adds it to the Tables Collection
DataTable aTable = new DataTable("Table 1");
myDataSet.Tables.Add("Table 1");

//Creates and names the 4 columns data from file 2cylinintegers.dat and adds to Table 1
DataColumn Column1 = new DataColumn("X value");
DataColumn Column2 = new DataColumn("Y value");
DataColumn Column3 = new DataColumn("Vorticity");
DataColumn Column4 = new DataColumn("Stream Function");

myDataSet.Tables["Table 1"].Columns.Add(Column1);
myDataSet.Tables["Table 1"].Columns.Add(Column2);
myDataSet.Tables["Table 1"].Columns.Add(Column3);
myDataSet.Tables["Table 1"].Columns.Add(Column4);

//Creates the StreamReader to read the file and a string variable to hold the output of the StreamReader
StreamReader myReader = new StreamReader("Fluids1.dat");

//Checks to see if the Reader has reached the end of the stream
while (myReader.Peek()>-1)
{
//Reads a line of data from the data file
string myString = myReader.ReadLine().Trim();

//Uses Regex.Split method to create an array of strings that represents each entry in the line.
string [] split = System.Text.RegularExpressions.Regex.Split(myString, "\\s+", RegexOptions.None);

//Adds the array as a new DataRow to Table 1
myDataSet.Tables["Table 1"].Rows.Add(split);
}

}
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 14th, 2006
0

Re: Datagrid to draw a table

We can look at the 2d arrays later. Your code is almost there.
First, with your columns if you know they are going to be doubles then set the column data type as a double too.

Finally at the end instead of your line
myDataSet.Tables["Table 1"].Rows.Add(split);

use the following
C# Syntax (Toggle Plain Text)
  1. DataRow theRow = myDataSet.Tables["Table 1"].NewRow();
  2. theRow["X value"] = (decimal)split[0];
  3. theRow["Y value"] = (decimal)split[1];
  4. theRow["Vorticity"] = (decimal)split[2];
  5. theRow["Stream Function"] = (decimal)split[3];
  6. myDataSet.Tables["Table 1"].Rows.Add(theRow);

But thats just better code... your main problem is you didnt bind the dataset to the grid!!! Easy to miss but causes a problem after so much coding huh.

MyGrid.Datasource = myDataSet.Tables["Table 1"];

or MyGrid.DataMember = "Table 1";
myGrid.DataSource = myDataSet

If you use the second option always set the datamember name before binding the datasource otherwise you will bind twice and waste performance

Hope that gets you going.

However, i still think you could have just called myDataSet.SaveXML("mydatfile.dat")
then all you have to do is create a dataset, then call myDataSet.ReadXml("mydatfile.dat") and then bind it straight to the grid, no reading, no stream writers and readers, no anything but 2 lines of code.

You fill the table in the first place much the same way column by column .. row by row but that is the same as the dat file, only i suspect it will be easier too.

Let me know how you get on
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC