简体   繁体   中英

Format GridView Auto Generated Columns

I am trying to format the width of my gridview columns dynamically for easy of use in editing and updating. Is it possible to have multiple column widths defined? Here is the code I am using to create the 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();
        }

    }

Is it possible to make a statement like:

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

First you must change AutoSizeColumnsMode set to fill then you can change the width of the column.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM