tablestyles Object reference not set to an instance

  • Thread starter Thread starter Eddie B.
  • Start date Start date
E

Eddie B.

I can't seem to find out why i get "Object reference not set to an instance
of an object" error when I put a loop to test what DataTable columns are
present and the loop says that the col is present in the data table that I
am using.


DataTable dtClass = new DataTable();
dtClass = nmopl.GetDrugByClass_PL("");
dsMeds.Tables.Add(dtClass);

DataView dv = new DataView();
dv.Table = dtClass;

this.dgNewMedOrders.DataSource = dtClass;

foreach(DataColumn col in cols)
{
if(col.ColumnName == "ID")
{
MessageBox.Show(col.ColumnName + " Exists");
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "dtClass";
this.dgNewMedOrders.TableStyles.Clear();
dgNewMedOrders.TableStyles.Add(dgts);

dgNewMedOrders.TableStyles["dtClass"].GridColumnStyles["ID"].Width =
10; <---------PROBLEM HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

}
else
{
MessageBox.Show(col.ColumnName + " is the present column");
}
}
 
Eddie,

I think the reason is because you have added the table styles, but not
the column styles. I don't believe that the column styles get
auto-populated when the table style is set. You have to do it manually.

Hope this helps.
 
The table already has columns and when I try to add the columnstyle I am
told that the column already exists.
Any suggestions?

Thanks,

Eddie


Nicholas Paldino said:
Eddie,

I think the reason is because you have added the table styles, but not
the column styles. I don't believe that the column styles get
auto-populated when the table style is set. You have to do it manually.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Eddie B. said:
I can't seem to find out why i get "Object reference not set to an instance
of an object" error when I put a loop to test what DataTable columns are
present and the loop says that the col is present in the data table that I
am using.


DataTable dtClass = new DataTable();
dtClass = nmopl.GetDrugByClass_PL("");
dsMeds.Tables.Add(dtClass);

DataView dv = new DataView();
dv.Table = dtClass;

this.dgNewMedOrders.DataSource = dtClass;

foreach(DataColumn col in cols)
{
if(col.ColumnName == "ID")
{
MessageBox.Show(col.ColumnName + " Exists");
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "dtClass";
this.dgNewMedOrders.TableStyles.Clear();
dgNewMedOrders.TableStyles.Add(dgts);

dgNewMedOrders.TableStyles["dtClass"].GridColumnStyles["ID"].Width =
10; <---------PROBLEM HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

}
else
{
MessageBox.Show(col.ColumnName + " is the present column");
}
}
 
Eddie,

In that case, I would look in the debugger at the following terms:

dgNewMedOrders
dgNewMedOrders.TableStyles["dtClass"]
dgNewMedOrders.TableStyles["dtClass"].GridColumnStyles["ID"].Width

One of these is bound to return null, and you can find out which it is.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Eddie B. said:
The table already has columns and when I try to add the columnstyle I am
told that the column already exists.
Any suggestions?

Thanks,

Eddie


message news:%[email protected]...
Eddie,

I think the reason is because you have added the table styles, but not
the column styles. I don't believe that the column styles get
auto-populated when the table style is set. You have to do it manually.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Eddie B. said:
I can't seem to find out why i get "Object reference not set to an instance
of an object" error when I put a loop to test what DataTable columns are
present and the loop says that the col is present in the data table
that
dgNewMedOrders.TableStyles["dtClass"].GridColumnStyles["ID"].Width
 
I was getting the same error in C#. I fixed it by using the Invoke
command. I am updating a DataTable which is a datasource of a
DataGrid. I was just directly calling the procedure that updated the
DataTable. This was causing an exception outside my code, like you
are getting. It has something to do with accessing a form component
from your thread that doesn't "own" the component.

Here is what fixed it for me:

Define the procedure that does the updating like this:
public void UpdGrid_handler(object sender, EventArgs evArgs)

Whenever you need to update the grid, use a line like this:
grid.Invoke(new EventHandler(UpdGrid_handler));

Hope this helps,
Mick
 
Back
Top