DataGrid Width

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

PLEASE help me!
What's wrong in my code, that the width of my column can
not be changed (it's an application for a Pocket PC)

I hope that you can see very fast The error in my thinking

thanks



// Create new DataTable and DataSource objects.
DataTable myDataTable = new DataTable();
// Declare DataColumn and DataRow variables.
DataColumn myColumn, myColumn2;
DataRow myRow;
DataView myDataView;
// Create new DataColumn, set DataType, ColumnName and
add to DataTable.
myColumn = new DataColumn();
myColumn.MaxLength =2;
myColumn.DataType = System.Type.GetType("System.Int32");
myColumn.ColumnName = "id";
myDataTable.Columns.Add(myColumn);

// Create second column.
myColumn2 = new DataColumn();
myColumn2.DataType = Type.GetType("System.String");
myColumn2.ColumnName = "item";
myDataTable.Columns.Add(myColumn2);

// Create new DataRow objects and add to DataTable.
for(int i = 0; i < 10; i++)
{
myRow = myDataTable.NewRow();
myRow["id"] = i;
myRow["item"] = i.ToString();
myDataTable.Rows.Add(myRow);
}

// Formatierung DataGrid
dataGrid1.BackColor = System.Drawing.Color.Blue;
DataGridTableStyle MyGridStyle = new DataGridTableStyle();
MyGridStyle.MappingName = "Days";
// Add a GridColumnStyle and set the MappingName
// to the name of a DataColumn in the DataTable.
// Set the HeaderText and Width properties.
DataGridColumnStyle MyColumnStyle = new
DataGridTextBoxColumn();
MyColumnStyle.MappingName = "id";
MyColumnStyle.HeaderText = "First Name";
MyColumnStyle.Width = 10;
MyGridStyle.GridColumnStyles.Add(MyColumnStyle);
// Add the DataGridTableStyle instance to
// the GridTableStylesCollection.
dataGrid1.TableStyles.Add(MyGridStyle);


// Create a DataView using the DataTable.
myDataView = new DataView(myDataTable);
// Set a DataGrid control's DataSource to the DataView.
dataGrid1.DataSource = myDataView;
 
Your data mappings need to match the source data exactly. For example your
custom grid style is mapped to "Days" however you have not used this name
with your DataTable object. Using
DataTable myDataTable = new DataTable("Days");
Should correct this.

Peter
 
Thank you Peter,
you have right. This right mapping coreccts my problem

THANK YOU !
Michael


-----Original Message-----
Your data mappings need to match the source data exactly. For example your
custom grid style is mapped to "Days" however you have not used this name
with your DataTable object. Using
DataTable myDataTable = new DataTable("Days");
Should correct this.

Peter

--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org

PLEASE help me!
What's wrong in my code, that the width of my column can
not be changed (it's an application for a Pocket PC)

I hope that you can see very fast The error in my thinking

thanks



// Create new DataTable and DataSource objects.
DataTable myDataTable = new DataTable();
// Declare DataColumn and DataRow variables.
DataColumn myColumn, myColumn2;
DataRow myRow;
DataView myDataView;
// Create new DataColumn, set DataType, ColumnName and
add to DataTable.
myColumn = new DataColumn();
myColumn.MaxLength =2;
myColumn.DataType = System.Type.GetType ("System.Int32");
myColumn.ColumnName = "id";
myDataTable.Columns.Add(myColumn);

// Create second column.
myColumn2 = new DataColumn();
myColumn2.DataType = Type.GetType("System.String");
myColumn2.ColumnName = "item";
myDataTable.Columns.Add(myColumn2);

// Create new DataRow objects and add to DataTable.
for(int i = 0; i < 10; i++)
{
myRow = myDataTable.NewRow();
myRow["id"] = i;
myRow["item"] = i.ToString();
myDataTable.Rows.Add(myRow);
}

// Formatierung DataGrid
dataGrid1.BackColor = System.Drawing.Color.Blue;
DataGridTableStyle MyGridStyle = new DataGridTableStyle ();
MyGridStyle.MappingName = "Days";
// Add a GridColumnStyle and set the MappingName
// to the name of a DataColumn in the DataTable.
// Set the HeaderText and Width properties.
DataGridColumnStyle MyColumnStyle = new
DataGridTextBoxColumn();
MyColumnStyle.MappingName = "id";
MyColumnStyle.HeaderText = "First Name";
MyColumnStyle.Width = 10;
MyGridStyle.GridColumnStyles.Add(MyColumnStyle);
// Add the DataGridTableStyle instance to
// the GridTableStylesCollection.
dataGrid1.TableStyles.Add(MyGridStyle);


// Create a DataView using the DataTable.
myDataView = new DataView(myDataTable);
// Set a DataGrid control's DataSource to the DataView.
dataGrid1.DataSource = myDataView;


.
 
Back
Top