如何隐藏随机生成的gridview行

HI编码器我有一个gridview,其中数据来自数据库..现在所有行的id都是我想要它只显示我的gridview的第一行并隐藏其他的其他但不是它们的值我只是wana隐藏它们。 我想要他们的价值,因为我有一个灯箱。

这是我的代码:

    <asp:Label ID="lblpptId" runat="server" Text=''>     <asp:Label ID="lblPriority" runat="server" Text=''>     <a id="imageLink" href='https://stackoverflow.com/questions/21379242/how-to-hide-randomly-generated-row-of-gridview/' title='' rel="lightbox[Brussels]" runat="server">Start PPT                

编辑::

 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(); } } 

在上面的代码中pptId对于每一行是相同的,我的图像在我的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,如下所示:

       
<%--Your templates here--%>

Asp.net将从您的gridview生成一个html表,这就是您需要将样式应用于表的原因。 浏览器永远不会看到任何asp.net代码。 该代码只是生成浏览器知道如何显示的html的一种方式。

编辑有关更好的IE支持:

我想你要问的是IE8(可能还有7个)支持,因为IE9和支持nth-child。 要支持IE7及更高版本,请更改您的css以使用+选择器,它具有更好的支持:

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

+选择器是相邻的兄弟选择器。 因此,选择器tr + tr选择任何立即跟随另一个tr元素的tr。 有关进一步的了解,请参阅: http ://css-tricks.com/child-and-sibling-selectors/(特别是标题为“Adjacent sibling combinator”的部分)。

我认为这就是你要找的东西:

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

在将数据源绑定到后面的代码中的GridView1之后使用它。