前往
大廳
主題

[ASP.NET C#] GridView 固定寬度

阿宅 | 2021-01-18 16:01:26 | 巴幣 0 | 人氣 522

設定 GridView 事件 RowDataBound
DataControlRowType.Header 代表標頭的設置,Wrap代表不換行
DataControlRowType.DataRow 代表資料的設置
下面是將標頭固定大小並且不為內容影響(內容會自動換行,若內容也不換行也可以設Wrap屬性,但若內容太長則會影響寬度)

protected void GridView01_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                        e.Row.Cells[i].Width = 100;
                        e.Row.Cells[i].Wrap = false;
                }

            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                        e.Row.Cells[i].Width = 100;
                }
            }

創作回應

更多創作