以程序方式创建数据集并设置为DataGrid,导致应显示的记录数为空行

本文关键字:显示 记录 创建 方式 程序 数据集 DataGrid 设置 | 更新日期: 2023-09-27 18:25:11

我正在处理一个c#项目,我正在尝试创建一个数据集并将数据网格的项源设置为该数据集。

但是,当网格加载时,它显示4条空行(数据库中有4条记录),并且没有显示任何列。

以下是我如何调用数据集创建函数并将其分配给DataGrid的代码。

private void loadData()
{
    Classes.SoftwareManager softwareManager = new Classes.SoftwareManager();
    DataSet dataSet = softwareManager.getDatasetForSoftware();
    if (dataSet != null)
    {
        softwareGrid.AutoGenerateColumns = false;
        dataSet.Tables[0].Columns.RemoveAt(0);
        softwareGrid.ItemsSource = dataSet.Tables[0].DefaultView;
    }
}

下面是创建数据集的代码,该数据集在上面的函数中返回。

public DataSet getDatasetForSoftware()
        {
            DataSet ds = new DataSet();
            DataTable table = new DataTable();
            DataColumn idCol = new DataColumn();
            DataColumn softwareCol = new DataColumn("Software Name");
            DataColumn serverCol = new DataColumn("DB Server");
            DataColumn userNameCol = new DataColumn("DB Username");
            DataColumn passwordCol = new DataColumn("DB Password");
            DataColumn portCol = new DataColumn("DB Port");
            DataColumn webInstallLocationCol = new DataColumn("Web Install Location");
            DataColumn softwareInstallLocationCol = new DataColumn("Software Install Location");
            DataColumn startScriptNameCol = new DataColumn("Start Script Name");
            DataColumn statusCol = new DataColumn("Status");
            idCol.DataType = System.Type.GetType("System.Int32");
            softwareCol.DataType = System.Type.GetType("System.String");
            serverCol.DataType = System.Type.GetType("System.String");
            userNameCol.DataType = System.Type.GetType("System.String");
            passwordCol.DataType = System.Type.GetType("System.String");
            portCol.DataType = System.Type.GetType("System.String");
            webInstallLocationCol.DataType = System.Type.GetType("System.String");
            softwareInstallLocationCol.DataType = System.Type.GetType("System.String");
            startScriptNameCol.DataType = System.Type.GetType("System.String");
            statusCol.DataType = System.Type.GetType("System.String");
            table.Columns.Add(idCol);
            table.Columns.Add(softwareCol);
            table.Columns.Add(serverCol);
            table.Columns.Add(userNameCol);
            table.Columns.Add(passwordCol);
            table.Columns.Add(portCol);
            table.Columns.Add(webInstallLocationCol);
            table.Columns.Add(softwareInstallLocationCol);
            table.Columns.Add(startScriptNameCol);
            table.Columns.Add(statusCol);

            List<SoftwareDetails> softwareDetails = getSoftwareDetails();
            if (softwareDetails != null)
            {
                foreach (SoftwareDetails software in softwareDetails)
                {
                    DataRow dataRow = table.NewRow();
                    dataRow[idCol] = software.id;
                    dataRow[softwareCol] = software.softwareName;
                    dataRow[serverCol] = software.dbServer;
                    dataRow[userNameCol] = software.dbUsername;
                    dataRow[passwordCol] = software.dbPassword;
                    dataRow[portCol] = software.dbPort;
                    dataRow[webInstallLocationCol] = software.webInstallLocation;
                    dataRow[softwareInstallLocationCol] = software.softwareInstallLocation;
                    dataRow[startScriptNameCol] = software.startScriptName;
                    dataRow[statusCol] = software.status;
                    table.Rows.Add(dataRow);
                }
            }
            ds.Tables.Add(table);
            return ds;
        }

感谢您提供的任何帮助。

以程序方式创建数据集并设置为DataGrid,导致应显示的记录数为空行

我看到softwareGrid.AutoGenerateColumns = false;如果你的网格不包含列定义,你应该设置

softwareGrid.AutoGenerateColumns = true;