列被称为 null,而实际上它们不是 C# WinForms

Columns are said to be null while they really aren't C# WinForms

提问人:Kahn Kah 提问时间:7/11/2017 更新时间:7/11/2017 访问量:59

问:

我创建了一个具有嵌套关系的 datagridview,该关系每行列出事件及其活动。这是通过使用 DevExpress 实现的:

enter image description here

我的问题是,我根本没有访问这些列的权限。

这意味着我无法设置隐藏的列、列宽等。NullReferenceException

这是我使用的代码:

 private void DisplayData()
    {
        conn = new SqlConnection("Server=.\\SQLEXPRESS;Database=Ticketing;Integrated Security=true");
        daIncidents = new SqlDataAdapter("Select * from incidents", conn);
        daActivities = new SqlDataAdapter("Select * from Activity", conn);
        ds = new DataSet();

        daIncidents.Fill(ds, "Incidents");
        daActivities.Fill(ds, "Activities");

        DataColumn keyColumn = ds.Tables["Incidents"].Columns["IncidentID"];
        DataColumn foreignKeyColumn = ds.Tables["Activities"].Columns["IncidentID"];
        ds.Relations.Add("IncidentsActivities", keyColumn, foreignKeyColumn);

        gridControl.DataSource = ds.Tables["Incidents"];
        gridControl.ForceInitialize();

        GridView gridView = new GridView(gridControl);

        gridControl.LevelTree.Nodes.Add("IncidentsActivities", gridView);
        gridView.ViewCaption = "Activities";

        //gridView.Columns["ActivityID"].ReadOnly = true; 
        //gridView.Columns["Description"].Width = 40;
    }

如果列真的不是,为什么说它们为 null?我尝试使用作为索引,但同样的问题。int

C# SQL DataGridView NullReferenceException

评论

0赞 mjwills 7/11/2017
哪一行引发了异常?
0赞 Kahn Kah 7/11/2017
评论的最后几行
0赞 mjwills 7/11/2017
如果你在final上放了一个断点,然后调试它(所以它在断点上中断),然后在你的窗口中输入,你会看到什么?}?gridView.ColumnsImmediate
0赞 Kahn Kah 7/11/2017
我用过,它在列编辑之前说它们是 0。但是如果我不进行列编辑并计算列数,它也是 0。那么,我在哪里编辑列呢?Columns.Count()
0赞 mjwills 7/11/2017
这些链接有帮助吗?devexpress.com/Support/Center/Question/Details/B204415/...... devexpress.com/Support/Center/Question/Details/Q275799/...... devexpress.com/Support/Center/Question/Details/T207383/......

答:

2赞 mjwills 7/11/2017 #1

改变:

gridView.ViewCaption = "Activities";

自:

gridView.ViewCaption = "Activities";
gridView.PopulateColumns();

评论

0赞 Kahn Kah 7/11/2017
同样的问题:/