列表到数据表.数据表不显示任何值

本文关键字:数据表 任何值 显示 列表 | 更新日期: 2023-09-27 18:37:28

我正在尝试制作一个"历史页面"。读取用户以字符串形式存在的网址并将其添加到列表中并将其转换为 DataTable 但是当我单击显示历史记录菜单选项时,我得到的只是列"urls"和它下面的一个空单元格。我想我可能也不正确地使用了 Add 方法。

主窗体类:

private void showHistoryToolStripMenuItem_Click(object sender, EventArgs e)
{
   using (History history = new History())
   {
      history.ShowDialog();
      nonHomepage = URLInput.Text;
      if (String.IsNullOrEmpty(nonHomepage))
      {
         return;
      }
      else
      {
         addToList(nonHomepage);
      }
   }
}
public void addToList(string URLvalue)
{
   listH.Add(URLvalue);
}
public List<string> getList()
{
   return listH;
}

历史表单类:

private void History_Load(object sender, EventArgs e)
{
   Form1 br = new Form1();
   list = br.listH;
   DataTable table = ConvertListToDataTable(list);
   dataGridView1.DataSource = table;
}
static DataTable ConvertListToDataTable(List<string> l)
{
   DataTable table = new DataTable();
   //int columns = 0;
   table.Columns.Add("urls");
   foreach(string s in l)
   {
      table.Rows.Add(s);
   }
   return table;
}

有什么建议吗?如果我将所有这些 url 放在文件中,然后从文件中读取并写入文本框/表格怎么办?或者也许我应该更改数据结构?例如,去字典?提前谢谢。

列表到数据表.数据表不显示任何值

添加表行时,实际上必须添加一行,而不仅仅是字符串。

    foreach(string s in l)
    {
        var row = table.NewRow();
        row[0] = s;
        table.Rows.Add(row);
    }
    return table;
此外,在

转换列表之前添加断点并确保列表不为空,并确保之后正确填充表。


此外,从架构的角度来看,如果你只有一列信息,你不应该真正使用DataTable,一个List<T>就足够了。您在这里使用DataTable有什么原因吗?

你的问题是你在private void History_Load(object sender, EventArgs e)中创建了一个空Form1,并将listH(为空)传递到方法ConvertListToDataTable(list)中,因此你有空的网格。解决方案是您必须更改History初始化或显式调用某个方法LoadData加载实际列表,如下所示:

解决方案 1

public partial class History : Form {
  public History(){
    InitializeComponent();
  }
  public Form1 MainForm {get;set;}
  private void History_Load(object sender, EventArgs e) {     
    var list = MainForm == null ? new List<string>() : MainForm.listH;
    DataTable table = ConvertListToDataTable(list);
    dataGridView1.DataSource = table;
  }
  //other code ....
}
//Form1 class
private void showHistoryToolStripMenuItem_Click(object sender, EventArgs e) {
  //note the MainForm initialization using Property initializer
  using (History history = new History {MainForm = this}) {
    history.ShowDialog();
    nonHomepage = URLInput.Text;
    if (String.IsNullOrEmpty(nonHomepage)) {
      return;
    } else {
      addToList(nonHomepage);
    }
  }
}

解决方案 2

//History class
public partial class History : Form {
  //define this method to call explicitly before showing your History dialog
  public void LoadData(List<string> list){
    DataTable table = ConvertListToDataTable(list);
    dataGridView1.DataSource = table;
  }
  //other code ...
}
//Form1 (or Main Form) class
private void showHistoryToolStripMenuItem_Click(object sender, EventArgs e) {
  using (History history = new History()) {
     history.LoadData(listH);// <---- call this first to load data
     history.ShowDialog();
     nonHomepage = URLInput.Text;
     if (String.IsNullOrEmpty(nonHomepage)) {
       return;
     } else {
       addToList(nonHomepage);
     }
  }
}

SpikeX答案的替代语法:

 int i = 0;
 foreach (string s in l)
 {
      table.Rows.Add()
      tables.Rows[i].SetField("COLUMN NAME", s);
      i++
 }

我想你的表中只有 1 列,所以使用 SetField 可能有点多余。但是,当您有多个列时,它更容易阅读,而不必返回并检查哪个列具有哪个索引。