2013-10-19 14 views
0

尝试将List中的一些数据放到dataGridView中,但是它有一些问题。如何把数据从列表<string []>到dataGridView

目前有方法,返回我需要的清单 - 请下面的图片

代码

public List<string[]> ReadFromFileBooks() 
    { 
     List<string> myIdCollection = new List<string>(); 
     List<string[]> resultColl = new List<string[]>(); 
     if (chooise == "all") 
     { 
      if (File.Exists(filePath)) 
      { 
       using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) 
       {       
        StreamReader sr = new StreamReader(fs); 
        string[] line = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, 
         StringSplitOptions.RemoveEmptyEntries); 
        foreach (string l in line) 
        { 
         string[] result = l.Split(','); 
         foreach (string element in result) 
         { 
          myIdCollection.Add(element); 
         } 
         resultColl.Add(new string[] { myIdCollection[0], myIdCollection[1], myIdCollection[2], myIdCollection[3] }); 
         myIdCollection.Clear(); 
        } 
        sr.Close(); 
        return resultColl; 
       } 
      } 
    .... 

这回我需要requred形式的数据(比如从数组列表)。 enter image description here

在此之后,尝试将其移动到DataGridView,已经有4列有姓名(因为我敢肯定,这需要不超过4 colums) - 请PIC下面

enter image description here

尝试使用下面的代码把数据中的dataGridView

private void radioButtonViewAll_CheckedChanged(object sender, EventArgs e) 
    { 
     TxtLibrary myList = new TxtLibrary(filePathBooks); 
     myList.chooise = "all"; 
     //myList.ReadFromFileBooks(); 
     DataTable table = new DataTable(); 
     foreach (var array in myList.ReadFromFileBooks()) 
     { 
      table.Rows.Add(array); 
     } 
     dataGridViewLibrary.DataSource = table; 
    } 

但作为结果遇到错误 - “中存在的DataGridView需要更多行”,但accordint什么,我看到(PIC以上)q-TY的行(4)等于List(4)中数组元素的q-ty。

尝试将额外的临时变量检查结果 - 但它的确定 - 请参见下面

enter image description here

如果我错了,好看吗?也许我用正确的方式使用dataGridView?

编辑文件的

例子(简单CSV)

11111,作者,名称,类别

11341,作者1,名1,Categories1这

回答

2

您需要列添加到您的DataTable第一:

private void radioButtonViewAll_CheckedChanged(object sender, EventArgs e) 
{ 
    TxtLibrary myList = new TxtLibrary(filePathBooks); 
    myList.chooise = "all"; 
    DataTable table = new DataTable(); 
    //add columns first 
    table.Columns.Add("ID"); 
    table.Columns.Add("Author"); 
    table.Columns.Add("Caption"); 
    table.Columns.Add("Categories"); 
    //then add rows 
    foreach (var array in myList.ReadFromFileBooks()) { 
     table.Rows.Add(array); 
    } 
    dataGridViewLibrary.DataSource = table; 
} 
+0

它的工作完美,所以我的数据结构dataGridView不能用于部分配置的DataSourse?或存在某种方式来做到这一点?意思是如果我在FormConstructor中创建colums,我可以在代码中创建只是一排或反正我必须在代码中创建colums? – gbk

+1

@Kirill在将行添加到DataTable之前,点是**,它必须至少有1列,换句话说,在添加行**之前它应该有一些表结构。你不需要手动添加列到你的'DataGridView',当你分配'dataGridViewLibrary.DataSource = table'时,'Gridview'会自动生成相应的列(AutoGenerateColumns = true)。 –

+0

哦。明确。谢谢 – gbk

1

我觉得你的代码,它太复杂。简单地说,如果你想看到从文件表中的所有数据,您可以添加行之前做到这一点

if (!System.IO.File.Exists("file.txt")) 
      return; 
     dgvDataGridView.ColumnCount = 4; 
     dgvDataGridView.Columns[0].HeaderCell.Value = "ID"; 
     dgvDataGridView.Columns[1].HeaderCell.Value = "Author"; 
     dgvDataGridView.Columns[2].HeaderCell.Value = "Caption"; 
     dgvDataGridView.Columns[3].HeaderCell.Value = "Categories"; 
     using (System.IO.StreamReader sr = new System.IO.StreamReader("file.txt")) 
      while (sr.Peek() > -1) 
       dgvDataGridView.Rows.Add(sr.ReadLine().Split(',')); 
相关问题