Click here to Skip to main content
15,888,202 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Want to display a dataTable in C#. I am using : "using System.Data" as a directive. Where am I going wrong?

C#
DataTable dt0 = new DataTable();
            dt0.Columns.Add("", typeof(string));
            dt0.Columns.Add("Sun", typeof(double));
            dt0.Columns.Add("Mercury", typeof(double));
            dt0.Columns.Add("Venus", typeof(double));
            dt0.Columns.Add("EMB", typeof(double));
            dt0.Columns.Add("Mars", typeof(double));
            dt0.Columns.Add("Jupiter", typeof(double));
            dt0.Columns.Add("Saturn", typeof(double));
            dt0.Columns.Add("Uranus", typeof(double));
            dt0.Columns.Add("Neptune", typeof(double));
            dt0.Columns.Add("Pluto", typeof(double));
            dt0.Columns.Add("Moon", typeof(double));
            dt0.Rows.Add("Xecl", Xecl[0], Xecl[1], Xecl[2], Xecl[3], Xecl[4], Xecl[5], Xecl[6], Xecl[7], Xecl[8], Xecl[9], Xecl[10]);
            dt0.Rows.Add("Yecl", Yecl[0], Yecl[1], Yecl[2], Yecl[3], Yecl[4], Yecl[5], Yecl[6], Yecl[7], Yecl[8], Yecl[9], Yecl[10]);
            dt0.Rows.Add("Zecl", Zecl[0], Zecl[1], Zecl[2], Zecl[3], Zecl[4], Zecl[5], Zecl[6], Zecl[7], Zecl[8], Zecl[9], Zecl[10]);
            dt0.Rows.Add("Recl", Recl[0], Recl[1], Recl[2], Recl[3], Recl[4], Recl[5], Recl[6], Recl[7], Recl[8], Recl[9], Recl[10]);
            
            DataView dv = new DataView(dt0);


What I have tried:

Not printing out the datatable. I have 12 columns of data to be printed out.
Posted
Updated 22-Mar-23 5:47am
v4
Comments
Dave Kreskowiak 2-Jan-23 21:28pm    
You haven't shown the code you're using to display this data.
[no name] 2-Jan-23 22:51pm    
https://stackoverflow.com/questions/15547959/print-contents-of-a-datatable
BillWoodruff 3-Jan-23 7:52am    
that's an excellent answer, if the OP wants a Console (string) type output !

i'd vote that #5 if you posted that as solution

Adding GridView completes the DataTable


C#
GridView1.DataSource = dt0;
GridView1.DataBind();
 
Share this answer
 
Comments
Dave Kreskowiak 9-Jan-23 17:56pm    
Next time you ask a question, specify the type of app you're writing. The application type will dictate which controls are available and what their names are.

Yes, it matters.
A DataView is not a user-interface element, it is a
Quote:
Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation. The DataView does not store data, but instead represents a connected view of its corresponding DataTable. Changes to the DataView's data will affect the DataTable. Changes to the DataTable's data will affect all DataViews associated with it.
[^]

You display/render a DataTable/DataView by binding it as the data source of a UI element like a DataGridView.

Assuming you have a DataGridView named dataGridView1 on a a form:

dataGridView1.Columns.Clear();
dataGridView1.DataSource = dv;
 
Share this answer
 
v2
Comments
John Ertle Jr 2-Jan-23 23:34pm    
Where do you get dataGridView1 from? dataGridView1.DataSource=dv is needed to display the DataTable?
BillWoodruff 3-Jan-23 0:25am    
You either:

1) drag-drop a DataGridView Control onto a Windows Form at design time

or

2) create a new onw in code, and then add that to the Controls collection of a container control, like a Form or UserControl.

DataGridView dataGridView1 = new DataGridView();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900