【问题标题】:hiding the Select button in gridview在gridview中隐藏选择按钮
【发布时间】:2012-08-06 05:34:59
【问题描述】:

我目前有一个gridview,允许您通过单击该行的任意位置来选择该行。但是,有一个问题。 AutoGenerateSelectButton 必须设置为 true。这意味着选择按钮现在可见并且是网格视图的一部分。我想知道是否有办法在不破坏网格大小的情况下隐藏它?

【问题讨论】:

  • 你问是否有办法从渲染的标记中隐藏选择按钮?

标签: c# asp.net select gridview


【解决方案1】:

您是否考虑过使用 CSS 隐藏该列?这可以在RowDataBound 事件中完成:

protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Style["display"] = "none";
    // or e.Row.Cells[0].CssClass = "hidden-cell";
}

【讨论】:

  • 这是我最初尝试的方法。虽然它弄乱了网格。行的大小变得很大,并与标题一起消失。我认为这可能是因为 jQuery 可自动滚动并且它不会影响标题。
  • 你在if (e.Row.RowType == DataControlRowType.DataRow) { } 支票后面有它吗?如果是这样,它只会隐藏行并且不会影响页眉/页脚。这可能会破坏您的桌面显示。
【解决方案2】:

你可以像这样隐藏整行

protected void grdView_OnDataBound(object sender, EventArgs e)
{
    grdView.Rows[rowNumber].Visible = false;
}

【讨论】:

  • 该方法似乎不起作用。由于某种原因,它不允许我的数据绑定。
【解决方案3】:

检查每一行并根据需要隐藏/调整标签:

foreach (GridViewRow r in gv.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    TableCell editCell = r.Cells[0];
                    if (editCell.Controls.Count > 0)
                    {
                        LinkButton editControl = editCell.Controls[0] as LinkButton;
                        // control[1] is a literal space
                        LinkButton selectControl = editCell.Controls[2] as LinkButton;
                        editControl.Text = "New Edit Label Text";
                        //Ensure "Select" control, not "Cancel" control 
                        selectControl.Text = selectControl.Text == "Select" ? "New Select Label Text" : selectControl.Text;
                    }
                }
            }

【讨论】:

    【解决方案4】:

    基于 derek 的回答,这就是我所做的

    <asp:Panel ID="pnl" runat="server" Visible="false">
    <asp:GridView ID="gv" runat="server" Width="100%" AutoGenerateColumns="False"
        DataKeyNames="BillingAccountNo" OnRowDataBound="gv_RowDataBound"
        OnSelectedIndexChanged="dgBillingInformation_SelectedIndexChanged">
        <Columns>>
            <asp:BoundField DataField="field" HeaderText="field" />
            <asp:BoundField DataField="field" HeaderText="field" />
            <asp:CommandField ShowSelectButton="true" ButtonType="Button" Visible="true" />
        </Columns>
        <SelectedRowStyle BackColor="LightCyan" ForeColor="DarkBlue" Font-Bold="true" />
    </asp:GridView>
    

    我把选择按钮放在gridview的最后,用derek的代码在后面遮住最后一行,消除了隐藏选择按钮和表头搞砸的问题。

    【讨论】:

      【解决方案5】:

      您可以隐藏一整列,然后在完成后使用以下内容再次显示它..

      YourGridView.columns(0).Visible = False //Will hide the entire first column with all the select links in there. 
      

      还将标题列向左移动,以便它们正确匹配以进行打印等...

      然后当你需要再次显示时,只需再次将属性更改为true即可。 这是 VB 语法,您可能希望在 C# 中使用 [] 作为列索引。

      【讨论】:

        【解决方案6】:
                        <asp:GridView ID="gv" runat="server" Width="100%" AutoGenerateColumns="False"
                            DataKeyNames="BillingAccountNo" OnRowDataBound="gv_RowDataBound"
                            OnSelectedIndexChanged="dgBillingInformation_SelectedIndexChanged">
                            <Columns>
        
                                <asp:BoundField DataField="field" HeaderText="field" />
                                <asp:BoundField DataField="field" HeaderText="field" />
                                <asp:CommandField ShowSelectButton="true" ButtonType="Button" ControlStyle-CssClass="hidden" ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden" FooterStyle-CssClass="hidden" />
                            </Columns>                     
                        </asp:GridView>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-04
          • 1970-01-01
          • 2015-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多