在绑定DataGrid Compact Framework时修改值

时间:2014-02-19 11:15:02

标签: compact-framework

我使用BindingSource在我的窗口6.5应用程序中绑定DataGrid。但问题是我想在网格绑定时将列的文本从Int更改为字符串。

如果你看下面的代码,我将DataGrid与Designation绑定,其值为502,但我需要显示“SE”,我不能在我的Candidate对象中取值,所以我需要在运行时修改DataGrid。我不知道如何实现这一目标。请帮忙 。感谢

cand.Add(new Candidate { ID = 10, Name = "Andrew", Designation       =501 ,DCode="SSE" });
        cand.Add(new Candidate { ID = 11, Name = "Peter", Designation = 502 , DCode="SE"});
        DataGridTableStyle myDataGridTableStyle = new DataGridTableStyle();
        myDataGridTableStyle.MappingName = "Candidate";
        DataGridTextBoxColumn colA = new DataGridTextBoxColumn();
        colA.MappingName = "Name";
        colA.HeaderText = "Field A";
        colA.Width = 50;
        DataGridTextBoxColumn colB = new DataGridTextBoxColumn();
        colB.MappingName = "Designation";
        colB.HeaderText = "Position Holding";
        colB.Width = 100;
        //dataGrid1.CurrentCell.ColumnNumber.
        DataGridTextBoxColumn colC = new DataGridTextBoxColumn();
        colC.MappingName = "DCode";
        colC.HeaderText = "Code Position";
        colC.Width = 50;

        myDataGridTableStyle.GridColumnStyles.Add(colA);
        myDataGridTableStyle.GridColumnStyles.Add(colB);
        myDataGridTableStyle.GridColumnStyles.Add(colC);

        myBindingSource.DataSource = cand.ToBindingList();
        myDataGridTableStyle.MappingName = myBindingSource.GetListName(null);
        dataGrid1.TableStyles.Clear(); // Recommended on MSDN in the code examples.
        dataGrid1.TableStyles.Add(myDataGridTableStyle);
        dataGrid1.DataSource = myBindingSource;

1 个答案:

答案 0 :(得分:0)

为什么不将DCode属性作为您的Designation属性的从属,以便:

Canidate c = new Candidate();
c.Designation = 501;
print(c.DCode);//outputs "SSE"

然后,当您只想要对Designation进行字符串解释时,可以绑定到DCode。