簡體   English   中英

ASP VB.NET GridView:使用AutogenerateColumn = True通過列標題名稱更改列單元格文本

[英]ASP VB.NET GridView: Change Column cell text by column header name with AutogenerateColumn = True

拉出我的頭發,一些幫助會很棒。

我正在ASP中加載GridView,以動態填充SQL語句的結果。 該頁面用於常規查詢,因此標題名稱是從查詢結果中提取的,沒有模板。

我想做的是當標題被稱為ie。 SSN(敏感數據列)我想遍歷此特定列中的每個單元格並屏蔽該字段。 示例:“ ###-##-####”。 在頁面顯示之前,因此所有更改都將保留在每個頁面更改以及重新綁定GridView時。

我查看了一些事件,例如GridView1_RowCreated,GridView1_OnDataBound

但是,每當我搜索header.text時,它始終為空! 我可以對其進行更改和設置,但是在這些事件中從來沒有填充過它。 這使我相信我在錯誤的位置進行此更新。

即:

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    Dim header As String

    ' DataControlRowType.DataRow - also tried this with checking HeaderText too.
    If e.Row.RowType = DataControlRowType.Header Then

        For columnIndex As Integer = 0 To e.Row.Cells.Count - 1 Step 1
            header = e.Row.Cells(columnIndex).Text                
            Response.Write(header) ' Empty
            Response.Write("Cell") ' Will Display this for each header cell. 
            header = String.Empty         
        Next

    End If

End Sub

我需要在Page_Load或PreRender上執行此操作嗎? 任何想法/例子都很好。
謝謝你的幫助。

如果可以通過其標題文本確定特殊列,則可以找到列索引,然后在找到特定列時,在RowDataBound事件中更改單元格的文本。

這在我的測試中有效:

ASPX:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
      <div>
        <asp:GridView runat="server" ID="GridView1" />
      </div>
    </form>
</body>
</html>

代碼床:

Partial Public Class _Default
    Inherits Page

    Private _specialColumnName As String = "DEF"
    Private _specialColumnIndex As Integer = -1

    Private ReadOnly Property Data() As DataTable
        Get
            If Session("Default.Data") Is Nothing Then
                Dim value = New DataTable()

                Using connection = New SqlClient.SqlConnection("your_connection_string")
                    Using command = connection.CreateCommand()
                        command.CommandText = "SELECT * FROM your_table"

                        Using adapter = New SqlClient.SqlDataAdapter(command)
                            adapter.Fill(value)
                        End Using
                    End Using
                End Using

                'value.Columns.Add("ABC", GetType(String))
                'value.Columns.Add("DEF", GetType(Integer))
                'value.Columns.Add("GHI", GetType(Boolean))

                'value.Rows.Add("A", 1, True)
                'value.Rows.Add("B", 2, False)
                'value.Rows.Add("C", 3, False)

                Session("Default.Data") = value
            End If

            Return CType(Session("Default.Data"), DataTable)
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        GridView1.DataSource = Data
        GridView1.DataBind()
    End Sub

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then
            For index As Integer = 0 To e.Row.Cells.Count - 1
                If e.Row.Cells(index).Text = _specialColumnName Then
                    _specialColumnIndex = index
                    Return
                End If
            Next
        ElseIf _specialColumnIndex > -1 AndAlso e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(_specialColumnIndex).Text = "###"
        End If
    End Sub

End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM