creating tables in c# without sql or .mdb using data from a file

Hello All.
I wrote a stupid program in java that creates a table with 4 columsn and the fourst column being uneditable. I want to do the same in c#.

I have no clue how to instantiate a table or how to add information to it.
I have seriously! looked all over and everything i found realted to connecting to an sql or a .mdb
I dont' want to do this.

I want to be able to instantiate a table and fill it up wiht data that i will read from a file.

Can someone please give some code to work on for this...

Thanks a lot.

If i am not asking to much, all i want is form that has a men with file (open,save,saveas,quit) about  and i will fill up the data in the table when they click open.

so if they run the program it will initially look empty (only the collumn names are visilbe -3 columsn olnly.)

thanks.
lucilue2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smeggheadCommented:
Use a ListView, you can add columns to this control and populate the data yourself.

Smg.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jj819430Commented:
This is taken right from the quickstart tutorials
It uses a datagrid and populates it manually. (goes through the steps of setting up your columns) etc etc.

<%@ Page Debug="true" %>
<%@ Import Namespace="System.Data" %>

<html>
<script language="C#" runat="server">

    ICollection CreateDataSource() {
        DataTable dt = new DataTable();
        DataRow dr;

        dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
        dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
        dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
        dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

        for (int i = 0; i < 9; i++) {
            dr = dt.NewRow();

            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = DateTime.Now;
            dr[3] = (i % 2 != 0) ? true : false;
            dr[4] = 1.23 * (i+1);

            dt.Rows.Add(dr);
        }

        DataView dv = new DataView(dt);
        return dv;
    }

    void Page_Load(Object sender, EventArgs e) {
        MyDataGrid.DataSource = CreateDataSource();
        MyDataGrid.DataBind();
    }

</script>

<body>

    <h3><font face="Verdana">Specifying Columns in DataGrid</font></h3>

    <form runat=server>

      <ASP:DataGrid id="MyDataGrid" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
        AutoGenerateColumns="false">
          <Columns>
            <asp:BoundColumn HeaderText="Integer" DataField="IntegerValue" />
            <asp:BoundColumn HeaderText="Date/Time" DataField="DateTimeValue"/>
            <asp:BoundColumn HeaderText="String" DataField="StringValue"/>
            <asp:BoundColumn HeaderText="True/False" DataField="BoolValue"/>
            <asp:BoundColumn HeaderText="Price" DataField="CurrencyValue" DataFormatString="{0:c}" ItemStyle-HorizontalAlign="right" />
          </Columns>

      </asp:DataGrid>

  </form>

</body>
</html>

0
TheAvengerCommented:
You can use a DataGrid with DataGridTableStyles and DataGridColumns to define the columns and the look of the table. Then create a DataTable with the needed columns and create new rows in it for every record you need. Then set the DataTable as the DataSource of the DataGrid/DataGridTableStyle.

All words in the above text which are with PascalCase are classes or properties from .Net Framework and you can search for them in the MSDN library (msdn.microsoft.com/library)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

