Solved

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

Posted on 2003-12-02
9
437 Views
Last Modified: 2008-03-04
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.
0
Comment
Question by:lucilue2003
9 Comments
 
LVL 10

Accepted Solution

by:
smegghead earned 42 total points
ID: 9860619
Use a ListView, you can add columns to this control and populate the data yourself.

Smg.
0
 
LVL 7

Assisted Solution

by:jj819430
jj819430 earned 41 total points
ID: 9860807
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
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9862386
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lucilue2003
ID: 9863558
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
 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 41 total points
ID: 9864960
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
 
LVL 1

Assisted Solution

by:HarmnKardn
HarmnKardn earned 41 total points
ID: 9942769
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
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9943054
Making a ListView editable (except the last column) is much more work than using DataGrid...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now