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();