【问题标题】:Convert contents of DataGridView to List in C#在 C# 中将 DataGridView 的内容转换为列表
【发布时间】:2010-11-25 21:54:02
【问题描述】:

在 C# 中获取 DataGridView 的内容并将这些值放入列表的最佳方法是什么?

【问题讨论】:

    标签: c# list datagridview


    【解决方案1】:
            List<MyItem> items = new List<MyItem>();
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                MyItem item = new MyItem();
                foreach (DataGridViewCell dc in dr.Cells)
                { 
                    ...build out MyItem....based on DataGridViewCell.OwningColumn and DataGridViewCell.Value  
                }
    
                items.Add(item);
            }
    

    【讨论】:

    • 如果在 foreach 语句中我想访问行中第一个元素的值,我将如何做到这一点?
    • 第二个 foreach 将包含您的单元格数据...您逐行移动...同时从该行中的每个单元格中提取数据。然后使用 DataGridViewCell.OwningColumn 如果您需要匹配,否则它是线性的,您可以简单地使用 DataGridViewCell.Value 在数据中以水平方式移动时提取每个单元格的值
    【解决方案2】:

    如果您使用 DataSource 绑定您的列表,您可以通过以下方式转换回来:

    List<Class> myClass = DataGridView.DataSource as List<Class>;
    

    【讨论】:

      【解决方案3】:
      var Result = dataGridView1.Rows.OfType<DataGridViewRow>().Select(
                  r => r.Cells.OfType<DataGridViewCell>().Select(c => c.Value).ToArray()).ToList();
      

      或获取值的字符串字典

      var Result = dataGridView1.Rows.OfType<DataGridViewRow>().Select(
                  r => r.Cells.OfType<DataGridViewCell>().ToDictionary(c => dataGridView1.Columns[c.OwningColumn].HeaderText, c => (c.Value ?? "").ToString()
                      ).ToList();
      

      【讨论】:

        【解决方案4】:

        或linq方式

        var list = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                   from cell in row.Cells.Cast<DataGridViewCell>()
                   select new 
                   {
                     //project into your new class from the row and cell vars.
                   }).ToList();
        

        【讨论】:

          【解决方案5】:

          IEnumerable.OfType&lt;TResult&gt; 扩展方法可以成为你最好的朋友。以下是我通过 LINQ 查询完成的方法:

          List<MyItem> items = new List<MyItem>();
          dataGridView1.Rows.OfType<DataGridViewRow>().ToList<DataGridViewRow>().ForEach(
                          row =>
                          {
                              foreach (DataGridViewCell cell in row.Cells)
                              {
                                  //I've assumed imaginary properties ColName and ColValue in MyItem class
                                  items.Add(new MyItem { ColName = cell.OwningColumn.Name, ColValue = cell.Value });
                              }
                          });
          

          【讨论】:

            【解决方案6】:

            VB:

            Dim lst As List(Of DataGridViewRow) = Me.MasterDataGridView.Rows.Cast(Of DataGridViewRow).AsEnumerable.ToList
            

            C#:

            List<DataGridViewRow> lst = this.MasterDataGridView.Rows.Cast<DataGridViewRow>.AsEnumerable.ToList;
            

            【讨论】:

              猜你喜欢
              • 2019-04-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-06-12
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多