隐藏gridView中的列

时间:2011-06-20 19:17:36

标签: asp.net vb.net

我有一个数据集返回以下数据CategoryDI,CategoryName,CateoryPicture我使用gridview显示这些数据但是我想只显示CategoryName隐藏categoryid,而CategoryPicture我使用以下代码但它不起作用请帮助我谢谢< / p>

GridView1.DataSource = getDataSet.Tables(0)

    GridView1.DataBind()

    GridView1.Columns(0).Visible = False
    GridView1.Columns(1).Visible = true
    GridView1.Columns(2).Visible = False

6 个答案:

答案 0 :(得分:3)

 protected void gvDocuments_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].Visible = false;
        }

答案 1 :(得分:3)

  Me.GridView1.DataSource = ds6
    Me.GridView1.DataBind()
    If GridView1.Columns.Count > 0 Then
        Me.GridView1.Columns(0).Visible = False
        Me.GridView1.Columns(9).Visible = False
        Me.GridView1.Columns(2).Visible = False
        Me.GridView1.Columns(3).Visible = False 
    Else
        GridView1.HeaderRow.Cells(0).Visible = False
        GridView1.HeaderRow.Cells(9).Visible = False
        GridView1.HeaderRow.Cells(2).Visible = False
        GridView1.HeaderRow.Cells(3).Visible = False
        For Each gvr In GridView1.Rows
            gvr.Cells(0).Visible = False
            gvr.Cells(9).Visible = False
            gvr.Cells(2).Visible = False
            gvr.Cells(3).Visible = False
        Next
    End If

答案 2 :(得分:1)

在您的aspx页面中,确保使用“AutogenerateColumns = False”定义gridview,然后指定要查看的列。

例如:

 <asp:GridView ID="mygrid" runat="server" AutoGenerateColumns="False" CellPadding="4"
 GridLines="Horizontal" Width="800px" ShowFooter="False" CssClass="grid"  AlternatingRowStyle-CssClass="alt">
    <Columns>
  <asp:BoundField HeaderText="Name" DataField="CategoryName"></asp:BoundField>
  <asp:BoundField HeaderText="Picture" DataField="CategoryPciture"></asp:BoundField>
    </Columns>
  <asp:GridView>

这将只显示您想要查看的列。

在你的codebebind中,只需做一个

 mygrid.Datasource= xxx

后跟

 mygrid.databind 

这将成功。

答案 3 :(得分:1)

.DisplayNone
{
 Display:none   
}

然后在结束之前放置以下内容&#39; BoundField&#39;您想要隐藏的每个列的标记:

<HeaderStyle CssClass="DisplayNone"></HeaderStyle>
<ItemStyle CssClass="DisplayNone"></ItemStyle>

就像你指定了&#39; Visible = False&#34;但是你的代码可以访问它。但请记住,如果您查看来源&#39;如果隐藏数据是敏感的,请不要使用此技术。

答案 4 :(得分:0)

一个简单的代码示例

    Me.DataGridView1.Columns("CategoryID").Visible = False
    Me.DataGridView1.Columns("CategoryName").Visible = False
    Me.DataGridView1.Columns("CateoryPicture").Visible = False

问候。

答案 5 :(得分:0)

但它给了我以下

Conversion from string "Branch" to type 'Integer' is not valid.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    setMenu()
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)
    Response.Cache.SetNoStore()
    lbldte.Text = DateAndTime.Now.ToShortDateString
    If Page.PreviousPage IsNot Nothing Then
        Dim GridView1 As GridView = DirectCast(Page.PreviousPage.FindControl("GridView1"), GridView)

    End If
    If Session("ProductsTable") IsNot Nothing Then
        dt = DirectCast(Session("ProductsTable"), DataTable)

        GridView1.DataSource = dt
        GridView1.DataBind()
        If GridView1.Columns.Count > 0 Then
            Me.GridView1.Columns("Branch").Visible = False
            Me.GridView1.Columns("Visit date").Visible = False
            Me.GridView1.Columns("Emp.Age").Visible = False
        End If
    End If

    If Not IsPostBack Then
        'check if the webpage is loaded for the first time.
        'Saves the Previous page url in ViewState
        ViewState("PreviousPage") = Request.UrlReferrer
    End If
End Sub