0

我正在使用 VB .NET,我有一个 GridView,它有 4 列(删除、下载、命名和 PK)。“名称”和“ID”列是我的 Sql 命令的结果,我想向用户隐藏“PK”列。但我做不到。我已经尝试了一些代码,但没有。

这是我的代码:

  Public Sub obterTabela()
        Dim db As DbConnect = New DbConnect()
        Dim conn = New SqlConnection(db.geraConnString)
        Dim cmd As New SqlCommand()
        Dim aux As String = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.Query)
        aux = aux.Replace("?id=", "")
        Dim conjunto As Integer = Convert.ToInt32(aux)

        conn.Open()
        'alterar
        cmd = New SqlCommand("SELECT nome AS 'Nome', pk_table AS 'PK' FROM t_kml WHERE fk_table = " & aux, conn)

        Dim dt As DataTable = New DataTable()
        Dim sdr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        dt.Load(sdr)
        'Dim indice As Integer = (Integer).Parse(dt.Columns("PK"))

        'dt.Columns.Item("PK").ColumnMapping = MappingType.Hidden

        sdr.Close()

        gdv_kml.DataSource = dt
        'gdv_kml.Columns(2).Visible = False
        gdv_kml.DataBind()
    End Sub

<asp:GridView ID="gdv_kml" runat="server" HorizontalAlign="Center" CellPadding="4" 
            ForeColor="#333333" GridLines="None">
            <RowStyle BackColor="#E3EAEB" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:ButtonField CommandName="Download" Text="Download" />
            </Columns>
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <EmptyDataTemplate>
                <b>Nenhum KML incluso.</b>
            </EmptyDataTemplate>
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
     </asp:GridView>

谢谢你

4

3 回答 3

0

有没有办法可以从 SQL 查询中排除 PK?这样你只提取三列,没有PK

于 2013-04-15T13:37:23.573 回答
0

关闭自动生成字段并列出您希望在该部分中显示的字段。

于 2013-04-15T18:02:49.247 回答
0

首先,将AutoGenerateColumns属性设置为False然后:

Private Sub GridView_RowDataBound(ByVal sender As System.Object,ByVal e As System.GridViewRowEventArgs )
{
  e.Row.Cells("PK").Visible = False
}
于 2013-04-15T18:09:06.087 回答