冠逹小站

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

wince下的.net 控件 Datagrid,要实现自定义各列列宽,可以使用DataGridTableStyle,控件运行时表样式.

代码如下:

//获取数据

DataSet ds = Helper.DBHelper.Query(sql);
DataTable dt = ds.Tables[0];

//样式登场

DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = dt.TableName;//此处非常关键,数据表的名字不对,将无法映射成功
//定义列样式
DataGridColumnStyle LBarcodeColStyle =new DataGridTextBoxColumn();
LBarcodeColStyle.MappingName = "Barcode";
LBarcodeColStyle.HeaderText = "仓位";
LBarcodeColStyle.Width = 70;
ts.GridColumnStyles.Add(LBarcodeColStyle);
//定义列样式
DataGridColumnStyle UNameColStyle = new DataGridTextBoxColumn();
UNameColStyle.MappingName = "UserName";
UNameColStyle.HeaderText = "锁定人";
UNameColStyle.Width = 60;
ts.GridColumnStyles.Add(UNameColStyle);
//定义列样式
DataGridColumnStyle LockTimeColStyle = new DataGridTextBoxColumn();
LockTimeColStyle.MappingName = "CreateTime";
LockTimeColStyle.HeaderText = "锁定时间";
LockTimeColStyle.Width = 102;
ts.GridColumnStyles.Add(LockTimeColStyle);
//将样式和控件绑定到一起
this.dataGrid1.TableStyles.Add(ts);
this.dataGrid1.DataSource = dt;

通过以上,就能实现wince下对 Datagrid的数据列宽度等属性的设置了.

另外,在wince下, Datagrid好像没办法设置SelectionMode ,自然也没有FullRowSelect 选项了.

变通一下,使用如下代码实现:

//对 Datagrid获得焦点和当前cell事件,触发执行选中整行的代码,变通实现了整行选中效果

private void dataGrid1_GotFocus(object sender, EventArgs e)
{
int index = ((DataGrid)sender).CurrentCell.RowNumber;
((DataGrid)sender).Select(index);
}
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
int index = ((DataGrid)sender).CurrentCell.RowNumber;
((DataGrid)sender).Select(index);
}
posted on 2011-06-22 17:46  林冠逹  阅读(2499)  评论(0编辑  收藏  举报