当我在gridview中单击下一页时,它给出了错误,我该如何解决? [英] when I click next page in my gridview it gives me error how can I solve it?

查看:89
本文介绍了当我在gridview中单击下一页时,它给出了错误,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面中有一个gridview,它允许分页,但当我点击下一个数字时,它给了我这个错误:System.Web.HttpException:GridView''GridView1''触发了事件PageIndexChanging,但没有处理。我不知道如何解决它,我必须解决它,直到明天我真的需要帮助。

I have a gridview in my page it has allow paging but when I click next number it gives me this error: System.Web.HttpException: The GridView ''GridView1'' fired event PageIndexChanging which wasn''t handled. I dontknow how can I solve it and I have to solve it untill tommorrow I really need help .

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                CellPadding="4"  DataKeyNames="idFromUsers" AutoGenerateColumns="False"
    ForeColor="#333333" GridLines="None" onrowcommand="GridView1_RowCommand"
               ><AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
     <asp:BoundField HeaderText="order code" DataField="idfromIndex"  />
     <asp:BoundField HeaderText="customer code" DataField="idFromUsers" />
     <asp:BoundField HeaderText="factor number"  DataField="indexNO"/>
     <asp:BoundField HeaderText="bank name" DataField="bankName" />
     <asp:BoundField HeaderText="order date" DataField="orderDate" DataFormatString="{0:yyyy/MM/dd}"/>
        <asp:ButtonField CommandName="cart" Text="shopping cart detailsد" >
        <ItemStyle HorizontalAlign="Center" />
        </asp:ButtonField>
        <asp:ButtonField CommandName="customer" Text="customer details">
        <ItemStyle HorizontalAlign="Center" />
        </asp:ButtonField>
        </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>




      protected void Page_Load(object sender, EventArgs e)
      {
    if (!IsPostBack)
    {

        initialGrid1();
    }

      }

       private void initialGrid1()
       {
    getDistinctOrderId();
    DataTable myDT = getTheData();

    GridView1.DataSource = myDT;
    GridView1.DataBind();

     }

         private void getDistinctOrderId()
        {
    string command = "";

    if (ViewState["searchFactor"] != null)
    {
        int index = Convert.ToInt32(ViewState["searchFactor"]);

        switch (index)
        {

            case 1:
                {
                    string fact = factorNO.Text.Trim();
                    command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve=''True'' and Ispay=''False''  and indexNO =''" + fact + "'' )  GROUP BY [idfromIndex]  ";

                }
                break;
            case 2:
                {
                    string name = TextBox3.Text.Trim();
                    string familly = TextBox4.Text.Trim();
                    command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve=''True'' and Ispay=''False''  and user_name =''" + name + "''  and user_familly =''" + familly + "'' )  GROUP BY [idfromIndex]  ";
                }
                break;
            case 3:
                {
                    command = getSearchDate();
                    //string startdate = TextBox1.Text.Trim();
                    //string  enddate = TextBox2.Text.Trim();
                    //command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where IsObserve=''True'' and Ispay=''False'' and orderDate >=''" + startdate + "''  and orderDate <=''" + enddate + "'' )  GROUP BY [idfromIndex]  ";
                }
                break;
            case 0:
                {
                    command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where  IsObserve=''True'' and Ispay=''False'' )  GROUP BY [idfromIndex]  ";

                }
                break;

            default:
                {
                    command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where  IsObserve=''True'' and Ispay=''False'')  GROUP BY [idfromIndex]  ";

                }
                break;
        }
    }
    else
    {
        command = "SELECT min(id), [idfromIndex] FROM OrdersView WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM OrdersView where  IsObserve=''True'' and Ispay=''False'')  GROUP BY [idfromIndex]  ";

    }

    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["mobile_storeConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = myConnection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = command;
    DataView MyDv = new DataView();

    try
    {
        myConnection.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        MyDv = dt.DefaultView;
        for (int i = 0; i < MyDv.Count; i++)
        {
            if (i == 0)
            {
                distinctList = MyDv[i][0].ToString() + ",";
            }
            if (i == MyDv.Count - 1)
            {
                distinctList += MyDv[i][0].ToString();
            }
            else if (i != 0 && i != MyDv.Count - 1)
            {
                distinctList += MyDv[i][0].ToString() + ",";
            }
        }
        dr.Close();

        //Label1.Text = MyDv.Count.ToString();

    }
    catch (Exception EXP)
    {


    }
    finally
    { myConnection.Close(); }
}


          public DataTable getTheData()
            {

    DataSet DS = new DataSet();
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = myConnection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select idfromIndex,indexNO,bankName,Ispay,orderDate,idFromUsers,orderId from factors where orderId IN (" + distinctList + ") and IsObserve=''True''";
    // SqlDataAdapter objSQLAdapter = new SqlDataAdapter("select idfromIndex,indexNO,bankName,Ispay,orderDate,idFromUsers,orderId from factors where orderId IN (" + distinctList + ") and IsObserve=''False''", myConnection); objSQLAdapter.Fill(DS, "mobile_store");
    //ViewState["idfromindex"] = DS.Tables[0].Rows[0].ce
    // DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(DS);
       try
        {
        myConnection.Open();
        cmd.ExecuteNonQuery();

 
         }
       catch (Exception EXP)
       {


         }
        finally
      { myConnection.Close(); }
         return DS.Tables[0];
        }

推荐答案

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindGridView(); //bindgridview will get the data  and bind it again
    }







尝试上面的代码,看看是否有效。




Try above code and see if that works.


<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
CellPadding="4" DataKeyNames="idFromUsers" AutoGenerateColumns="False" onpageindexchanging="GridView1_PageIndexChanging"
ForeColor="#333333" GridLines="None"  onrowcommand="GridView1_RowCommand"
>







并在该代码之后






and after that code

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindGrid(); 
    }


HI,



你可以使用datapager为你的分页提供分页网格。



将以下代码添加到网格控件下方的aspx页面:





You can use datapager to give paging to your grid.

Add the following code to the aspx page below the grid control:

<asp:DataPager ID="DataPagerPro" runat="server"
            PagedControlID="Grid"
            PageSize="8">
    ...
</asp:DataPager>





将以下事件添加到后面的代码中:





Add the following event to code behind:

protected void Grid_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    this.DataPagerPro.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    Grid.DataBind();
}





确保您的网格具有Grid_PagePropertiesChanging方法定义。



谢谢



Make sure your grid is having the "Grid_PagePropertiesChanging" method defined.

Thanks


这篇关于当我在gridview中单击下一页时,它给出了错误,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