int[] a_nCellWidth = new int[4] { 300, 200, 200, 200 };//前4列宽度,可自定义
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Label lb = new Label();
for (int nCell = 0; nCell < a_nCellWidth.Length; nCell++)
{
//设置居中、margin、padding等格式
GridView1.Rows[i].Cells[nCell].Style.Add(“text-align”, “center”);
GridView1.Rows[i].Cells[nCell].Style.Add(“margin”, “2px”);
GridView1.Rows[i].Cells[nCell].Style.Add(“padding”, “2px”);
//设置宽度,自动换行
string cType = GridView1.Rows[i].Cells[nCell].Controls[1].GetType().ToString().ToLower();
if (cType.IndexOf(“.label”) >= 0)
{
string cCellText = GridView1.Rows[i].Cells[nCell].Controls[1].ID.ToString();
try { lb = (Label)GridView1.Rows[i].FindControl(cCellText); }
catch { }
if (lb != null)
{
lb.Width = a_nCellWidth[nCell];
GridView1.Rows[i].Cells[nCell].Wrap = true;
}
}
}
}