【问题标题】:Hide a gridView row in asp.net在 asp.net 中隐藏 gridView 行
【发布时间】:2008-09-22 12:23:36
【问题描述】:

我正在创建一个gridView,它允许通过将插入所需的控件添加到FooterTemplate 中来添加新行,但是当ObjectDataSource 没有记录时,我添加了一个虚拟行,因为FooterTemplate 是有数据时才显示。

如何隐藏这个虚拟行?我尝试在RowDataBound 上设置e.row.visible = false,但该行仍然可见。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以处理 gridview 的数据绑定事件并隐藏虚拟行。 (不要忘记在 aspx 代码中分配事件属性):

    protected void GridView1_DataBound(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count == 1)
                GridView1.Rows[0].Visible = false;
        }
    

    【讨论】:

      【解决方案2】:

      请尝试以下方法

          protected void GridView1_DataBound(object sender, EventArgs e)
          {
              GridView1.Rows[0].Visible = false;
          }
      

      【讨论】:

      • 这隐藏了我的整个 GV。
      【解决方案3】:

      我认为这是你需要的:

      <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowDataBound="OnRowDataBound">
          <Columns>
              <asp:TemplateField HeaderText="headertext">
                  <ItemTemplate>
                      itemtext
                  </ItemTemplate>
                  <FooterTemplate>
                      insert controls
                  </FooterTemplate>
              </asp:TemplateField>
          </Columns>
      </asp:GridView>
      

      以及代码隐藏:

      protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              e.Row.Attributes["style"] = "display:none";
          }
      }
      

      但我不明白您为什么要将“插入控件”添加到页脚而不是将它们放在网格下方。

      【讨论】:

        【解决方案4】:

        不妨试试:

        e.Row.Height = Unit.Pixel(0);
        

        这不是正确的答案,但在您得到正确答案之前它可能会起作用。

        【讨论】:

          【解决方案5】:

          也许用 CSS 来设置不显示?!

          【讨论】:

            【解决方案6】:

            这是 GridView 控件的错误使用。 GridView 控件有一个特殊的 InsertRow,这是您的控件应该去的地方。

            【讨论】:

            • 我总是希望显示页脚,因为这是包含我的插入控件的位置,我想隐藏我添加的 DummyRow。
            【解决方案7】:

            GridView 有一个特殊的属性可以访问 Footer Row,名为“FooterRow”

            然后,你冷试试 yourGrid.FooterRow.Visible = false;

            【讨论】:

              【解决方案8】:

              我在之前的工作中这样做了,但由于您可以添加行,所以我总是在页脚行中看到它。为了使网格显示出来,我绑定了一个通常绑定类型的空行

              dim row as Datarow = table.NewRow()
              table.AddRow(row)
              gridView.DataSource = table
              gridView.Databind()
              

              然后它有所有的列,然后你需要。你可以通过拉这个来访问页脚:

              'this will get the footer no matter how many rows there are in the grid.
              
              Dim footer as Control = gridView.Controls(0).Controls(gridView.Controls(0).Controls.Count -1)
              

              然后要访问页脚中的任何控件,您需要执行以下操作:

              Dim cntl as Control = footer.FindControl(<Insert Control Name Here>)
              

              我假设你能够做到:

              footer.Visible = false
              

              使页脚行不可见。

              我希望这会有所帮助!

              编辑我才明白你说的是什么。当我添加新行时,我基本上会删除该行,但要执行此操作,您需要检查是否有任何其他行,如果有,请检查其中是否有值。

              要删除虚拟行,请执行以下操作:

              If mTable.Rows.Count = 1 AndAlso mTable.Rows(0)(<first column to check for null value>) Is DBNull.Value AndAlso mTable.Rows(0)(<second column>) Is DBNull.Value AndAlso mTable.Rows(0)(<thrid column>) Is DBNull.Value Then  
              mTable.Rows.Remove(mTable.Rows(0))  
              End If
              mTable.Rows.Add(row)
              gridView.Datasource = mTable
              gridView.Databind()
              

              【讨论】:

                【解决方案9】:

                您为什么不使用 EmptyDataTemplate?即使我只使用了几天,它似乎也很有效......

                【讨论】:

                  【解决方案10】:

                  您应该在 GridView 中使用 DataKeyNames:

                  &lt;asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID"&gt;

                  然后在您的代码中检索它: GridView1.DataKeys[0].Value.ToString()

                  其中“0”是您要获取“FooID”的行号

                  【讨论】:

                    【解决方案11】:

                    要使其可见,只需使用:

                    Gridview.Rows.Item(i).Attributes.Add("style", "display:block")
                    

                    让它不可见

                    Gridview.Rows.Item(i).Attributes.Add("style", "display:none")
                    

                    【讨论】:

                      【解决方案12】:

                      如果不想在列/行为空时显示数据:

                      if (!String.IsNullOrEmpty(item.DataName))
                      {
                          e.Row.Visible = false;
                      }
                      

                      【讨论】:

                        【解决方案13】:

                        通过SQL可以轻松完成

                        USE YourdatabaseName select * from TableName where Column_Name <> ''
                        

                        【讨论】:

                        • -1 该问题与SQL无关,他正在尝试使用GridView。
                        猜你喜欢
                        • 2010-09-17
                        • 2013-02-05
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2013-03-01
                        • 2016-12-06
                        • 2011-07-17
                        相关资源
                        最近更新 更多