views:

1011

answers:

2

I'm using VS 2005 to create a Windows Mobile program in C#. I need to display data in a grid. The only grid control I could find for Windows Mobile is DataGrid, so I placed one on my form. I now need to change the width of some columns and the font & color of some cells. How do I do this?

Also is there is a better control to use for Windows Mobile?

thanks John.

A: 

I am not sure you can change the font for individual columns or cells. The grid has a property that lets you set the font and size. To set the width of columns, I use this method (it adds a table style to the grid):

private void SetColumnWidth(int columnID, int width)
{
    // add table style if first call
    if (this.dataGrid1.TableStyles.Count == 0)
    {
        // Set the DataGridTableStyle.MappingName property
        // to the table in the data source to map to.
        dataGridColumnTableStyle.MappingName = "<name of your table in the DS here>";

        // Add it to the datagrid's TableStyles collection
        this.dataGrid1.TableStyles.Add(dataGridColumnTableStyle);
    }

    // set width
    this.dataGrid1.TableStyles[0].GridColumnStyles[columnID].Width = width;
}

This method is also helpful when you want to hide a column that is in the bound DataTable, but you don't want to show (then you set width = 0).

cdonner
I added the function you posted however I got the error The name 'dataGridColumnTableStyle' does not exist in the current context
Rossini
Sorry, my mistake. You need to create a DatagridTableStyle instance for your form. You can do that at design time or in the code. See http://msdn.microsoft.com/en-us/library/aa984371(VS.71).aspx
cdonner
A: 

You have to do custom painting. The CF team blogged about how to do it here.

ctacke