我正在尝试删除gridview中的一些列。然后这个方法是由我试过的人提供的
protected void yourGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[7].Visible = false;
e.Row.Cells[8].Visible = false;
e.Row.Cells[9].Visible = false;
}
和
GWCase.DataSource = ds;
GWCase.DataBind();
GWCase.Columns[7].Visible = false;
GWCase.Columns[8].Visible = false;
GWCase.Columns[9].Visible = false;
gridview和SQL Server的绑定在页面加载时完成。这就是我的页面加载的样子
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, crdatetime, address, detail, incidentdate, incidenttime, property, victim, suspect from memberreport", conn);
da.Fill(ds);
GWCase.DataSource = ds;
GWCase.DataBind();
conn.Close();
ddlpid1.Visible = false;
ddlpid2.Visible = false;
ddlpid3.Visible = false;
ddlpid4.Visible = false;
ddlpid5.Visible = false;
ddlpid6.Visible = false;
ddlpid7.Visible = false;
ddlpid8.Visible = false;
ddlpid9.Visible = false;
ddlpid10.Visible = false;
}
我试图改变我的
EventArgs to GridViewRowEventArgs.
我运行了代码并且它没有工作,实际上我的gridview消失了。我把它改回原来的eventargs但是gridview仍然消失了。
这是我的gridview的源代码
<asp:GridView ID="GWCase" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="100%" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
似乎通过更改EventArgs会导致整个gridview失败。我删除了我的aspx页面并重新创建并复制相同的代码,gridview仍然消失。
答案 0 :(得分:0)
如果您想要更好地控制哪些列呈现给GridView
,那么您可以使用Columns
的{{1}}属性,如下所示:
GridView
注意:不是按<asp:GridView ID="GWCase" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="100%" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<Columns>
<asp:BoundField datafield="CustomerID" headertext="Customer ID"/>
<asp:BoundField datafield="CompanyName" headertext="Company Name"/>
<asp:BoundField datafield="Address" headertext="Address"/>
<asp:BoundField datafield="City" headertext="City"/>
<asp:BoundField datafield="PostalCode" headertext="Postal Code"/>
<asp:BoundField datafield="Country" headertext="Country"/>
</Columns>
</asp:GridView>
语法隐藏列,而是不要将该列定义为绑定到GWCase.Columns[7].Visible = false;
部分中的GridView
。