设置网格视图自动生成的列的格式

本文关键字:格式 自动生成 网格 视图 设置 | 更新日期: 2023-09-27 18:35:35

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

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

是否可以发表这样的声明:

taskTable.Column.Add("Id", typeof(int), width="200px");???

设置网格视图自动生成的列的格式

首先,您必须更改设置为填充的自动大小列模式,然后才能更改列的宽度。

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