gridview动态设定列宽问题,高分求教.
以下程序试图动态生成gridview列,并设定每列的列宽.
protected readonly string[] TaskGridView1_ColNames = {"任务编号","任务名称","巡视时间","用户组" };
protected readonly int[] TaskGridView1_ColWidths = {100,250,100,100};
protected void TaskGridView1_ShowHeader()
{
DataTable dt = new DataTable();
DataColumn dc = null;
for (int i = 0; i < TaskGridView1_ColNames.Length; i++)
{
dc = new DataColumn(TaskGridView1_ColNames[i], typeof(string));
dt.Columns.Add(dc);
}
dt.Rows.Add(dt.NewRow());
TaskGridView1.DataSource = dt;
TaskGridView1.DataBind();
TaskGridView1.Rows[0].Visible = false;
for (int i = 0; i < TaskGridView1.Columns.Count; i++)
{
TaskGridView1.Columns[i].HeaderStyle.Width = TaskGridView1_ColWidths[i];
}
}
在对列宽进行调整的代码for (int i = 0; i < TaskGridView1.Columns.Count; i++){...}里,TaskGridView1.Columns.Count为0,所以无法进入循环对列宽进行赋值,但在IE中,能正确显示各列的名称.
请问以上代码错在何处,或提供一个能动态生成GridView列,且设置列宽的其它方法,谢谢.
[解决办法]
3楼
这样写,才是取头的列数
TaskGridView1.HeaderRow.Cells.Count.ToString()