Additional information: Object reference not set to an instance of an object.

M

michael walser

I get this message in "Spaltenbreite definieren" => Definition for Columns
Width

An unhandled exception of type 'System.NullReferenceException' occurred in
WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object.

Please help me.

Thanks

Michael



CODE:
=====
private void CreateMyDatagrid()

{

// MyDatagrid erstellen

DataGrid MyDataGrid = new DataGrid();

MyDataGrid.Bounds = new Rectangle(new Point(10,10), new Size(650,350));

// Datagrid Eigenschaften

MyDataGrid.CaptionText="Berechtigung -> ALLE = A+B+E+L / A=Ansehen /
B=Bearbeiten / E = Erstellen / L=Löschen";

MyDataGrid.ReadOnly = false;

// Dataset erstellen

DataSet MyDataSet = new DataSet();


DataGridTableStyle myTableStyle = new DataGridTableStyle();

myTableStyle.MappingName = "MappingName_ts_berechtigung"; // ts=tablestyle

DataGridBoolColumn boolCol = new DataGridBoolColumn();

boolCol.MappingName="MappingName_dgbc_berechtigung";
//dgbc=datagridboolcolumn

((DataGridBoolColumn)boolCol).AllowNull = false;

myTableStyle.GridColumnStyles.Add(boolCol);

//MyDataGrid.TableStyles.Clear();

// DataTable erstellen

DataTable dt = new DataTable("DataTable_Berechtigung");


// Spalten erstellen

dt.Columns.Add("Modul", typeof(string)).ReadOnly = true;

dt.Columns.Add("Zusatz...", typeof(string)).ReadOnly = true;

dt.Columns.Add("ALLE", typeof(bool));

dt.Columns.Add("A", typeof(bool));

dt.Columns.Add("B", typeof(bool));

dt.Columns.Add("E", typeof(bool));

dt.Columns.Add("L", typeof(bool));

//Spaltenbreite definieren

myTableStyle.GridColumnStyles["Modul"].Width=20;

myTableStyle.GridColumnStyles["Zusatz..."].Width=50;

myTableStyle.GridColumnStyles["ALLE"].Width=60;



//MyDataGrid.TableStyles["DataTable_Berechtigung"].GridColumnStyles[0].Width
=0;

//MyDataGrid.TableStyles["DataTable_Berechtigung"].GridColumnStyles[1].Width
=80;

//MyDataGrid.TableStyles["DataTable_Berechtigung"].GridColumnStyles[2].Width
=80;

//MyDataGrid.TableStyles["DataTable_Berechtigung"].GridColumnStyles[3].Width
=20;

// Zeilen erstellen und inhalt abfüllen

// checkbox in variable

int test = 1;

dt.Rows.Add(new Object[] {"Adresse", "ev. sonst noch was",0,test,0,0,0});

dt.Rows.Add(new Object[] {"Depositen", "ev. sonst noch was",1,1,1,1,1});

dt.Rows.Add(new Object[] {"Obligation", "ev. sonst noch was",0,0,0,0,0});

dt.Rows.Add(new Object[] {"Miete", "ev. sonst noch was",1,1,1,1,1});

dt.Rows.Add(new Object[] {"Stammdaten", "ev. sonst noch was",0,0,0,0,0});

// DataTable in dataset

MyDataSet.Tables.Add(dt);


// Dataset in datagrid

MyDataGrid.SetDataBinding(MyDataSet,"DataTable_Berechtigung");

// Tablestyle dem Datagrid hinzufügen

MyDataGrid.TableStyles.Add(myTableStyle);


// Anzeigen des Datagrids

this.Controls.Add(MyDataGrid);

}
 
F

Frans Bouma [C# MVP]

I get this message in "Spaltenbreite definieren" => Definition for
Columns Width

An unhandled exception of type 'System.NullReferenceException' occurred
in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object.

Please help me.
// Spalten erstellen

dt.Columns.Add("Modul", typeof(string)).ReadOnly = true;
dt.Columns.Add("Zusatz...", typeof(string)).ReadOnly = true;
dt.Columns.Add("ALLE", typeof(bool)); ....
//Spaltenbreite definieren

myTableStyle.GridColumnStyles["Modul"].Width=20;
myTableStyle.GridColumnStyles["Zusatz..."].Width=50;
myTableStyle.GridColumnStyles["ALLE"].Width=60;

In the debugger, could you please check if these column styles are
actually defined? You probably should add a new GridColumnStyle first.

Frans.
 
A

Anton Tupy

Frans said:
I get this message in "Spaltenbreite definieren" => Definition for
Columns Width

An unhandled exception of type 'System.NullReferenceException' occurred
in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object.

Please help me.
// Spalten erstellen

dt.Columns.Add("Modul", typeof(string)).ReadOnly = true;
dt.Columns.Add("Zusatz...", typeof(string)).ReadOnly = true;
dt.Columns.Add("ALLE", typeof(bool));
...

//Spaltenbreite definieren

myTableStyle.GridColumnStyles["Modul"].Width=20;
myTableStyle.GridColumnStyles["Zusatz..."].Width=50;
myTableStyle.GridColumnStyles["ALLE"].Width=60;


In the debugger, could you please check if these column styles are
actually defined? You probably should add a new GridColumnStyle first.

Frans.
Yes? that's right. I met with this error many time.
 
A

Anton Tupy

Frans said:
I get this message in "Spaltenbreite definieren" => Definition for
Columns Width

An unhandled exception of type 'System.NullReferenceException' occurred
in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object.

Please help me.
// Spalten erstellen

dt.Columns.Add("Modul", typeof(string)).ReadOnly = true;
dt.Columns.Add("Zusatz...", typeof(string)).ReadOnly = true;
dt.Columns.Add("ALLE", typeof(bool));
...

//Spaltenbreite definieren

myTableStyle.GridColumnStyles["Modul"].Width=20;
myTableStyle.GridColumnStyles["Zusatz..."].Width=50;
myTableStyle.GridColumnStyles["ALLE"].Width=60;


In the debugger, could you please check if these column styles are
actually defined? You probably should add a new GridColumnStyle first.

Frans.
Yes, that's right. I met with this error many time.
 
M

michael walser

Yes it works.

// Tablestyle erstellen

DataGridTableStyle myTableStyle = new DataGridTableStyle();

myTableStyle.MappingName = "DataTable_Berechtigung";

MyDataGrid.TableStyles.Clear();


// Spalten-Eigenschaften

DataGridTextBoxColumn Col_0 = new DataGridTextBoxColumn();

Col_0.MappingName="Modul";

Col_0.Width = 150;

Col_0.ReadOnly = true;

Col_0.HeaderText = "Module";

DataGridBoolColumn Col_1 = new DataGridBoolColumn();

Col_1.MappingName="ALLE";

Col_1.Width = 60;

Col_1.AllowNull = false;

Col_1.HeaderText = "ALLE";

Col_1.Alignment = System.Windows.Forms.HorizontalAlignment.Center;

DataGridBoolColumn Col_2 = new DataGridBoolColumn();



......etc etc..



Thanks for your help



Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top