Search code examples
c#.netwinformsdevexpressxtragrid

GridControl Object reference not set to an instance of an object


I got Object reference not set to an instance of an object w I read it first to DataTable then

  gridControl2.DataSource = dataTable5;

I read .txt file to GridControl just fine. I got the error After using :

gridView2.Columns["SomethingA"].Visible = false;

I don't think there is null values on the Columns since I took those values using a SQL Query and avoided null values.

EDIT: Full code

 char[] chrArray10 = new char[1];
        chrArray10[0] = '\t';
        Assembly assembly = Assembly.LoadFile("C:/Users/PC-ASQ/documents/visual studio 2013/Projects/ClassLibrary1/ClassLibrary1/bin/Debug/Mydll.dll");

        System.Resources.ResourceManager resourcemanager = new System.Resources.ResourceManager("ClassLibrary1.Properties.Resources", assembly);


        var textList = resourcemanager.GetString("GISS").Split(chrArray10);
        string[] strArrays15 = resourcemanager.GetString("GISS").Split('\n');


        string[] strArrays16 = strArrays15[0].Split(chrArray10);
        DataTable dataTable5 = new DataTable();
        string[] strArrays17 = strArrays16;
        for (int s = 0; s < (int)strArrays17.Length; s++)
        {
            string str5 = strArrays17[s];
            dataTable5.Columns.Add(str5);
        }
        for (int t = 1; t < (int)strArrays15.Length; t++)
        {
            char[] chrArray11 = new char[1];
            chrArray11[0] = '\t';
            dataTable5.Rows.Add(strArrays15[t].Split(chrArray11));
        }


        gridControl2.DataSource = dataTable5;


       gridView2.Columns["ToolTip"].Visible = false;
       gridView2.Columns["Icons"].Visible = false;


        return;

Solution

  • I checked with this code,it's working fine.

     DataTable datatable5 = new DataTable();
     datatable5.Columns.Add("ToolTip");
     datatable5.Columns.Add("Icons");
     datatable5.Columns.Add("ID");
     datatable5.Columns.Add("Number");
    
     for (int i = 0; i < 4; i++)
         datatable5.Rows.Add(new object[] { String.Format("ToolTip{0}.", i),i,i,i});
    
      gridControl2.DataSource = dataTable5;
    
      gridView2.Columns["ToolTip"].Visible = false;
      gridView2.Columns["Icons"].Visible = false;
    

    In your code : place a breakpoint and check how the column names are adding to datatable

     DataTable dataTable5 = new DataTable();
     string[] strArrays17 = strArrays16;
     for (int s = 0; s < (int)strArrays17.Length; s++)
     {
         string str5 = strArrays17[s];
         dataTable5.Columns.Add(str5);//check any extra space adding here
     }
     for (int t = 1; t < (int)strArrays15.Length; t++)
     {
         char[] chrArray11 = new char[1];
         chrArray11[0] = '\t';
         dataTable5.Rows.Add(strArrays15[t].Split(chrArray11));
     }