首页 > 解决方案 > 带有组合框的 DataGridView 不填充数据

问题描述

我有一个带有 1 个组合框列的 DataGridView,我用数据填充。我之前在 MSDN 论坛上问过,但他们无法帮助我,因为我使用的是SQLITE

它看到该列未填充数据,我不知道为什么。我的代码是

if ((cmbCol.DataSource = myList()) == null) 返回;

/************************************************/

    private List<string[]> myList()
    {
        string connectionString = dBFunctions.ConnectionStringSQLite;
        helper = new dBHelper(connectionString);
        if (helper.Load("SELECT Naam FROM Klant ORDER BY Naam", ""))
        {
            List<string[]> results = new List<string[]>();
            DataSet ds = helper.DataSet;
            DataTable dt = ds.Tables[0];
            dataRow = helper.DataSet.Tables[0].Rows[0];
            results =
            dt.Select()
                .Select(dataRow =>
                    dataRow.ItemArray
                        .Select(x => x.ToString())
                        .ToArray())
                .ToList();

            return results;
        }
        return null;
    }

标签: c#sqlite

解决方案


我确实更改了代码:

            if ((cmbCol.DataSource = myList(1)) == null)
                return;

            cmbCol.Name = "Naam";
            cmbCol.DisplayMember = "";
            cmbCol.ValueMember = "Naam";

            dataGridView1.Columns.Insert(2, cmbCol); // there is no field with the name 'Naam'
        }
    }

    private List<string> myList(int col)
    {
        string connectionString = dBFunctions.ConnectionStringSQLite;
        helper = new dBHelper(connectionString);
        if (helper.Load("SELECT * FROM Klant ORDER BY Naam", ""))
        {
            List<string> results = new List<string>();
            DataSet ds = helper.DataSet;
            DataTable dt = ds.Tables[0];

            dataRow = helper.DataSet.Tables[0].Rows[0];

            foreach(DataRow row in dt.Rows)
            {
                results.Add(row[col].ToString());
            }

            return results;
        }
        return null;
    }

但是组合框仍然是空的,为什么?数据在数据源中。


推荐阅读