| | |
Datagrid to draw a table
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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.
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.
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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.
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.
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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
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
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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
use the following
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
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)
DataRow theRow = myDataSet.Tables["Table 1"].NewRow(); theRow["X value"] = (decimal)split[0]; theRow["Y value"] = (decimal)split[1]; theRow["Vorticity"] = (decimal)split[2]; theRow["Stream Function"] = (decimal)split[3]; 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
![]() |
Other Threads in the C# Forum
| Thread Tools | Search this Thread |
.net access algorithm alignment app array barchart bitmap box broadcast c# c#gridviewcolumn cast check checkbox client combobox communication concurrency control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum excel file focus form format forms function gdi+ hospitalmanagementsystem image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml





