Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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 0..so the editable fields of that row are loading with the first page, first row data..

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

Please anyone help me...

aspx.cs code:

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

C#
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;
            ShowGrid();
            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;                    
                    else
                        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);
                    //}
                    //drv[6].ToString();
                    
                }
            }
        }


C#
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
       {
           string direction,sortExp;
           //PageSet = 1;
           if(Set==1)
           {
               direction = Session["direction"].ToString();
               sortExp = Session["sortExp"].ToString();
               ShowSortGrid(sortExp, direction);
           }
           else
               ShowGrid();
       }
Posted
Updated 29-Feb-12 21:04pm
v2
Comments
J.Karthick 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.
Ankit Maini 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.

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.
 
Share this answer
 
Comments
Member 8680835 29-Feb-12 5:01am    
I dont want to remove paging..i want the same code to work with that..
J.Karthick 29-Feb-12 5:59am    
Yes....For that you can maintain UNIQUE ID column
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.

VB
index = Convert.ToInt32(e.CommandArgument)
If Not IsNothing(Session("page")) Then
    Gridview.PageIndex = Session("page")
    Gridview.DataBind()
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..
 
Share this answer
 
C#
<small><small><small></small></small></small>
Add a Data table to the grid and make the update and delete operations based on data table row count..


C#
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;
                   else
                       limit = i+GridviewOrder.Rows.Count;
               }
               else
               {
                   if (dt.Rows.Count >= GridviewOrder.PageSize+1)
                       limit = GridviewOrder.Rows.Count + 1;
                   else
                       limit = GridviewOrder.Rows.Count;
               }
            }
 
Share this answer
 
v2
i = Convert.ToInt32(e.CommandArgument)
i = i - (GridView1.PageIndex * GridView1.PageSize)

Dim myRow As GridViewRow = GridView1.Rows(i)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900