C# Windows Mobile 应用程序中 DataGrid 的列宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1051690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:04:12  来源:igfitidea点击:

Column width of a DataGrid in a Windows Mobile Application

c#windows-mobiledatagridwidth

提问by

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!

宽度保持不变。你有什么线索吗?谢谢!

采纳答案by Henk Holterman

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.

哦,4000 对于手机来说有点大,但我认为这是一个错字。

回答by B.A.

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

对于使用 DataTable 作为数据源而不是列表的任何人,您似乎必须更改:

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

to:

到:

tableStyle.MappingName = lista.TableName;

Took me a while to figure this out!

我花了一段时间才弄清楚这一点!