C# 为什么我的gridview在编辑一些gridviewroweditor后消失了

C# 为什么我的gridview在编辑一些gridviewroweditor后消失了,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试删除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; } 及 gridview和SQL server的绑定在页面

我正在尝试删除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;
}

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

如果您想要更多地控制哪些列被渲染到
GridView
,那么您可以使用
GridView
columns
属性,如下所示:

<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语法,您只是没有在
Columns
部分中定义要绑定到
GridView
的列。

您将页面\u load eventargs更改为GridView roweventargs??我正在尝试。我更改了它,运行了它,gridview就消失了。然后我把它改回
eventargs
,我的gridview就完全消失了。即使删除了我以前的gridview并添加了一个空的gridview,但仍然没有显示gridview。您使用过try-catch块吗?GridView没有出现的一个可能原因是代码在GridView.DataBind()之前终止。我已经尝试过了,但它没有出现。但我去拿备份文件后很快就开始工作了。似乎当我将作为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" />
    <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>