Asp.net 設置GridView自適應列寬不變形

動態綁定的GridView因爲列數不固定,而列又太多,這樣設置GridView固定寬度就不能知足需求了。爲此整理了兩種方法來達到GridView自適應列寬不變形的效果。html

    //在GridView的行數據綁定完的事件中設置
    protected void gvObjectList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            //保持列不變形
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {   
                //方法一:
                e.Row.Cells[i].Text = "&nbsp;" + e.Row.Cells[i].Text + "&nbsp;";
                e.Row.Cells[i].Wrap = false;
                //方法二:
                //e.Row.Cells[i].Text = "<nobr>&nbsp;" + e.Row.Cells[i].Text + "&nbsp;</nobr>";            
            }        
        }    
     }

代碼說明:
方法一,設置cell的自動換行屬性爲false。
方法二,用html標記的方式實現不換行; 就是一個空格,可讓網格線和裏面的內容留有必定的距離保持美觀。spa

有興趣的朋友,動手下吧,看看效果如何哦。code

相關文章
相關標籤/搜索