CodeBehind方式設定GridView欄寬、置中

使用GridView中內建的編輯功能預設是沒有設定欄寬的,因此會造成編輯時的TextBox將欄位撐開的問題:
2010-02-25_205559


在GridView的RowDataBound事件中以CodeBehind設定欄位寬度與文字置中:

Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound
        '設定欄位寬度
  GridView.Columns(1).ItemStyle.Width = 80
        GridView.Columns(1).ControlStyle.Width = 80
 
        '設定文字置中
   GridView.Columns(1).ItemStyle.HorizontalAlign = HorizontalAlign.Center
    End Sub

ItemStyle.Width:普通模式欄位寬度
ControlStyle.Width :編輯模式欄位寬度

設定後結果:
2010-02-25_205627 

補充:
以For迴圈方式設定GridView中所有欄位寬度



Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound
        Dim i As Integer
        For i As Integer = 0 To GridView.Columns.Count - 1
            GridView.Columns(i).ItemStyle.Width = 80
        Next
End Sub

0 意見:

張貼留言