Column width of a DataGrid in a Windows Mobile Application

时光毁灭记忆、已成空白 提交于 2019-12-01 02:08:58

问题


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!


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/1051690/column-width-of-a-datagrid-in-a-windows-mobile-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!