62,047
社区成员
发帖
与我相关
我的任务
分享
if (e.Row.RowType == DataControlRowType.Header)
{
for (int iColums = 0; iColums < dt.Rows.Count; iColums++)
{//循环在列表中要隐藏显示的列
strColums = dt.Rows[iColums][0].ToString();
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环当前绑定列表,从中找到与当前要隐藏的列名同名的表头列,将该cells置为false以达到隐藏显示列的目的
if (e.Row.Cells[iCells].Text == strColums)
{//这个地只有前面几个固定列能取到页眉的文本值 其余都为空 e.Row.Cells[iCells].Visible = false;
lInt.Add(iCells);
break;//找到后跳出内层循环继续循环要隐藏的下一列
}
}
}
}
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" OnSorting="GridView1_Sorting" AllowSorting="True"
Width="100%" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
CssClass="border" OnRowCreated="GridView1_RowCreated">
<AlternatingRowStyle HorizontalAlign="Center" CssClass="dbtable_data1" />
<RowStyle HorizontalAlign="Center" CssClass="dbtable_data2" VerticalAlign="Middle"></RowStyle>
<HeaderStyle CssClass="dbtable_title"></HeaderStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
<Columns>
<asp:TemplateField HeaderText="编号">
<HeaderStyle Width="2px" />
<ItemStyle Width="2px" />
<ItemTemplate>
<asp:Label ID="lblNum" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑">
<HeaderStyle Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="edititem" CommandArgument='<%# Eval(PKField) %>'>
<asp:Image runat="server" AlternateText="编辑" ID="Image1" ImageUrl="~/img/modify.gif">
</asp:Image>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<HeaderStyle Width="40px"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonDelete" runat="server" CommandName="deleteitem" CommandArgument='<%# Eval(PKField) %>'>
<asp:Image ID="Image2" runat="server" ImageUrl="~/img/delete.gif" AlternateText="删除">
</asp:Image>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="查看">
<HeaderStyle Width="40px"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonLook" runat="server" CommandName="lookitem" CommandArgument='<%# Eval(PKField) %>'>
<asp:Image ID="Image3" runat="server" ImageUrl="~/img/look.gif" AlternateText="查看详细">
</asp:Image>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<input type='checkbox' name='allbox' onclick='javascript:selectAll();'"
Visible="false">
<HeaderStyle Width="2%"></HeaderStyle>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="Cyan" CssClass="dbtable_data3" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
try
{
string strColums = "";
if (e.Row.RowType == DataControlRowType.Header)
{
for (int iColums = 0; iColums < dt.Rows.Count; iColums++)
{//循环在列表中要隐藏显示的列
strColums = dt.Rows[iColums][0].ToString();
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环当前绑定列表,从中找到与当前要隐藏的列名同名的表头列,将该cells置为false以达到隐藏显示列的目的
if (e.Row.Cells[iCells].Text == strColums)
{
e.Row.Cells[iCells].Visible = false;
lInt.Add(iCells);
break;//找到后跳出内层循环继续循环要隐藏的下一列
}
}
}
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < lInt.Count; j++)
{
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环在列表中要隐藏显示的列
if (iCells == lInt[j])
{
e.Row.Cells[iCells].Visible = false;
break;
}
}
}
}
}
catch (Exception err)
{
AjaxAlert("隐藏显示列失败,请联系管理员 表" + TableName + "的字典配置 错误描述:" + ConvertTohtml(err.Message.ToString()));
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string[] arrayHide = {"ProductID", "ProductName"}; //要隐藏的列名,这里用string[]代替了你的DataTable.
if (e.Row.RowType == DataControlRowType.Header)
{
for (int iColums = 0; iColums < arrayHide.Length; iColums++)
{
string strColums = arrayHide[iColums];
for (int iCells = 0; iCells < e.Row.Cells.Count; iCells++)
{//循环当前绑定列表,从中找到与当前要隐藏的列名同名的表头列,将该cells置为false以达到隐藏显示列的目的
if (e.Row.Cells[iCells].Text == strColums)
{
e.Row.Cells[iCells].Visible = false;
break;
}
}
}
}
}