在mobile的开发中,使用DataGrid,和在web/winform开发中使用DataGrid相比,需要加额外的设置或代码,才能达到您想要的效果.
先初始化DataGrid的列表头样式,如下代码:
private void InitDataGridColumnHeader()
{
dataGrid1.TableStyles.Clear();
DataGridTableStyle dts = new DataGridTableStyle();
//注意:必须加上这一句,否则自定义列格式无法使用
dts.MappingName = "Table";
dataGrid1.TableStyles.Add(dts);
dataGrid1.TableStyles[0].GridColumnStyles.Clear();
//========================设置表头栏位===========================
//DataGridTableStyle dtsLog = new DataGridTableStyle();
DataGridTextBoxColumn colID = new DataGridTextBoxColumn();
colID.Width = 65;
colID.HeaderText = "商品編號";
colID.MappingName = "PROD_ID";
dataGrid1.TableStyles[0].GridColumnStyles.Add(colID);
DataGridTextBoxColumn colName = new DataGridTextBoxColumn();
colName.Width = 80;
colName.HeaderText = "商品名稱";
colName.MappingName = "PROD_CHN";
dataGrid1.TableStyles[0].GridColumnStyles.Add(colName);
DataGridTextBoxColumn colSL1 = new DataGridTextBoxColumn();
colSL1.Width = 70;
colSL1.HeaderText = "ONHAND";
colSL1.MappingName = "ONHAND";
dataGrid1.TableStyles[0].GridColumnStyles.Add(colSL1);
DataGridTextBoxColumn colSL2 = new DataGridTextBoxColumn();
colSL2.Width = 70;
colSL2.HeaderText = "COMMIT";
colSL2.MappingName = "COMMIT";
dataGrid1.TableStyles[0].GridColumnStyles.Add(colSL2);
DataGridTextBoxColumn colSL3 = new DataGridTextBoxColumn();
colSL3.Width = 70;
colSL3.HeaderText = "HCOMMIT";
colSL3.MappingName = "HCOMMIT";
dataGrid1.TableStyles[0].GridColumnStyles.Add(colSL3);
DataGridTextBoxColumn colSL4 = new DataGridTextBoxColumn();
colSL4.Width = 70;
colSL4.HeaderText = "SCOMMIT";
colSL4.MappingName = "SCOMMIT";
dataGrid1.TableStyles[0].GridColumnStyles.Add(colSL4);
DataGridTextBoxColumn colSL5 = new DataGridTextBoxColumn();
colSL5.Width = 70;
colSL5.HeaderText = "ONRECEIPT";
colSL5.MappingName = "ONRECEIPT";
dataGrid1.TableStyles[0].GridColumnStyles.Add(colSL5);
}
在这里需要注意的是,列的MappingName的属性设置中,去分大小写,比如您的Select语句选择出来的结果
集中,在sql server compact edition server辅助程序中显示的如果是大写,您在此绑定时写做了小写,
该列将不会出现在您的DataGrid中.
然后,您就可以把您要绑定的DataTable关联到该DataGrid上了,顺利显示数据.