【问题标题】:how to load a list<t> from a datagridview如何从 datagridview 加载列表<t>
【发布时间】:2019-01-02 15:45:41
【问题描述】:

我有一个网格视图,我正在尝试使用网格视图中的一列加载列表,我得到一个空引用异常

我试过了

    public static List<string> LoadStringList()
    {
        List<string> stringList = new List<string>();

        if (contactDataGridView.RowCount != 0)
        {
            for (int i = 0; i < contactDataGridView.Rows.Count; i++)
            {
                stringList.Add((string)contactDataGridView.Rows[i].Cells[2].Value);
            }

        }
        return stringList;
    }

我试过了

    public static List<string> LoadStringList()
    {
        List<string> stringList = new List<string>();

        if (frmPRG299.mainForm.contactDataGridView.RowCount != 0)
        {
            for (int i = 0; i <frmPRG299.mainForm.contactDataGridView.Rows.Count; i++)
            {
                stringList.Add((string)frmPRG299.mainForm.contactDataGridView.Rows[i].Cells[2].Value);
            }

        }
        return stringList;
    }

进一步解释

我有两种形式 frmMain 和 frmSub,其中 gridview 在 frmMain 和一个组合框在 frmSub 我需要调用函数 LoadStringList() 来填充组合框

【问题讨论】:

  • 大多数其他用户会将您重定向到这里:What is a NullReferenceException, and how do I fix it?
  • 在将其添加到 stringList 之前尝试检查 null 或空。尝试使用 as 而不是 cast。
  • @Viju 我需要转换,因为我需要将对象类型转换为字符串类型
  • 从函数中删除静态词并尝试引用实际对象。或者,将您的呼叫更改为 public static List&lt;string&gt; LoadStringList(DataGridView dgv) 并将现有控件作为参考传递。
  • 你总是可以做 var stringData = contactDataGridView.Rows[i].Cells[2].Value as string; if(!string.IsNullOrEmpty(stringData )) { 将 stringData 添加到 stringList }

标签: c# datagridview


【解决方案1】:

使用允许您引用对象(在本例中为 Control)的方法,并将对该对象的引用传递给该方法。
如果没有硬编码的对象引用,您的方法会更加灵活。

在这里,我将 DataGridView 控件引用和从中提取当前值的单元格编号传递给该方法。

由于Cell.Value 可能是null,因此您必须在尝试读取它和/或将其转换为所需类型之前对其进行验证。

List<string> MyList = LoadStringList(this.dataGridView1, 2);


public List<string> LoadStringList(DataGridView dgv, int cell)
{
    if ((dgv == null) || (dgv.RowCount == 0)) return null;

    List<string> result = dgv.Rows.Cast< DataGridViewRow>()
        .Select(r => { return r.Cells[cell].Value != null 
                            ? r.Cells[cell].Value.ToString() 
                            : default; })
        .ToList();

    return result;
}

如果需要更通用的输入类型:

try
{ 
    List<int> MyList = LoadList<int>(this.dataGridView1, 2).ToList();
}
catch (Exception ex)
{
    //Handle the exception/Update the UI/Prompt the User
    Console.WriteLine(ex.Message);
}


public IEnumerable<T> LoadList<T>(DataGridView dgv, int cell)
{
    if ((dgv == null) || (dgv.RowCount == 0)) return null;

    IEnumerable<T> result = null;

    try
    {
        result = dgv.Rows.Cast<DataGridViewRow>()
                         .Select(r => { return r.Cells[cell].Value != null
                                   ? (T)Convert.ChangeType(r.Cells[cell].Value, typeof(T))
                                   : default;
                            })
                         .ToList();
    }
    catch (Exception ex) {
        //Manage the exception as required
        throw ex;
    }
    return result;
}

【讨论】:

    【解决方案2】:

    而不是使用 stringList.Add((string)contactDataGridView.Rows[i].Cells[2].Value); 将代码更改为 stringList.Add(contactDataGridView.Rows[i].Cells["YOUR COLUMN NAME"].Value+"");

    【讨论】:

    • 非静态字段、方法或属性“frmPRG299.contactDataGridView”需要对象引用
    • 从函数中移除静态关键字
    【解决方案3】:
      public List<string> LoadStringList(DataGridView contactDataGridView)
        {
            List<string> stringList = new List<string>();
    
            if (contactDataGridView.RowCount != 0)
            {
                for (int i = 0; i < contactDataGridView.Rows.Count; i++)
                {
                    var stringData = contactDataGridView.Rows[i].Cells[2].Value as string;
                    if(!string.IsNullOrEmpty(stringData))
                    {
                        stringList.Add(stringData);
                    }
                }
    
            }
            return stringList;
        }
    

    【讨论】:

    • 将不起作用,因为非静态字段、方法或属性“frmPRG299.contactDataGridView”需要对象引用
    • 为什么要去掉 static 关键字?你能解释一下吗
    • 如果在实例中可以访问contactDataGridView,则静态将不起作用,但如果您将DataGridView传递给该方法,则它可以是静态的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多