0

我正在尝试删除我的 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 服务器的绑定是在页面加载时完成的。这就是我的页面加载的样子

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 仍然消失了。

4

1 回答 1

0

如果您想更好地控制将哪些列呈现给GridView,则可以使用 的Columns属性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部分中的列Columns

于 2013-08-01T01:57:50.587 回答