lucilue2003Author Commented:
I really learn best when i see code.
I have this talbe set up but i dont' like how you hav to click on the left in order for the table to show up. I am trying to figure out how to get rid of that and i cant. So i guess if you can tell me how to make it so that when the program is ran, it displays the table immediately please
: code below:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

      public class MultiTables : System.Windows.Forms.Form
      {
            private System.Data.DataSet dataSet;
            private System.Data.DataTable table;
            private System.Windows.Forms.DataGrid dataGrid1;
            private System.Data.DataView dataView1;
            private System.Data.DataColumn priority;
            private System.Data.DataColumn deadline;
            private System.Data.DataColumn description;
            private System.Data.DataColumn hasNotes;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
   

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

                  /// populate data tables here
                  DataRow aRow; // do table tables first since it has relations
                  aRow = table.NewRow();
                  aRow[0] = 461;      aRow[1] = 20;
                  aRow[2] = 261;      aRow[3] = "CS 585";
                  table.Rows.Add(aRow);
                  aRow = table.NewRow();
                  aRow[0] = 463;      aRow[1] = 21;
                  aRow[2] = 118;      aRow[3] = "CS 310";
                  table.Rows.Add(aRow);
                  aRow = table.NewRow();
                  aRow[0] = 550;      aRow[1] = 20;
                  aRow[2] = 115;      aRow[3] = "CS 585";
                  table.Rows.Add      (aRow);
                  aRow = table.NewRow();
                  aRow[0] = 464;      aRow[1] = 11;
                  aRow[2] = 118;      aRow[3] = "CS 465";
                  table.Rows.Add(aRow);
                  aRow = table.NewRow();
                  aRow[0] = 463;      aRow[1] = 21;
                  aRow[2] = 115;      aRow[3] = "CS 310";
                  table.Rows.Add(aRow);  
                  aRow = table.NewRow();
                  aRow[0] = 363;      aRow[1] = 21;
                  aRow[2] = 115;      aRow[3] = "Math 482";
                  table.Rows.Add(aRow);                      
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.dataSet = new System.Data.DataSet();
                  this.table = new System.Data.DataTable();
                  this.priority = new System.Data.DataColumn();
                  this.deadline = new System.Data.DataColumn();
                  this.description = new System.Data.DataColumn();
                  this.hasNotes = new System.Data.DataColumn();
                  this.dataGrid1 = new System.Windows.Forms.DataGrid();
                  
                  ((System.ComponentModel.ISupportInitialize)(this.dataSet)).BeginInit();
                  ((System.ComponentModel.ISupportInitialize)(this.table)).BeginInit();
                  ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
                  this.SuspendLayout();
                  //
                  // dataSet
                  //
                  this.dataSet.DataSetName = "Things To Do";
                  //this.dataSet.Locale = new System.Globalization.CultureInfo("en-US");
                  this.dataSet.Tables.Add(this.table);
                  //
                  // table
                  //

                  //this.dataSet.Tables.
                  this.table.Columns.AddRange(new System.Data.DataColumn[] {
                                                                                                             this.priority,
                                                                                                             this.deadline,
                                                                                                             this.description,
                                                                                                             this.hasNotes});
                  this.table.TableName = "Main Table";
                  //
                  // priority
                  //
                  this.priority.AllowDBNull = false;
                  this.priority.ColumnName = "Priority";
                  this.priority.DataType = typeof(int);
                  //
                  // deadline
                  //
                  this.deadline.ColumnName = "Deadline";
                  this.deadline.DataType = typeof(int);
                  //
                  // description
                  //
                  this.description.ColumnName = "Description";
                  this.description.DataType = typeof(int);
                  //
                  // hasNotes
                  //
                  this.hasNotes.ColumnName = "hasNotes";
                  //
                  // dataGrid1
                  //
                  this.dataGrid1.DataMember = "";
                  this.dataGrid1.DataSource = this.dataSet;
                  this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
                  this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                  this.dataGrid1.Location = new System.Drawing.Point(0, 0);
                  this.dataGrid1.Name = "dataGrid1";
                  this.dataGrid1.Size = new System.Drawing.Size(376, 218);
                  this.dataGrid1.TabIndex = 0;

                  //
                  // dataView1
                  //
                  //this.dataView1.Table = this.table;
                  this.dataView1 = new System.Data.DataView(this.table);
                  
                  //
                  // MultiTables
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(376, 218);
                  //this.Controls.Add(this.dataGrid1);
                  this.Name = "MultiTables";
                  this.Text = "dataSet Courses";
                  ((System.ComponentModel.ISupportInitialize)(this.dataSet)).EndInit();
                  ((System.ComponentModel.ISupportInitialize)(this.table)).EndInit();
                  ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
                  this.ResumeLayout(false);

            }
            #endregion

            [STAThread]
            static void Main()
            {
                  Application.Run(new MultiTables());
            }

      }
0
TheAvengerCommented:
OK, 2 simple things you have to do:

1. Uncomment this line:
//this.Controls.Add(this.dataGrid1);
This will show the grid (I think you made some tests or something)

2. Select the grid in the designer, go to the properties window and in the DataMember property select (or type in) Main Table (the name of your table). As a single dataset can contain more than one table, you need to specify the name of the table to be shown in the grid. Once you do this, run it and you will see immediately your data.

