C#中DataGridView的標題和寬度設置
private void SetDataGridStylesFromNet(string strFlag, DataTable dtableTemp, DataGridView dataGridView)
{
if (strFlag == "老化類")
{//商品信息
int[] iArrayWidth = {180,100,200,80,60,
150,150,150,150
};
string[] strArrayText = { "條形碼","產品代碼","產品名稱","規格型號","單位"
,"開始時間","結束時間","總耗時間","操作人"
};
string[] strArrayZiduanMapping = { "FMainBarCode","FNumber","ItemName","FModel","FUnitName"
,"FStartTime","FEndTime","IntervalTime","UserName"
};
//將DataTable綁定到DataView中
dataGridView.Columns.Clear();
dataGridView.AutoGenerateColumns = false;
//綁定DataView到dataGridView上
dataGridView.DataSource = dtableTemp;
for (int i = 0; i < iArrayWidth.Length; i++)
{
DataGridViewTextBoxColumn dataGridViewClolumnTemp = new DataGridViewTextBoxColumn();
dataGridViewClolumnTemp.DataPropertyName = strArrayZiduanMapping[i];
dataGridViewClolumnTemp.HeaderText = strArrayText[i];
dataGridViewClolumnTemp.Width = iArrayWidth[i];
dataGridView.Columns.Add(dataGridViewClolumnTemp);//最后一定要添加進去
}
return;
}
}