Click here to Skip to main content
13,051,404 members (45,790 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am shwetha..

I have a Paged gridview, with pagesize 3..i,e only 3 records are displaying per page.I have added EDIT,UPDATE,CANCEL operations to each row of gridview using Templates.when i click EDIT, the editable fields of that particular row [here, i have used e.row.rowindex of RowDataBound event, to load the values to dropdown list on particular row select] are loaded with possible data values via dropdown list.

This mechanism is working fine with the first page of gridview..i,e pagenum=0.
But the same is not working fine with the second page.Because when i select EDIT operation of "first row" in the "second page" of gridview, the e.row.RowIndex value is again starting from the editable fields of that row are loading with the first page, first row data..

I want to maintain the rowindex value depending on i have pagesize 3..when i select EDIT operation of first row of second page..e.row.rowINDEX value must be in 3rd page the first row e.row.rowIndex value must be 6....

Please anyone help me...

aspx.cs code:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
           GridView1.PageIndex = e.NewPageIndex;
           Session["PageNum"] = e.NewPageIndex;
           PageSet = int.Parse(Session["PageNum"].ToString());

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            Label lb5 = (Label)GridView1.Rows[e.RowIndex].FindControl("Label5");
            Label lb6 = (Label)GridView1.Rows[e.RowIndex].FindControl("Label6");
            Label lb7 = (Label)GridView1.Rows[e.RowIndex].FindControl("Label7");
            DropDownList ddl1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
            DropDownList ddl2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");
            DropDownList ddl3 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList3");
            DropDownList ddl4 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList4");
            Label lb12 = (Label)GridView1.Rows[e.RowIndex].FindControl("Label12");
            GridView1.DataSource = data.UpdateReport(ddl1.SelectedValue.ToString(), ddl2.SelectedValue.ToString(), ddl3.SelectedValue.ToString(), ddl4.SelectedValue.ToString(), lb5.Text.ToString());
            GridView1.EditIndex = -1;
            lblMsg.Visible = true;
            lblMsg.Text = "Updated Successfully...";

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            ListItem lt = new ListItem();
            //DataRowView drv = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    if (PageSet != 0)
                        index = (PageSet * 3) + e.Row.RowIndex;                    
                        index = e.Row.RowIndex;
                    DropDownList dp1 = (DropDownList)e.Row.FindControl("DropDownList1");
                    DataTable dt = data.DisplayData();
                    dp1.SelectedValue = dt.Rows[index][3].ToString();

                    DropDownList dp2 = (DropDownList)e.Row.FindControl("DropDownList2");
                    dp2.SelectedValue = dt.Rows[index][4].ToString();

                    DropDownList dp3 = (DropDownList)e.Row.FindControl("DropDownList3");
                    dp3.SelectedValue = dt.Rows[index][5].ToString();

                    DropDownList dp4 = (DropDownList)e.Row.FindControl("DropDownList4");
                    //for (int i = 0; i < dt.Rows.Count; i++)
                    //ListItem lt = new ListItem();
                    dp4.SelectedValue = dt.Rows[index][6].ToString();
                    //    dp4.Items.Add(lt);

protected void GridView1_PageIndexChanged(object sender, EventArgs e)
           string direction,sortExp;
           //PageSet = 1;
               direction = Session["direction"].ToString();
               sortExp = Session["sortExp"].ToString();
               ShowSortGrid(sortExp, direction);
Posted 28-Feb-12 22:45pm
Updated 29-Feb-12 21:04pm
jkarthick1989 29-Feb-12 4:50am
The same problem i found in my application, i have coded lot for binding it again and again. but it always fails at any one condition.

Hence finally i just remove Paging from my grid.
ankitmaini 1-Mar-12 4:07am
what is PageSet and where it is declared? if it is declared at class level, it is possible that it gets initialised to 0 on EDIT calls. try gridview1.PageIndex instead of PageSet. It may solve the issue.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I have maintained an id column(invisible to user) when user clicks the 'Edit' i just bind the row with column values based on that id columns.
Member 8680835 29-Feb-12 5:01am
I dont want to remove paging..i want the same code to work with that..
jkarthick1989 29-Feb-12 5:59am
Yes....For that you can maintain UNIQUE ID column
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I have had the same problem:

With a RowCommand button I caught the selected rowindex (e.CommandArgument), but this referred to the selected index of PageIndex 0.

What I did to solve this is to "remember" the selected page (Grid.PageIndex) in a Session variable whenever I change page (on Grid.PageIndexChanged event, start in Page_Load with 0).
When I wanted to have the selected rowdata, I set Grid.PageIndex to Session variable and confirmed with Grid.DataBind.

index = Convert.ToInt32(e.CommandArgument)
If Not IsNothing(Session("page")) Then
    Gridview.PageIndex = Session("page")
End If
Dim selectedRow As GridViewRow = Gridview.Rows(index)
Dim IDCell As TableCell = selectedRow.Cells(0) 'Cell(0) is ID needed
strDesiredValue = IDCell.Text

Maybe not the best solution, but worked for me..
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Add a Data table to the grid and make the update and delete operations based on data table row count..

DataTable dt = (DataTable)Session["CurrentTable"];
           if (dt.Rows.Count > 0)
               int i=0;
               int limit = dt.Rows.Count;
               if (GridviewOrder.PageIndex > 0)
                   i = (GridviewOrder.PageIndex * GridviewOrder.PageSize);
                   if (dt.Rows.Count >=((GridviewOrder.PageIndex + 1)* GridviewOrder.PageSize) + 1)
                       limit = (GridviewOrder.PageIndex + 1) * GridviewOrder.PageSize+1;
                       limit = i+GridviewOrder.Rows.Count;
                   if (dt.Rows.Count >= GridviewOrder.PageSize+1)
                       limit = GridviewOrder.Rows.Count + 1;
                       limit = GridviewOrder.Rows.Count;
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

i = Convert.ToInt32(e.CommandArgument)
i = i - (GridView1.PageIndex * GridView1.PageSize)

Dim myRow As GridViewRow = GridView1.Rows(i)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 12 Nov 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100