Following is the fixed code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class MultiTables : System.Windows.Forms.Form
{
      private System.Data.DataSet dataSet;
      private System.Data.DataTable table;
      private System.Windows.Forms.DataGrid dataGrid1;
      private System.Data.DataView dataView1;
      private System.Data.DataColumn priority;
      private System.Data.DataColumn deadline;
      private System.Data.DataColumn description;
      private System.Data.DataColumn hasNotes;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;
   

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

            /// populate data tables here
            DataRow aRow; // do table tables first since it has relations
            aRow = table.NewRow();
            aRow[0] = 461;      aRow[1] = 20;
            aRow[2] = 261;      aRow[3] = "CS 585";
            table.Rows.Add(aRow);
            aRow = table.NewRow();
            aRow[0] = 463;      aRow[1] = 21;
            aRow[2] = 118;      aRow[3] = "CS 310";
            table.Rows.Add(aRow);
            aRow = table.NewRow();
            aRow[0] = 550;      aRow[1] = 20;
            aRow[2] = 115;      aRow[3] = "CS 585";
            table.Rows.Add     (aRow);
            aRow = table.NewRow();
            aRow[0] = 464;      aRow[1] = 11;
            aRow[2] = 118;      aRow[3] = "CS 465";
            table.Rows.Add(aRow);
            aRow = table.NewRow();
            aRow[0] = 463;      aRow[1] = 21;
            aRow[2] = 115;      aRow[3] = "CS 310";
            table.Rows.Add(aRow);  
            aRow = table.NewRow();
            aRow[0] = 363;      aRow[1] = 21;
            aRow[2] = 115;      aRow[3] = "Math 482";
            table.Rows.Add(aRow);                
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
            if( disposing )
            {
                  if (components != null)
                  {
                        components.Dispose();
                  }
            }
            base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
            this.dataSet = new System.Data.DataSet();
            this.table = new System.Data.DataTable();
            this.priority = new System.Data.DataColumn();
            this.deadline = new System.Data.DataColumn();
            this.description = new System.Data.DataColumn();
            this.hasNotes = new System.Data.DataColumn();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.dataView1 = new System.Data.DataView();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.table)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataView1)).BeginInit();
            this.SuspendLayout();
            //
            // dataSet
            //
            this.dataSet.DataSetName = "Things To Do";
            this.dataSet.Locale = new System.Globalization.CultureInfo("en-US");
            this.dataSet.Tables.AddRange(new System.Data.DataTable[] {
                                                                                                       this.table});
            //
            // table
            //
            this.table.Columns.AddRange(new System.Data.DataColumn[] {
                                                                                                       this.priority,
                                                                                                       this.deadline,
                                                                                                       this.description,
                                                                                                       this.hasNotes});
            this.table.TableName = "Main Table";
            //
            // priority
            //
            this.priority.AllowDBNull = false;
            this.priority.ColumnName = "Priority";
            this.priority.DataType = typeof(int);
            //
            // deadline
            //
            this.deadline.ColumnName = "Deadline";
            this.deadline.DataType = typeof(int);
            //
            // description
            //
            this.description.ColumnName = "Description";
            this.description.DataType = typeof(int);
            //
            // hasNotes
            //
            this.hasNotes.ColumnName = "hasNotes";
            //
            // dataGrid1
            //
            this.dataGrid1.DataMember = "Main Table";
            this.dataGrid1.DataSource = this.dataSet;
            this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(0, 0);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.Size = new System.Drawing.Size(376, 218);
            this.dataGrid1.TabIndex = 0;
            //
            // MultiTables
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(376, 218);
            this.Controls.Add(this.dataGrid1);
            this.Name = "MultiTables";
            this.Text = "dataSet Courses";
            ((System.ComponentModel.ISupportInitialize)(this.dataSet)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.table)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataView1)).EndInit();
            this.ResumeLayout(false);

      }
      #endregion

      [STAThread]
      static void Main()
      {
            Application.Run(new MultiTables());
      }

}
0
HarmnKardnCommented:
I agree with Smegghead, use a ListView.  Create one in forms designer.  Set the "View" propety to "Details", and modify the "Columns" collection to add the column captions you want.  The control is very easy to use.

I believe DataGrid control is beyond the scope of what you need.
0
TheAvengerCommented:
Making a ListView editable (except the last column) is much more work than using DataGrid...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.