如何隐藏随机生成的GridView行



hi编码器我有一个gridview,其中数据来自数据库。.现在所有行的ID都是我想要它仅显示我的gridview的第一行,并隐藏其余的其余部分但没有它们的价值,我只是想隐藏它们。我想要他们的价值,因为我的灯箱上有一个。

这是我的代码:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="False" PageSize="1" PersistedSelection="true" 
    DatakeyNames="pptId,Priority">
        <Columns>
            <asp:TemplateField HeaderText="pptId" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblpptId" runat="server" Text='<%# Bind("pptId") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
          <asp:TemplateField HeaderText="Priority" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblPriority" runat="server" Text='<%# Bind("Priority") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="View PPT">
                <ItemTemplate>
                    <a id="imageLink" href='<%# Eval("Imageurl") %>' title='<%#Eval("Description") %>'
                        rel="lightbox[Brussels]" runat="server">Start PPT</a>
                </ItemTemplate>
            </asp:TemplateField>                
        </Columns>
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
         </asp:GridView>

编辑::

protected void Page_Load(object sender, EventArgs e)
{
    BindModule();
}
protected void BindModule()
{
    try
    {
        con.Open();
        string id = Request.QueryString["pptId"].ToString();
        //Query to get Imagesurl and Description from database
        SqlCommand command = new SqlCommand("select * from Image_Master where pptId='" + id + "' and IsEnable='True' order by Priority", con);
        dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(command);
        da.Fill(dt);          
        GridView1.DataSource = dt;
        GridView1.DataBind();                      
        GridView1.Dispose();
    }
    catch (Exception ex)
    {
        Response.Write("Error occured : " + ex.Message.ToString());
    }
    finally
    {
        con.Close();
    }
}

在上面的代码中,每行都相同,我的图像显示我的gridView我想要的是仅显示第一个图像并隐藏其余的其余值,而不是它们的值。

我该怎么做.....谢谢

您不能只用CSS做到这一点吗?

table tr:nth-child(n + 3) {
    display: none;
}

在这里对nth-Child CSS-Selector有一个解释:

http://css-tricks.com/how-nth-child-works/

我也在这里的另一个答案中解释了它:

https://stackoverflow.com/a/21166162/1256868

您可能需要在生成表上的类或静态ID

编辑以下评论:

尝试将CSS类添加到GridView并在上面添加CSS:

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        table.Grid tr:nth-child(n + 3) {
            display: none;
        }
    </style>
</head>
<body>
    <form runat="server">
    <asp:GridView runat="server" CssClass="Grid">
        <%--Your templates here--%>
    </asp:GridView>
    </form>
</body>
</html>

ASP.NET将从您的GridView生成HTML表,这就是为什么您需要将样式应用于表。浏览器从未看到任何ASP.NET代码。该代码只是生成浏览器知道如何显示的HTML的一种方式。

编辑更好的IE支持:

我想您要问的是IE8(可能是7)的支持,如IE9及以后的支持。为了支持IE7及向上,请更改您的CSS以使用 选择器,该 选择器具有更好的支持:

table.Grid tr + tr + tr{    
    display: none;
}

选择器是相邻的同胞选择器。因此,选择器tr + tr选择即时遵循另一个TR元素的任何TR。有关进一步的外观,请参见:http://css-tricks.com/child-and-sibling-selectors/(特别是标题为"相邻同胞组合者"的部分)。

我认为这是您要寻找的:

for(int i = 1; i < GridView1.Rows.Count; i++)
{
    GridView1.Rows[i].Visible = false;
}

在将数据源绑定到gridview1之后的代码后使用后使用。

最新更新