如何在GridView中隐藏列 [英] How do I hide a column in a GridView

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

问题描述

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



GridView(.aspx)代码:



I'm trying to hide a column in 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>




$ b PageLoad()事件的$ b .cs代码是:





.cs code for PageLoad() event is:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                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()事件代码是:





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

}





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



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.

推荐答案

在不知道您实际遇到的错误的情况下,当前代码出现了这些错误:



1.您的模板列将显示在其他列的左侧,因此与数据源中的Ordinal相比,所有自动生成的列上都有+1偏移量

2.您应该检查RowCreated事件处理程序中的rowtype以查看它是数据行或标题,因为您只有这些行中的单元格:

Without knowing which error you're actually getting, these things are wrong with the current code:

1. Your template column will appear to the left of the other columns, so compared to the Ordinal in your datasource, there is a +1 offset on all auto-generated columns
2. You should probably check the rowtype in your RowCreated event handler to see if it is a datarow or headerrow, because you will only have the cells in these rows:
e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow





然而,实际上会看到您收到的错误消息会有所帮助。我永远不明白为什么人们说有错误信息,不要在帖子中包含它,并期望正确的解决方案......



It would help however, to actually see the error message that you're getting. I never understand why people say there's an error message, don't include it in their post, and expect the right solution...






在rowdatabound事件中使该单元格不可见(即visible = false)

这是最好的方法。



例如:



protected void Gridview1_RowDataBound(object sender,GridViewRowEventArgs e)

{

GridViewRow gr = e。行;

if(e.Row.RowType == DataControlRowType.Header)

{

gr.Cells [i] .Visible = false ;

}

else if(e.Row.RowType == DataControlRowType.DataRow)

{

gr .Cells [i] .Visible = false;

}

}





这里我引用了单元格inidex。
Hi,

make that cell invisible(i.e visible=false) in rowdatabound event
it is the best way.

ex:

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gr = e.Row;
if (e.Row.RowType == DataControlRowType.Header)
{
gr.Cells[i].Visible = false;
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
gr.Cells[i].Visible = false;
}
}


here i referes inidex of the cell.


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

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