hi everybody,

I am new to c sharp and was wondering if anyone could help me. I have a text file which has 10 records set out like this Smith,Adam,John,1986/11/24,M. I want to create a C# Windows Application program which reads the data and allows the user to browse the data record by record, allowing the user to specify First, Last, Previous and Next by pressing an appropriate button and also allow the user to change the data through a second form, and save the changed data to another text file.

Any help would be much appreciated

Recommended Answers

All 9 Replies

Where exactly do you wanna show the data? In textBoxes or in some data collection control (like datagridview)?
Depends on data binding.

Yes just in datagrid view

Why you dont want to show all the records from the text file in datagridview at ones?
Like:

string[] rows = File.ReadAllLines(@"C:\1\test35.txt");
            DataTable table = new DataTable("customers");
            table.Columns.AddRange(new DataColumn[] 
            { 
                new DataColumn("First name", typeof(string)), 
                new DataColumn("Middle name", typeof(string)), 
                new DataColumn("Last name", typeof(string)), 
                new DataColumn("Born", typeof(DateTime)), 
                new DataColumn("Gender", typeof(string)) 
            });
            DataRow dr;
            for (int i = 0; i < rows.Length; i++)
            {
                string[] columns = rows[i].Split(',');
                dr = table.NewRow();
                if (columns.Length == 5) //with middle name
                {
                    dr["First name"] = columns[0];
                    dr["Middle name"] = columns[1];
                    dr["Last name"] = columns[2];
                    dr["Born"] = columns[3];
                    dr["Gender"] = columns[4];
                }
                else if (columns.Length == 4) //no middle name (if it might occur)!
                {
                    dr["First name"] = columns[0];
                    dr["Last name"] = columns[1];
                    dr["Born"] = columns[2];
                    dr["Gender"] = columns[3];
                }
                table.Rows.Add(dr);
            }
            //bind table to datagridview:
            dataGridView1.DataSource = table.DefaultView;
commented: Nice. +15

The project you were working on in December 2011 already does a lot of the processing (like loading a text file into a collection of class objects).

Does that not serve as a good reference?

Code reuse will be one of the greatest benefits to your academic or business career.

yes i am trying to use some of that aswell, how would i edit the file, save it in datagrid view once i have imported it?

Let's say you've collected your records into class objects and into a collection.
You can choose to modify the records by reaching into the collection.
You can also take that collection and add it as the datasource of a DataGridView.
Check it out:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace DW_418402_CS_FORM
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         List<CPerson> lstPeople = new List<CPerson>()
         {
            new CPerson(){strFirstName = "Reginald", strLastName="Forman"},
            new CPerson(){strFirstName = "Clarence", strLastName="Bodaker"},
            new CPerson(){strFirstName = "William", strLastName="Rutherford"}
         };

         ///////////////////////////////////
         // Modify the data in some manner
         lstPeople =
            (
               from p in lstPeople
               where p.strLastName.Equals("Bodaker")
               select new CPerson() {strFirstName = p.strFirstName, strLastName = "Boddicker"}
            )
            .Union
            (
               from p in lstPeople
               where !p.strLastName.Equals("Bodaker")
               select new CPerson() {strFirstName = p.strFirstName, strLastName = p.strLastName}
            )
            .ToList();

         // >>> THIS // dataGridView1.DataSource = lstPeople; // OR

         dataGridView1.DataSource =
         (
            from person in lstPeople
            select new
            {
               FIRST_NAME = person.strFirstName,
               LAST_NAME = person.strLastName
            }
         ).ToList();
      }
   }

   public class CPerson
   {
      public string strFirstName { get; set; }
      public string strLastName { get; set; }
      public CPerson() { strFirstName = ""; strLastName = ""; }
      public override string ToString()
      {
         return strLastName + ", " + strFirstName;
      }
      public override bool Equals(object obj)
      {
         return this.ToString().Equals(((CPerson)obj).ToString());
      }

      public override int GetHashCode()
      {
         return this.ToString().GetHashCode();
      }
   }
}
commented: nice example :) +12

say i want to use a textbox instead of a datagrid view, how would i go about that?

You would need to format the text from the query and add it to the .Text property of the text box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.