在GridView中进行分页时出错

时间:2015-04-23 07:23:50

标签: c# asp.net gridview

我有一个包含两列的GridView。第一列显示日期,第二列显示第一列中的日期年份。到目前为止,一切都运行良好但是当我更改页面时,网络失败并显示以下消息:"对象引用未设置为对象的实例。"

这是我的代码:

Test.aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="qq_site_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TEST</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" 
            AllowSorting="True" AutoGenerateColumns="False" 
            EnableModelValidation="True" onrowcreated="GridView1_RowCreated" 
            onpageindexchanged="GridView1_PageIndexChanged" 
            onpageindexchanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="creationDate" HeaderText="creationDate" SortExpression="creationDate" />
                <asp:TemplateField HeaderText="Year">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>                
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Test.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Data;

public partial class qq_site_Test : System.Web.UI.Page
{
    static DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            String connectionString = "DSN=kitchenmaster.es.qq-site;";
            String sqlQuery = "SELECT * FROM tblSystems WHERE ID < 100";

            ds = new DataSet();

            OdbcConnection connection = new OdbcConnection(connectionString);
            OdbcCommand command = new OdbcCommand(sqlQuery, connection);
            OdbcDataAdapter adapter = new OdbcDataAdapter(command);

            try
            {
                adapter.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            catch (System.Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "creationDate");
                e.Row.Cells[GetColumnByID("Year")].Text = dt.Year.ToString();
            }
            catch (System.Exception ex)
            {
                Response.Write(ex.Message);             
            }
        }
    }

    protected int GetColumnByID(String columnName)
    {
        foreach (DataControlField column in GridView1.Columns)
        {
            if (column.HeaderText == columnName)
                return GridView1.Columns.IndexOf(column);
        }

        return -1;
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        GridView1.SelectedIndex = -1;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            if (e.NewPageIndex != -1)
            {
                GridView1.PageIndex = e.NewPageIndex;
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
        catch (System.Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

你能帮助我吗?

3 个答案:

答案 0 :(得分:1)

这行代码

GridView1.DataSource = ds;

可能是您收到错误的地方。

调用GridView1_PageIndexChanging事件时,您尚未设置ds的值。代码中唯一能够执行此操作的部分是Page_Load事件,并且只有当它不是回发时才会出现。

答案 1 :(得分:0)

如果我在事件GridView1_RowCreated()中检查e.Row.DataItem的值,则不会发生错误,但我不知道这是否正确。新版本的活动是:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                if (e.Row.DataItem != null)
                {
                    DateTime dt = (DateTime)DataBinder.Eval(e.Row.DataItem, "creationDate");
                    e.Row.Cells[GetColumnByID("Year")].Text = dt.Year.ToString();
                }
            }
            catch (System.Exception ex)
            {
                Response.Write("** " + ex.Message);
            }
        }
    }

答案 2 :(得分:0)

请检查你的&#34; ds&#34; PageIndexChanging事件中的对象。 首先填写数据集并绑定。