views:

2025

answers:

1

I a beginner in Windows Mobile development and found that when I set the Width using a DataGridTextBoxColumn it does not affect the DataGrid column size. Here is my code:

        DataGridTableStyle tableStyle = new DataGridTableStyle();
        tableStyle.MappingName = "MainStyle";

        DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
        tbcName.Width = 120;
        tbcName.MappingName = "Name";
        tbcName.HeaderText = "Name";
        tableStyle.GridColumnStyles.Add(tbcName);

        DataGridTextBoxColumn tbcValue = new DataGridTextBoxColumn();
        tbcValue.Width = 200;
        tbcValue.MappingName = "Value";
        tbcValue.HeaderText = "Value";
        tableStyle.GridColumnStyles.Add(tbcValue);

        // dgUserAttributes is defined as a System.Windows.Forms.DataGrid
        dgUserAttributes.TableStyles.Clear();
        dgUserAttributes.TableStyles.Add(tableStyle);

Any ideas?

+1  A: 

As Henk Holterman stated you must map the DataGridTableStyle to the name of the DataTable bound to the the DataGrid. As an alternative, you can set the MappingName of each DataGridTextBoxColumn to the column name of the DataColumn in the bound DataTable.

Assuming you have added a DataGrid named dgUserAttributes to a Form named Form1 in the form designer, you can test how the DataGridTextBoxColumn width property changes the width of the column in the displayed DataGrid using the code below.

Also check that you have not set a value to dgUserAttributes TableStyles collection at design time that could be overriding the values you have set programatically.

    public Form1()
    {
        InitializeComponent();

        //Create In-Memory DataTable
        DataTable myTable = new DataTable();

        // Add DataColumns to the DataTable.
        DataColumn myNameColumn = new DataColumn("Name");
        myNameColumn.DataType = System.Type.GetType("System.String");
        myNameColumn.DefaultValue = "default string";
        myTable.Columns.Add(myNameColumn);
        DataColumn myValueColumn = new DataColumn("Value");
        myValueColumn.DataType = System.Type.GetType("System.String");
        myValueColumn.DefaultValue = "default string";
        myTable.Columns.Add(myValueColumn);

        //Bind DataTable to dataGrid1
        dgUserAttributes.DataSource = myTable;

        DataGridTableStyle tableStyle = new DataGridTableStyle();
        //tableStyle.MappingName = "MainStyle";
        DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
        tbcName.Width = 12; 
        tbcName.MappingName = "Name";
        tbcName.HeaderText = "Name";
        tableStyle.GridColumnStyles.Add(tbcName);
        DataGridTextBoxColumn tbcValue = new DataGridTextBoxColumn();
        tbcValue.Width = 300;
        tbcValue.MappingName = "Value";
        tbcValue.HeaderText = "Value";
        tableStyle.GridColumnStyles.Add(tbcValue);
        // dgUserAttributes is defined as a System.Windows.Forms.DataGrid
        dgUserAttributes.TableStyles.Clear();        
        dgUserAttributes.TableStyles.Add(tableStyle);           
    }
Michael
In your example if there were 10 columns would you have to add 10 columns or just the ones that you are concerned with?
runxc1 Bret Ferrier