GridView,怎样在后台控制前台的一些列显示或者隐藏
比如GridView前台绑定了三个列 分别为 “查看” “修改” “删除” 代码如下
<asp:GridView ID="gv_OrderInfo" runat="server" OnSelectedIndexChanged="gv_OrderInfo_SelectedIndexChanged" AutoGenerateColumns="False" OnRowEditing="gv_OrderInfo_RowEditing" OnRowDeleting="gv_OrderInfo_RowDeleting" OnRowDataBound="gv_OrderInfo_RowDataBound1"> <Columns> <asp:ButtonField CommandName="Select" Text="查看" > <ControlStyle ForeColor="Blue" /> <ItemStyle Width="30px"></ItemStyle> </asp:ButtonField> <asp:ButtonField CommandName="Edit" Text="修改" > <ControlStyle ForeColor="Blue" /> <ItemStyle Width="30px"></ItemStyle> </asp:ButtonField> <asp:ButtonField CommandName="Delete" Text="删除" > <ItemStyle Width="30px"></ItemStyle> <ControlStyle ForeColor="Blue" /> </asp:ButtonField> </Columns> </asp:GridView>
Asp.net2.0GridView隐藏列visible="false" 后你就无法取得这列的值了, 而用datagrid就没有这个问题 protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e) { //隐藏不必要的列 if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer)) { e.Row.Cells[0].Visible = false; e.Row.Cells[3].Visible = false; } } 这是迄今为止最简洁的解决方法了。 实例:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //隐藏不必要的列 if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer)) { e.Row.Cells[3].Visible = false; e.Row.Cells[4].Visible = false; // e.Row.Cells[3]序号从0开始算 } }