0

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

但是,当网格加载时,它显示 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;
        }

感谢您的任何帮助,您可以提供。

4

1 回答 1

1

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

softwareGrid.AutoGenerateColumns = true;
于 2013-07-28T22:20:38.550 回答