Quantcast
Channel: c# – Jalena Blog
Viewing all articles
Browse latest Browse all 9

WinCe 6.0 DataGrid 绑定List数据源

$
0
0

最近在项目中,使用了老的掉牙的Windows CE作为开发基础。在实际项目使用中,我从后端通过RestFul接口获取数据,然后将数据通过JSON转为了实体类,然后直接绑定到了DataGird中。

问题接着就来了,在DataGrid中使用List<T>作为数据源的时候,我要自定义DataGridTableStyle的时候,需要设置MappingName,但各种资料翻完也没有找到如何设置这个值的信息。

通过Debug,原来设置List为数据源需要将MappingName设置为 List`1

经过反复测试,发现直接设置为 typeof(List).Name即可

参考代码

// 实体对应类
public class MTPDAOnTheShelfTaskQueryResp
{
    public ControlInfo ControlInfo { get; set; }
    public List<ITEM> ITEM { get; set; }
    public RETURN RETURN { get; set; }
}
// 初始化界面设定
private void StockInQuery_Load(object sender, EventArgs e)
{
    dateTimePickerStart.Value = DateTime.Now;
    dateTimePickerEnd.Value = DateTime.Now;

    // 设定 Style
    ColumnStyle[] columnStyles = new ColumnStyle[]{
                new ColumnStyle("MBLNR","任务单号",100),
                new ColumnStyle("MAKTX", "日期", 70),
                new ColumnStyle("MATNR", "创建人", 100)
            };
    CommonHandles.SetDataGridStyles(dataGrid, columnStyles, "List`1");
}

/// <summary>
/// 设置DataGird列样式
/// </summary>
/// <param name="dataGrid"></param>
/// <param name="styles"></param>
/// <param name="mappingName"></param>
public static void SetDataGridStyles(DataGrid dataGrid, ColumnStyle[] styles, string mappingName)
{
    dataGrid.TableStyles.Clear();

    DataGridTableStyle tableStyle = new DataGridTableStyle();
    tableStyle.MappingName = mappingName;
    foreach (ColumnStyle style in styles)
    {
        DataGridColumnStyle col = new DataGridTextBoxColumn();
        col.MappingName = style.MappingName;
        col.HeaderText = style.DisplayName;
        col.Width = style.Width;
        tableStyle.GridColumnStyles.Add(col);
    }

    dataGrid.TableStyles.Add(tableStyle);

    // 设置行头不可见
    dataGrid.RowHeadersVisible = false;
    //dataGrid.ColumnHeadersVisible = false;
}

// 数据绑定
if (!string.IsNullOrEmpty(result))
{
    StockInQueryRespnose respnose = JsonConvert.DeserializeObject<StockInQueryRespnose>(result);
    if (respnose.MT_PDA_On_The_Shelf_Task_Query_Resp.RETURN.MSGTYPE.Equals("E"))
    {
        MessageBox.Show(respnose.MT_PDA_On_The_Shelf_Task_Query_Resp.RETURN.MSGTXT, "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        return;
    }
    else if (respnose.MT_PDA_On_The_Shelf_Task_Query_Resp.RETURN.MSGTYPE.Equals("S"))
    {
        dataGrid.DataSource = respnose.MT_PDA_On_The_Shelf_Task_Query_Resp.ITEM;
    }
}

Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles





Latest Images