如何获取gridview [paged]行的行索引 [英] How to get the row index of a gridview[paged] row

查看:70
本文介绍了如何获取gridview [paged]行的行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是shwetha ..



我有一个Paged gridview,pagesize 3..i,e只有3每页都显示记录。我使用Templates为每行gridview添加了EDIT,UPDATE,CANCEL操作。当我点击EDIT时,该特定行的可编辑字段[这里,我使用了RowDataBound事件的e.row.rowindex,将值加载到特定行的下拉列表中选择]通过下拉列表加载可能的数据值。



这个机制在gridview的第一页工作正常。 .i,e pagenum = 0。

但第二页的效果不一样。因为当我在gridview的第二页中选择第一行的EDIT操作时,e .row.RowIndex值再次从0开始。所以该行的可编辑字段正在加载第一页,第一行数据..



我想要维护rowindex值取决于pagesize ..我有pagesize 3 ..当我选择第一行的EDIT操作第二页...... e.row.rowINDEX值必须为3 ...第三页接下来第一行e.row.rowIndex值必须为6 ....



请有人帮帮我...



aspx.cs代码:



<前lang = cs> protected void GridView1_PageIndexChanging( object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
会话[ PageNum] = e.NewPageIndex;
PageSet = int .Parse(Session [ 页次]的ToString());
ShowGrid();
}



  protected   void  GridView1_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{
Label lb5 =(Label)GridView1.Rows [e .RowIndex] .FindControl( Label5);
标签lb6 =(标签)GridView1.Rows [e.RowIndex] .FindControl( Label6);
标签lb7 =(标签)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);
标签lb12 =(标签)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 = 已成功更新......;


}

受保护 void GridView1_RowDataBound( object sender,GridViewRowEventArgs e)
{

ListItem lt = new ListItem();
// DataRowView drv = e.Row.DataItem as DataRowView;
< span class =code-keyword> 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();

}
}
}





 受保护  void  GridView1_PageIndexChanged( object  sender,EventArgs e)
{
string 方向,sortExp ;
// PageSet = 1;
if (Set == 1
{
direction = Session [ direction]。ToString();
sortExp = Session [ sortExp]。ToString();
ShowSortGrid(sortExp,direction);
}
else
ShowGrid();
}

解决方案

当用户点击编辑时,我维护了一个id列(用户不可见)列值基于该id列的行。


我遇到了同样的问题:



使用RowCommand按钮我捕获了所选的rowindex(e.CommandArgument),但这引用了PageIndex 0的选定索引。



我要解决的问题是记住所选每当我更改页面时,在Session变量中的页面(Grid.PageIndex)(在Grid.PageIndexChanged事件中,在Page_Load中以0开始)。

当我想要选择的rowdata时,我设置了Grid.PageIndex到Session变量并用Grid.DataBind确认。



 index = Convert.ToInt32(e.CommandArgument)
如果 IsNothing(会话( page))然后
Gridview.PageIndex = Session( page
Gridview.DataBind()
结束 如果
Dim selectedRow As GridViewRow = Gridview。行(索引)
Dim IDCell As TableCell = selectedRow.Cells( 0 ' 单元格(0)是需要的ID
strDesiredValue = IDCell.Text



也许不是最好的解决方案,但为我工作..


< small>< small>< small> < /   small  >  < /   small  >  < /   small  >  

将数据表添加到网格并基于更新和删除操作数据表行数...





 DataTable dt =(DataTable)会话[  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;
}
}


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:

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


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();
                    
                }
            }
        }



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();
       }

解决方案

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.


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")
    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..


<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..


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;
               }
            }


这篇关于如何获取gridview [paged]行的行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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