Column width of a DataGrid in a Windows Mobile Application
Asked Answered
S

2

7

I'm having problems trying to adjust the width of a column of a datagrid. I used the answer posted here, but I can't solve it.

I'm using a List of objects as a datasource. In this simple example, I have just created a smart device application, and just added a datagrid. Then my code is this one:

    public Form1()
    {            
        InitializeComponent();

        List<Prueba> lista = new List<Prueba>();
        lista.Add(new Prueba("uno", "dos"));
        lista.Add(new Prueba("tres", "cuatro"));

        dataGrid1.DataSource = lista;
        DataGridTableStyle tableStyle = new DataGridTableStyle();
        tableStyle.MappingName = lista.GetType().ToString();
        DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
        tbcName.Width = 4000;
        tbcName.MappingName = "UNO";
        tbcName.HeaderText = "UNO";
        tableStyle.GridColumnStyles.Add(tbcName);
        dataGrid1.TableStyles.Clear();
        dataGrid1.TableStyles.Add(tableStyle);
    }
}

public class Prueba
{
    public string UNO { get; set; }
    public string DOS { get; set; }

    public Prueba(string uno, string dos)
    {
        this.UNO = uno;
        this.DOS = dos;
    }
}

The width remains the same. Do you have a clue? Thank you!

Spelunker answered 26/6, 2009 at 23:22 Comment(0)
H
7

Change this line

tableStyle.MappingName = lista.GetType().ToString();

to

tableStyle.MappingName = lista.GetType().Name;

Oh, and 4000 is a little big for a mobile but I assume that's a typo.

Higbee answered 27/6, 2009 at 10:24 Comment(1)
Ah thank you! Yes it works quite right. Yes, the 4000 was just a value based on desperation :PSpelunker
I
0

For anyone using a DataTable as the DataSource instead of a list, it appears you have to change:

tableStyle.MappingName = lista.GetType().Name;

to:

tableStyle.MappingName = lista.TableName;

Took me a while to figure this out!

Illicit answered 25/2, 2019 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.