如何在运行时隐藏GridView中的列。 [英] How do I hide a column in a GridView on runtime.

查看:58
本文介绍了如何在运行时隐藏GridView中的列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试隐藏GridView的一列。我在GridView中有一个模板字段。我正在申请隐藏所需列的代码在GridView中没有模板字段的情况下运行良好。但是因为我需要在GridView中使用模板字段,所以当我添加它时,代码会出错。



GridView(.aspx)代码:



< asp :GridView ID =   GridView1 runat =   server AllowPaging =   True PageSize =   5 AutoGenerateColumns =   True CellSpacing =   4 AutoGenerateDeleteButton =   True AutoGenerateEditButton =   True CellPadding =   4 DataKeyNames =   File_Name GridLines =   OnRowEditing =   GridView1_RowEditing OnRowDeleting =   GridView1_RowDeleting OnRowUpdating =   GridView1_RowUpdating ForeColor =   #333333 OnRowCancelingEdit =   GridView1_RowCancelingEdit OnPageIndexChanging =   GridView1_PageIndexChanging AllowSorting =   True OnSorting =   GridView1_Sorting OnRowCommand =   GridView1_RowCommand OnRowDataBound =   GridView1_RowDataBound OnRowCreated =   GridView1_RowCreated >  
< PagerSettings Mode = NumericFirstLast FirstPageText = First LastPageText = Last PageButtonCount = 10位置= 底部 />
< AlternatingRowStyle BackColor = White />
< Columns>
< asp:TemplateField HeaderText = 文件 >
< ItemTemplate>
< asp:LinkBut​​ton ID = lnkbtnFileName runat = server CommandArgument = ' <%#Eval(File_Name)%>' CommandName = 下载 Text = ' <%#Eval(File_Name)%>' > < / asp:LinkBut​​ton >
< / ItemTemplate >
< / asp:TemplateField >
< / >
< EditRowStyle BackColor = #2461BF />
< FooterStyle BackColor = #507CD1 Font-Bold = True ForeColor = 白色 />
< HeaderStyle BackColor = #507CD1 Font-Bold = True ForeColor = 白色 />
< PagerStyle BackColor = #2461BF ForeColor = 白色 Horizo​​ntalAlign = 中心 />
< 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 >





.c。用于PageLoad()事件的代码是:



  protected   void  Page_Load(对象发​​件人,EventArgs e)
{
如果(!IsPostBack)
{
// BindGrid();
SqlConnection con = new SqlConnection( 数据源= MEHDI-PC \\SQLEXPRESS;初始目录= PIMS; Integrated Security = true;);
{
使用(SqlCommand cmd = new SqlCommand())
{
字符串 sql = 选择*来自dbo.Documents;
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
DataSet ds = new DataSet();
使用(SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
adp.Fill(ds);
}
DataTable mytable = ds.Tables [ 0 ];
GridView1.DataSource = mytable;
GridView1.DataBind();
MultiView1.SetActiveView(vHome);
btnBacktoHome.Visible = false ;
lblStatus.Visible = false ;
}
}
}
}



RowCreated()事件代码是:



  protected   void  GridView1_RowCreated( object  sender,GridViewRowEventArgs e)
{
DataTable myGridDataTable = null ;
TableCell tblcell1;
myGridDataTable =(DataTable)((GridView)sender).DataSource;
if (myGridDataTable!= null
{
tblcell1 = e.Row.Cells [myGridDataTable.Columns [ DocumentsID]。序数];
tblcell1.Visible = false ;
}

}





以上代码运行并带来预期结果如果我删除< TemplateField>来自< GridView1> .aspx代码。任何人都可以告诉我为什么当myGridDataTable填充了具有25列的数据表时,GridView1_RowCreated()事件中`e.Row.Cells`中的枚举单元格数为1。 ?在此先感谢。

解决方案

非常确定您需要rowdatabound事件而不是Rowcreated事件。这就是为什么你的网格不知道你的数据表中的25个字段。在rowcreated事件中数据尚未绑定到网格。



一旦将其更改为Gridview1_RowDataBound事件。添加此行



e.Row.Cells [index] .Visible = false;



其中index是要隐藏的列。


  protected   void  gridview_DataBound( object  sender,EventArgs e)
{
if (cat_check.SelectedItem!= null
{
string columnName = SelectedItem.Text;
var column = gridView1.Columns.Cast< datacontrolfield>()
.FirstOrDefault(c = > c.HeaderText == columnName);
if (column!= null )column.Visible = ;
}
}
< / datacontrolfield >


I'm trying to hide a column of a GridView. I've a template field in my GridView. Code that I'm applying to hide the desired column works well without having template field in the GridView. But because I need to have template field in my GridView, when I add it, code is giving error.

GridView (.aspx) code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" AutoGenerateColumns="True" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated">
                        <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="File">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <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>



.cs code for PageLoad() event is:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindGrid();
                SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        String sql = "select * from dbo.Documents";
                        cmd.Connection = con;
                        cmd.CommandText = sql;
                        con.Open();
                        DataSet ds = new DataSet();
                        using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                        {
                            adp.Fill(ds);
                        }
                        DataTable mytable = ds.Tables[0];
                        GridView1.DataSource = mytable;
                        GridView1.DataBind();
                        MultiView1.SetActiveView(vHome);
                        btnBacktoHome.Visible = false;
                        lblStatus.Visible = false;
                    }
                }
            }
        }


RowCreated() event code is:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    DataTable myGridDataTable = null;
    TableCell tblcell1;
    myGridDataTable = (DataTable)((GridView)sender).DataSource;
    if (myGridDataTable != null)
    {
        tblcell1 = e.Row.Cells[myGridDataTable.Columns["DocumentsID"].Ordinal];
        tblcell1.Visible = false;
    }

}



Above code runs and brings desired results if I remove <TemplateField> from <GridView1> .aspx code. Can anyone tell me why number of enumerated cells in `e.Row.Cells` in GridView1_RowCreated() event is 1 when myGridDataTable is filled with datatable having 25 columns. ? Thanks in advance.

解决方案

Pretty sure you want the rowdatabound event and not the Rowcreated event. Thats why your grid doesn't know about the 25 fields in your datatable. Data hasn't been bound to the grid yet on the rowcreated event.

Once you change this to the Gridview1_RowDataBound event. Add this line inside

e.Row.Cells[index].Visible = false;

where index is the column you want to hide.


protected void gridview_DataBound(object sender, EventArgs e)
{
    if(cat_check.SelectedItem != null)
    {
        string columnName =  SelectedItem.Text;
        var column = gridView1.Columns.Cast<datacontrolfield>()
            .FirstOrDefault(c => c.HeaderText == columnName);
        if (column != null) column.Visible = false;
    }
}
</datacontrolfield>


这篇关于如何在运行时隐藏GridView中的列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