2016-12-09 5 views
0

의 정보를 읽게하려면 어떻게해야합니까? 내 문제는 DataGridView가 이미 작성된 텍스트 파일에서 정보를 읽지 못하게하고, 실제로 무엇을해야할지 모르겠다는 것입니다. C#을 새로운, 그래서 당신이 날 도울 수 있기를 바랍니다 : DDataGridView에서 텍스트 파일 C#

여기

내 그리드에서 값을 저장하는 내 코드입니다 : 지금

private void buttonGuardar_Click(object sender, EventArgs e) 
{ 
    string[] conteudo = new string[dataGridView1.RowCount * dataGridView1.ColumnCount]; 
    int cont = 0; 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     foreach (DataGridViewCell cell in row.Cells) 
     { 
      conteudo[cont++] = cell.Value.ToString(); 
     } 
    } 
    File.WriteAllLines("dados.txt", conteudo); 

과는 다른 양식에서 다른 그리드가있다 해당 파일에 저장된 값으로 채워야합니다. 현재 코드 :

결론적
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 


namespace WindowsFormsApplication31 
{ 
    public partial class Form3 : Form 
    { 
     DateTime start, end; 

     private void Form3_Load(object sender, EventArgs e) 
     { 
      textBox1.Text = start.ToString("dd-MM-yyyy"); 
      textBox2.Text = end.ToString("dd-MM-yyyy"); 
     } 

     public Form3(DateTime s, DateTime e) 
     { 
      InitializeComponent(); 
      start = s; 
      end = e; 
     } 
    } 
} 

는 그리드 모두 104 개 세포 있어야 :

0 Designação을 1 그루 -2- 발러 3 데이터

그리고 Form3에서 두번째, 텍스트 파일을 올바른 순서로 읽어야합니다.

희망을 보내 주시면 감사하겠습니다.

+0

당신은 datagridview를 csv에 저장하고 다른 양식에서 해당 csv 파일을로드 할 수 있습니다. – Damith

답변

0

아래에서 확인하십시오. 그리드 데이터를 아래 그림과 같이 그리드와 동일한 구조로 텍스트 파일로 내 보냈습니다.

private void button1_Click(object sender, EventArgs e) 
    { 
     TextWriter writer = new StreamWriter("Text.txt"); 
     for (int i = 0; i < DataGridView1.Rows.Count; i++) 
     { 
      for (int j = 0; j < DataGridView1.Columns.Count; j++) 
      { 
       writer.Write(DataGridView1.Rows[i].Cells[j].Value.ToString() + "\t"); 
      } 
      writer.WriteLine(""); 

     } 
     writer.Close(); 
    } 

이 열 이름이 예에 표시된 below.Here 같은 클래스의 목록을 수집로 내 보낸 데이터를로드하는 방법 등의 특성을 가진 새로운 클래스를 생성, 내 그리드는 두 개의 열 이름과 Marks.so있다 제가

private void Form2_Load(object sender, EventArgs e) 
    { 
     dataGridView2.DataSource = User.LoadUserListFromFile("Text.txt"); 

    } 

아래와 같이 초 격자 위의 방법 및 바인드를 호출 번째 형태의 형태로드 이벤트

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace WindowsFormsApplication1 
{ 
public class User 
{ 

    public string Name { get; set; } 
    public string Marks { get; set; } 


    public static List<User> LoadUserListFromFile(string path) 
    { 
     var users = new List<User>(); 

     foreach (var line in File.ReadAllLines(path)) 
     { 
      var columns = line.Split('\t'); 
      users.Add(new User 
      { 
       Name = columns[0], 
       Marks = columns[1] 

      }); 
     } 

     return users; 
    } 
}} 

지금 나의 사용자 클래스 두 속성 선언 도움이된다면 이것을 답으로 표시하십시오.

+0

고맙습니다 폴이 정말 효과가 있었지만 한 가지 더 문제가 있습니다. 앞으로 추가 할 정보를 더하고 싶습니다. 다른 말로하면, 나는 모든 정보를 유지하기 위해 텍스트 라이터를 만들기 위해 코드 줄 아래에있는 텍스트 파일 – mptdias

+0

의 모든 정보를 추가하고 싶다. – paul

+0

TextWriter writer = 새 StreamWriter ("Text.txt", true); – paul