Pergunta

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;
Foi útil?

Solução

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));
 }

Outras dicas

Does the "SomethingA" grid column even exists?

You're saying there's no null values because you make sure your query doesn't result any, but that's irrelevant on how you reach to your visual grid column object. If the grid has no "SomethingA" column then regardless of the data your code will fail for sure.

(The above is irrelevant now that I know we're talking about the XtraGrid)

UPDATE:

Okay first, to retrieve a DevExpress's GridView column by field name, you must use the column collection's ColumnByFieldName method:

gridView2.Columns.ColumnByFieldName("SomethingA").Visible = false;

Second, next time you ask a question, be sure to include all the necessary details. The fact that you are using a 3rd party grid, as you can see, changes everything. Not every grids out there are the same. I edited your post to add relevant tags. In the future, be sure to include them.

Third, when asked for an exception stack trace, it means that the error message alone isn't enough to properly diagnose the problem. The stack holds important information about the steps your code is taking. If you want help on an exception you are having it's best to provide that information.

Fourth and last, DevExpress have their own support center. Your questions will likely be better answered there.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top