0

我正在尝试将特定的 gridview 数据移动到 excel 文件中,但出现错误object reference not set to the instance of an object

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
HeaderStyle-BackColor = "green">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="Chckcol0" runat="server" Checked="true"/>
<asp:Label ID="lbl_catid" runat="server" Text=" CategoryID"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_catid" runat="server" Text='<%# Eval("cat_id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="Chckcol1" runat="server" Checked="true"/>
<asp:Label ID="lbl_catname" runat="server" Text="category Name"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_categoryname" runat="server" Text='<%# Eval("categoryname")%>'>     </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="Chckcol2" runat="server" Checked="true"/>
<asp:Label ID="description" runat="server" Text="description"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_description" runat="server" Text='<%# Eval("description")%>'>  </asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

而我背后的代码:

public partial class _Default : System.Web.UI.Page
{
MySqlConnection con = new MySqlConnection("Data Source=localhost;Database=mylibrary_db;User ID=root;Password=MyLuck2010");
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        fillgrid();
    }
}
protected void fillgrid()
{
    MySqlDataAdapter da = new MySqlDataAdapter("select cat_id,categoryname,description from library_category", con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
}
protected void getcheckedgridview()
{
    CheckBox chkCol0 = (CheckBox)GridView1.HeaderRow.Cells[0].FindControl("chkCol0");
    CheckBox chkCol1 = (CheckBox)GridView1.HeaderRow.Cells[0].FindControl("chkCol1");
    CheckBox chkCol2 = (CheckBox)GridView1.HeaderRow.Cells[0].FindControl("chkCol2");
    ArrayList arr;
    if (ViewState["States"] == null)
    {
        arr = new ArrayList();
    }
    else
    {
        arr = (ArrayList)ViewState["States"];
    }
    arr.Add(chkCol0.Checked);
    arr.Add(chkCol1.Checked);
    arr.Add(chkCol2.Checked);
    ViewState["States"] = arr;
}
protected void btnExportExcel_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
    GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
    GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
    GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
    ArrayList arr = (ArrayList)ViewState["States"];
    GridView1.HeaderRow.Cells[0].Visible = Convert.ToBoolean(arr[0]);
    GridView1.HeaderRow.Cells[1].Visible = Convert.ToBoolean(arr[1]);
    GridView1.HeaderRow.Cells[2].Visible = Convert.ToBoolean(arr[2]);
    GridView1.HeaderRow.Cells[0].FindControl("chkCol0").Visible = false;
    GridView1.HeaderRow.Cells[1].FindControl("chkCol1").Visible = false;
    GridView1.HeaderRow.Cells[2].FindControl("chkCol2").Visible = false;

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {

        GridViewRow row = GridView1.Rows[i];
        row.Cells[0].Visible = Convert.ToBoolean(arr[0]);
        row.Cells[1].Visible = Convert.ToBoolean(arr[1]);
        row.Cells[2].Visible = Convert.ToBoolean(arr[2]);
        row.BackColor = System.Drawing.Color.White;
        row.Attributes.Add("class", "textmode");

        if (i % 2 != 0)
        {
            row.Cells[0].Style.Add("background-color", "#C2D69B");
            row.Cells[1].Style.Add("background-color", "#C2D69B");
            row.Cells[2].Style.Add("background-color", "#C2D69B");
        }

    }

    GridView1.RenderControl(hw);
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.End();
}

}

4

1 回答 1

0

故障最有可能与Convert.ToBoolean(arr[0]);(见行GridView1.HeaderRow.Cells[0].Visible = Convert.ToBoolean(arr[0]);)

我会在故障线上放一个中断标签,看看它的值arr[0]是多少。我猜 arr 为空或 null 并且您的 ViewState 没有返回您期望的数据。这可能是因为您getcheckedgridview()的代码没有调用您的方法(或者至少,不是您显示的代码)!

您可以尝试(唯一的变化是方法的第一行)

protected void btnExportExcel_Click(object sender, EventArgs e)
{
    getcheckedgridview();//Only change to your code
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
    GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
    GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
    GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
    ArrayList arr = (ArrayList)ViewState["States"];
    GridView1.HeaderRow.Cells[0].Visible = Convert.ToBoolean(arr[0]);
    GridView1.HeaderRow.Cells[1].Visible = Convert.ToBoolean(arr[1]);
    GridView1.HeaderRow.Cells[2].Visible = Convert.ToBoolean(arr[2]);
    GridView1.HeaderRow.Cells[0].FindControl("chkCol0").Visible = false;
    GridView1.HeaderRow.Cells[1].FindControl("chkCol1").Visible = false;
    GridView1.HeaderRow.Cells[2].FindControl("chkCol2").Visible = false;

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {

        GridViewRow row = GridView1.Rows[i];
        row.Cells[0].Visible = Convert.ToBoolean(arr[0]);
        row.Cells[1].Visible = Convert.ToBoolean(arr[1]);
        row.Cells[2].Visible = Convert.ToBoolean(arr[2]);
        row.BackColor = System.Drawing.Color.White;
        row.Attributes.Add("class", "textmode");

        if (i % 2 != 0)
        {
            row.Cells[0].Style.Add("background-color", "#C2D69B");
            row.Cells[1].Style.Add("background-color", "#C2D69B");
            row.Cells[2].Style.Add("background-color", "#C2D69B");
        }

    }

    GridView1.RenderControl(hw);
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.End();
}

编辑

根据您的评论,我认为错误消息现在是由于您的代码没有找到有问题的控件。所以

CheckBox chkCol0 = (CheckBox)GridView1.HeaderRow.Cells[0].FindControl("chkCol0");

找不到名为“chkCol0”的控件,因此 chkCol0 为空。

于 2013-04-15T08:49:33.547 回答