2014年6月26日 星期四

ASP.NET GridView 設定應用篇


20170904
1. GridView設定表格table、欄位td取消框線。
(1)GridView屬性BorderStyle設定為none
(2)GridView如果是自訂的TemplateField要取消欄位框線,可以設定ItemStyle的BorderStyle屬性為none

2. GridView套用bootstrap樣式。
    Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then
            e.Row.CssClass = "bg-primary"
            e.Row.Cells(0).CssClass = "col-xs-6 col-md-6"
            e.Row.Cells(1).CssClass = "col-xs-1 col-md-1"
            e.Row.Cells(2).CssClass = "col-xs-1 col-md-1"
            e.Row.Cells(3).CssClass = "col-xs-2 col-md-2"
            e.Row.Cells(4).CssClass = "col-xs-2 col-md-2"
        End If
    End Sub

20170619
1. GridView進入編輯模式時,設定欄位寬度。
Private Sub GridView3_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView3.RowDataBound
    If e.Row.RowState = (DataControlRowState.Edit Or DataControlRowState.Alternate) OrElse e.Row.RowState = DataControlRowState.Edit Then
        '按下編輯模式時,設定寬度
        e.Row.Cells(1).Style.Value = "width: 20%;"
    End If
    If e.Row.RowType = DataControlRowType.Header Then
        e.Row.CssClass = "bg-primary"
        e.Row.Cells(0).Style.Value = "width: 2%;"
        e.Row.Cells(1).Style.Value = "width: 10%;"
        e.Row.Cells(2).Style.Value = "width: 10%;"
    End If
End Sub



2. 設定GridView當沒有資料時,將框線設定為 0,也就是不顯示框線。
參考資料:emptydatarowstyle is not working

    Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.EmptyDataRow Then
            Dim bookingGridView As GridView = CType(sender, GridView)
            bookingGridView.Attributes.Add("border", "0") '框線為0
        End If
    End Sub


3.設定GridView上下顯示分頁功能、分頁對齊、分頁模式
2-1.如要在GridView的上下方同時顯示分頁功能,可設定PagerSettings.Position屬性,如下:
GridView1.PagerSettings.Position = PagerPosition.TopAndBottom
2-2.分頁對齊
GridView1.PagerStyle.HorizontalAlign = HorizontalAlign.Center
2-3.取得或設定在支援分頁的控制項中顯示頁面巡覽區控制項時所用的模式
.PagerSettings.Mode = PagerButtons.NumericFirstLast '上一頁、下一頁等多種分頁模式


4.當按下GridView中的編輯按鈕,如何在RowDataBound中攔截到使用者是編輯哪一列
    Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.RowIndex = GridView1.EditIndex Then
                '在此處可以設定編輯模式中的TextBox1等控制項
            End If
        End If
    End Sub

沒有留言:

張貼留言