【问题标题】:Importing text file inside of DataGridView , C#在 DataGridView 中导入文本文件,C#
【发布时间】:2021-07-12 22:21:04
【问题描述】:

我有一个问题,点击按钮时,dataview 应该从文本文件中获取所有数据。 我不明白为什么它不起作用。 我有另一个功能可以将数据放入文本文件中,该功能有效。 任何人都可以帮助我吗? 提前致谢 This is code of the function which should get all data and put them into the dataviewgrid when I run the program, but there is some problem , I dont get error messages at all , but its not happening . This is code which works really good , it showing all columns and all data will be in text file filling data data which I got in txt file

    private void dodajToolStripMenuItem_Click(object sender, EventArgs e) {
 //upis u datoteku
 using (TextWriter smesti = new StreamWriter("podaci.txt")) {
   for (int i = 0; i < dataGridView1.Rows.Count; i++) {
  if (!dataGridView1.Rows[i].IsNewRow) {
    for (int j = 0; j < dataGridView1.Columns.Count; j++) {
      smesti.Write(dataGridView1.Rows[i].Cells[j].Value.ToString());
      if (j < dataGridView1.Columns.Count - 1) {
        smesti.Write("|");
      }
    }
    smesti.WriteLine();
  }
}

} }

private void Admin_Load(object sender, EventArgs e)
    {
        DataTable tabela = new DataTable();
        tabela.Columns.Add("ID", typeof(int));
        tabela.Columns.Add("Naziv", typeof(string));
        tabela.Columns.Add("Zanr", typeof(string));
        tabela.Columns.Add("Duzina", typeof(int));
        tabela.Columns.Add("Granica godina: ", typeof(int));

        dataGridView1.DataSource = tabela;
        
       
        

    }
   
    private void ucitaj()
    {
        DataTable tabela = new DataTable();
        string[] tekst = File.ReadAllLines("podaci.txt");
        string[] vrednosti;

        for (int i = 0; i < tekst.Length; i++)
        {
            vrednosti = tekst[i].ToString().Split('|');
            string[] red = new string[vrednosti.Length];
            for (int j = 0; j < vrednosti.Length; j++)
            {
                red[j] = vrednosti[j].Trim();
            }
            tabela.Rows.Add(red);
        }
        
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ucitaj();
    }

【问题讨论】:

  • 还有一件事,我如何设置 ID 列的主键? :)
  • 请编辑您的问题以显示您的实际代码,而不是它的图像
  • 好的,我添加了代码
  • 代码使用Tab和“|”写入文件有什么原因吗(Bar) 字符作为每个字段的分隔符+"\t"+"|",然后,当代码读取文件时,它似乎在“/”正斜杠字符上分割字段....Split('/');?这显然行不通。此外,在编写文件时,代码从不将每一“行”放在单独的行上。这将在文本文件中创建一 (1) 行数据。因此,您将不知道哪些数据进入了哪一行。你能澄清一下吗?
  • 那个“|”仅用于组织更好的数据。

标签: c# datagridview dataview


【解决方案1】:

尝试下面的代码将网格数据写入文件。该代码只是将每个单元格写入文件,然后写入一个条形“|”分隔符,除非该单元格是该行的最后一个单元格。我们不希望行尾有分隔符。

private void dodajToolStripMenuItem_Click(object sender, EventArgs e) {
  //upis u datoteku
  using (TextWriter smesti = new StreamWriter("podaci.txt")) {
    for (int i = 0; i < dataGridView1.Rows.Count; i++) {
      if (!dataGridView1.Rows[i].IsNewRow) {
        for (int j = 0; j < dataGridView1.Columns.Count; j++) {
          smesti.Write(dataGridView1.Rows[i].Cells[j].Value.ToString());
          if (j < dataGridView1.Columns.Count - 1) {
            smesti.Write("|");
          }
        }
        smesti.WriteLine();
      }
    }
  }
}

然后在读取文件的代码中......

DataTable tabela;

private void ucitaj() {
  //DataTable tabela = new DataTable();
  tabela.Rows.Clear();
  string[] tekst = File.ReadAllLines("podaci.txt");
  string[] vrednosti;

  for (int i = 0; i < tekst.Length; i++) {
    vrednosti = tekst[i].ToString().Split('|');
    string[] red = new string[vrednosti.Length];
    for (int j = 0; j < vrednosti.Length; j++) {
      red[j] = vrednosti[j].Trim();
    }
    tabela.Rows.Add(red);
  }
  dataGridView1.DataSource = tabela;
}

并且...您需要将 DataTable ...tabela 移动为全局变量。所以让tabela 全球...

DataTable tabela;

然后在Load事件中,换行...

DataTable tabela = new DataTable();

到...

tabela = new DataTable();

【讨论】:

  • 好的,我更改了代码,程序会将数据放入文本文件中,但是当我尝试导入该文本文件以读取此文件时出现同样的错误。我更改了此代码,但错误相同。
  • datagridview 上的最后一列(Granice godina),我仍然没有进入文本文件,这真的很奇怪
  • 使用读取文件的更新代码编辑您的问题。
  • 为什么程序无法读取并记住文本文件中的最后一列(Granice godina),可能是因为我运行按钮时无法加载?
  • 我只能猜测您可能正在查看错误的podaci.txt 文件。在我的测试中,使用发布的代码,最后一列按预期保存。您能否确认您正在查看正确的文件?
猜你喜欢
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 2010-11-22
  • 2021-01-04
  • 1970-01-01
相关资源
最近更新 更多