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

时间:2017-07-11 11:52:47

标签: c# sql datagridview nullreferenceexception

我创建了一个带有嵌套关系的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;
    }

为什么如果它们确实不是,那么列是空的?我尝试使用int作为索引,但问题相同。

1 个答案:

答案 0 :(得分:2)

变化:

gridView.ViewCaption = "Activities";

为:

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