C# 格式化GridView自动生成的列

C# 格式化GridView自动生成的列,c#,vb.net,gridview,formatting,C#,Vb.net,Gridview,Formatting,我正在尝试动态格式化gridview列的宽度,以便于编辑和更新。是否可以定义多个列宽?下面是我用来创建gridview的代码 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { // Create a new table. DataTable taskTable = new DataTable("Tas

我正在尝试动态格式化gridview列的宽度,以便于编辑和更新。是否可以定义多个列宽?下面是我用来创建gridview的代码

protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            // Create a new table.
            DataTable taskTable = new DataTable("TaskList");

            // Create the columns.
            taskTable.Columns.Add("Id", typeof(int));
            taskTable.Columns.Add("Description", typeof(string));
            taskTable.Columns.Add("IsComplete", typeof(bool));

            //Add data to the new table. - could fill the table from database query if desired
            for (int i = 0; i < 1; i++)
            {
                DataRow tableRow = taskTable.NewRow();
                //tableRow["Id"] = 0;
                tableRow["Description"] = "";
                tableRow["IsComplete"] = false;
                taskTable.Rows.Add(tableRow);
            }


            //Persist the table in the Session object.
            Session["TaskTable"] = taskTable;

            //Bind data to the GridView control.
            BindData();
        }

    }

首先必须将AutoSizeColumnsMode集更改为fill,然后才能更改列的宽度

dataGridView1.Columns[columnName].AutoSizeMode= DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns[columnName].Width = columnWidth;

你只是想改变列宽吗?是的,其他一切都是在gridview的html代码中完成的,我只需要知道如何为每个单独的列设置列宽。。。
dataGridView1.Columns[columnName].AutoSizeMode= DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns[columnName].Width = columnWidth;