Column width of a DataGrid in a Windows Mobile Application

12,138

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.

Share:
12,138
Admin
Author by

Admin

Updated on July 06, 2022

Comments

  • Admin
    Admin almost 2 years

    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!

  • Admin
    Admin almost 15 years
    Ah thank you! Yes it works quite right. Yes, the 4000 was just a value based on desperation :P