private void SelectGoodsForm_Load(object sender, EventArgs e)
{//定义 DataGrid样式
DataGridTableStyle dataGridStyle = null;
dataGridStyle = new DataGridTableStyle();this.goodsGrid.DataSource = createDataTable();
//清楚之前的DataGrid样式
this.goodsGrid.TableStyles.Clear();
//添加新样式
this.goodsGrid.TableStyles.Add(dataGridStyle);//定义DataGrid列样式
GridColumnStylesCollection dataGridColStyle = this.goodsGrid.TableStyles[0].GridColumnStyles;
//设置DataGrid列宽度
dataGridColStyle[0].Width = 110;
dataGridColStyle[1].Width = 60;
dataGridColStyle[2].Width = 40;
dataGridColStyle[3].Width = 68;
dataGridColStyle[4].Width = 0;
dataGridColStyle[5].Width = 0;
dataGridColStyle[6].Width = 0;
//设置列头显示的文本
dataGridColStyle[0].HeaderText = "物资名称";
dataGridColStyle[1].HeaderText = "型号";
dataGridColStyle[2].HeaderText = "单位";
dataGridColStyle[3].HeaderText = "物资类型";
dataGridColStyle[4].HeaderText = "物资ID";
dataGridColStyle[5].HeaderText = "物资编号";
dataGridColStyle[6].HeaderText = "物资类型ID";}
//为DataGrid提供数据的DataTable
private DataTable createDataTable() {DataTable dt = new DataTable();
DataColumn col = new DataColumn();
col.Caption = "物资名称";
col.ColumnName = "GoodsName";
dt.Columns.Add(col);col = new DataColumn();
col.Caption = "型号";
col.ColumnName = "GoodsType";
dt.Columns.Add(col);col = new DataColumn();
col.Caption = "单位";
col.ColumnName = "OutCode";
dt.Columns.Add(col);col = new DataColumn();
col.Caption = "物资类型";
col.ColumnName = "ItemTypeName";
dt.Columns.Add(col);col = new DataColumn();
col.Caption = "物资ID";
col.ColumnName = "GoodsId";
dt.Columns.Add(col);col = new DataColumn();
col.Caption = "物资编号";
col.ColumnName = "GoodsCode";
dt.Columns.Add(col);col = new DataColumn();
col.Caption = "物资类型ID";
col.ColumnName = "ItemType";
dt.Columns.Add(col);return dt;
}
//获取DataGrid中当前选中行的第一列中的值
this.goodsGrid[this.goodsGrid.CurrentRowIndex, 0].ToString();
进行手持设备wince开发中DataGrid的样式设置(宽度),布布扣,bubuko.com