GridView,DataList控件获取当前行的行号

 
 

在用GridView控件时,我们经常会碰到获取当前行的索引,通过索引进行许多操作。例如,可以获得当前行某一个控件元素;设置某一元素的值等等。下面结合实例介绍几种获得GridView当前行索引值的方法。

实例: 
① 目的:获取GridView中RowCommand的当前索引行。 
② 前台页面:在GridView中添加一模版列,里面添加一个LinkButton控件。 
代码: 
<asp:TemplateField HeaderText="操作"> 
<ItemTemplate> 
<asp:LinkButton ID="lbtnQianRu" runat="server" CommandName="QianRu" 
CommandArgument='<%# Eval("Id") %>'>签入</asp:LinkButton> 
<asp:LinkButton ID="lbtnQianChu " runat="server" CommandName="QianChu">签出 </asp:LinkButton> 
</ItemTemplate> 
</asp:TemplateField> 
小提示:如果在后台代码中用e.CommandArgument取值的话,前台代码就必须在按钮中设置CommandArgument的值,值为绑定的数据库字段。如: 
//因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值 
int id = Convert.ToInt32(e.CommandArgument.ToString()); 
③ 在GridView里已经设置了LinkButton为事件处理按钮,将通过以下方法获取索引: 
protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){ 
if (e.CommandName == "QianRu") 
{ 
视频中的方法:DataRowView rowView =(DataRowView) e.Item.DataItem;
      var row=rowView.Row;
      TextBox box=(TextBox)e.Item.FindControl("txtName");

【方法一】

GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值 
注意:运用此方法,需要对GridView的DataKeyNames属性进行设置,此例中设置为主键字段。

【方法二】

GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被选中的索引值 
int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引



此外,还有一些方法可以实现获得当前行索引值。

【方法三】在linkbutton控件的Command事件,利用sender的Parent获取GridView中的当前行。 
protected void lbtnQianChu_Command(object sender, CommandEventArgs e) 
{ 
LinkButton lb = (LinkButton)sender; 
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent; 
GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被选中的索引值 
lbtnQianChu.SelectedIndex = gvr.RowIndex; 
}

【方法四】在linkbutton控件的Click事件,获取GridView中的当前行。

protected void LinkButton1_Click(object sender, EventArgs e) 
{ 
//行号 
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;

}

【方法五】如果在模板列中添加一下DropDownList控件,并开启其AutoPostback属性,在DropDownList 的SelectedIndexChanged事件中,获取GridView中的当前行。 
下面是SelectedIndexChanged事件的代码摘要: 
DropDownList ddl = (DropDownList)sender; 
GridViewRow gvr = (GridViewRow)ddl.NamingContainer; 
int id = int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString()); 
int num = int.Parse(ddl.Text); 
第一句用来获取触发事件的DropDownList控件。 
第二句就利用该控件的NamingContainer属性,获取其容器,也就是GridViewRow对象。 
提示:由于DropDoweList与button不同,无法指定其CommandName,所以,通过用NamingContainer属性来解决问题。 
先来看看微软对该NamingContainer属性的解释: 
获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 Control.ID 属性值的服务器控件。 
ASP.NET Web 应用程序的每一页均包含控件的层次结构。此层次结构与控件是否生成用户可见的 UI 无关。给定控件的命名容器是层次结构中该控件之上的父控件,此父控件实现 INamingContainer 接口。实现此接口的服务器控件为其子服务器控件的 ID 属性值创建唯一的命名空间。

当针对列表 Web 服务器控件(如 Repeater 和 DataList 服务器控件)进行数据绑定时,为服务器控件创建唯一的命名空间尤其重要。当数据源中的多个项创建服务器控件的多个实例,且该服务器控件是重复控件的子级时,命名容器确保这些子控件的每个实例具有不冲突的 UniqueID 属性值。页的默认命名容器是请求该页时生成的 Page 类的实例。 
可以使用此属性确定特定服务器控件所在的命名容器。

【方法六】如果模板列中有CheckBox控件的情况,通过CheckBox1_CheckedChanged事件中,获取GridView中的当前行。 
CheckBox chk = (CheckBox)sender; 
DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent; 
GridViewRow gvr = (GridViewRow)dcf.Parent;

【方法七】

<asp:GridView ID="gvTest" runat="server"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
DisplayIndex : <%# Container.DisplayIndex %> || DataItemIndex : <%# Container.DataItemIndex %><br /> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView>

【方法八】 
控件的ID和Name命名可以如上方法,我需要通过RowCommand()方法判断选中的是哪一列,而要使用这个方法的前提是,e.CommandArgument这么一个属性(首先必须知道在GridView里,行索引是被放在CommandArgument里面的),现在的任务就是获得这么一个属性。查资料可以知道,在创建GridView控件中每一行时,都将引发一个RowCreated事件,借此这么个方法,可以把linkButton所选择的行号写入CommandArgument中。 
protected void gvInfo_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
LinkButton lk1 = (LinkButton)e.Row.FindControl("lkbtn");//LinkButton的ID 
lk1.CommandArgument = e.Row.RowIndex.ToString(); 


protected void gvInfo_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
if (e.CommandName == "ADD")//我LinkButton的CommandName 
{ 
int index = Convert.ToInt32(e.CommandArgument); 
string aa = gvInfo.Rows[index].Cells[1].Text.ToString();//获取当前行列号为一的值,列号从0开始 

}

======================================================================================

gridview获取当前行索引的方法

在用GridView控件时,我们经常会碰到获取当前行的索引,通过索引进行许多操作。例如,可以获得当前行某一个控件元素;设置某一元素的值等等。

======================================================================================

  • 获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton
  • 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设置CommandArgument的值,值为绑定的数据库字段
  • <asp:TemplateField HeaderText="操作">
  •     <ItemTemplate>
  •         <asp:LinkButton ID="LinkButton1" runat="server" CommandName="QianRu" 
  •         CommandArgument='<%# Eval("Id") %>'>签入</asp:LinkButton>  
  •         <asp:LinkButton ID="LinkButton2" runat="server" CommandName="QianChu">签出</asp:LinkButton>
  •     </ItemTemplate>
  • </asp:TemplateField>
  • 后台
  • 在GridView里已经设置了LinkButton为事件处理按钮,将通过以下方法获取索引
  • protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){
  •         if (e.CommandName == "QianRu")
  •     {     //取ID的值方法一   
  •               GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
  •               inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值
  •           //取ID的值方法二   
  •               GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
  •               //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引
  •           int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); 
  •           //取ID的值方法三  
  •           //因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值
  •           int id = Convert.ToInt32(e.CommandArgument.ToString()); 
  •         }
  •     }
  • 还有一种就是我们并不需要知道当前点击的是第几行,可以用以下方法实现要求: 
  • <ItemTemplate> 
  •       <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument=' <%# Eval("field1") %>' 
  •       CommandName="play" Text=' <%# Eval("field2") %>'> </asp:LinkButton> 
  • </ItemTemplate> 
  • 上面这个LinkButton,Text绑定了字段2, CommandArgument绑定了字段1 
  • 那么, 
  • protected  void  GridView1_RowCommand(object  sender,  GridViewCommandEventArgs  e) 
  •     if(e.CommandName="play")
  •     {
  •         LinkButton lb = (LinkButton)e.CommandSource; 
  •         string  a  =  lb.Text;//这里可以获得点击行字段field2的值 
  •         string b = e.CommandArgument;//这里可以获得点击行字段field1的值
  •     }
  • }
  • 或:
  • 如果是使用模板列,可以把数据的任意一列绑定到按钮的CommandArgument,如下: 
  • <asp:TemplateField> 
  • <ItemTemplate> 
  • <asp:Button runat="server" CommandArgument='<%# Eval("id") %>' Text="Button" /> 
  • </ItemTemplate> 
  • </asp:TemplateField> 
  • 一般可以绑定到主键列,这样可以在RowCommand通过e.CommandArgument获取当前行的主键,也便于进行其他操作 
  • 如果是要获取行索引,比较麻烦一点,还是那个Button1,在GridView的RowDataBound事件中如下: 
  • Button btn = (Button)e.Row.FindControl("Button1"); 
  • if (btn != null
  • btn.CommandArgument = e.Row.RowIndex.ToString(); 
  • 这样就可以在RowCommand中通过 int rowId=Convert.ToInt32(e.CommandArgument.ToString()) 获取行索引了

     

    下面结合实例介绍几种获得GridView当前行索引值的方法。

    实例:

    ① 目的:获取GridView中RowCommand的当前索引行。

    ② 前台页面:在GridView中添加一模版列,里面添加一个LinkButton控件。

    代码:

    <asp:TemplateField HeaderText="操作">

                <ItemTemplate>

                        <asp:LinkButton ID="lbtnQianRu" runat="server" CommandName="QianRu" 

                         CommandArgument='<%# Eval("Id") %>'>签入</asp:LinkButton>  

                         <asp:LinkButton ID="lbtnQianChu " runat="server" CommandName="QianChu">签出                      

            </asp:LinkButton>

                        </ItemTemplate>

    </asp:TemplateField>

    小提示:如果在后台代码中用e.CommandArgument取值的话,前台代码就必须在按钮中设置CommandArgument的值,值为绑定的数据库字段。如:

     //因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值

    int id = Convert.ToInt32(e.CommandArgument.ToString()); 

    ③ 在GridView里已经设置了LinkButton为事件处理按钮,将通过以下方法获取索引:

    protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){

     if (e.CommandName == "QianRu")

    {  

    【方法一】

    GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值

     inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值 

    注意:运用此方法,需要对GridView的DataKeyNames属性进行设置,此例中设置为主键字段。

    【方法二】

    GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被选中的索引值

    int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text);  //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引

      }

     }

     

    此外,还有一些方法可以实现获得当前行索引值。

    【方法三】在linkbutton控件的Command事件,利用sender的Parent获取GridView中的当前行。

                       protected void lbtnQianChu_Command(object sender, CommandEventArgs e)

                       {

                        LinkButton lb = (LinkButton)sender;

                                DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;

                                GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被选中的索引值

                                lbtnQianChu.SelectedIndex = gvr.RowIndex;

                       }

    【方法四】在linkbutton控件的Click事件,获取GridView中的当前行。

    protected void LinkButton1_Click(object sender, EventArgs e)

    {

          //行号

          int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex; 

    }

    【方法五】如果在模板列中添加一下DropDownList控件,并开启其AutoPostback属性,在DropDownList 的SelectedIndexChanged事件中,获取GridView中的当前行。

    下面是SelectedIndexChanged事件的代码摘要:

    DropDownList ddl = (DropDownList)sender;

    GridViewRow gvr = (GridViewRow)ddl.NamingContainer;

    int id = int.Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString());

    int num = int.Parse(ddl.Text);

    第一句用来获取触发事件的DropDownList控件。

    第二句就利用该控件的NamingContainer属性,获取其容器,也就是GridViewRow对象。

     

    提示:由于DropDoweList与button不同,无法指定其CommandName,所以,通过用NamingContainer属性来解决问题。

        先来看看微软对该NamingContainer属性的解释:

        获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 Control.ID 属性值的服务器控件。

        ASP.NET Web 应用程序的每一页均包含控件的层次结构。此层次结构与控件是否生成用户可见的 UI 无关。给定控件的命名容器是层次结构中该控件之上的父控件,此父控件实现 INamingContainer 接口。实现此接口的服务器控件为其子服务器控件的 ID 属性值创建唯一的命名空间。

        当针对列表 Web 服务器控件(如 Repeater 和 DataList 服务器控件)进行数据绑定时,为服务器控件创建唯一的命名空间尤其重要。当数据源中的多个项创建服务器控件的多个实例,且该服务器控件是重复控件的子级时,命名容器确保这些子控件的每个实例具有不冲突的 UniqueID 属性值。页的默认命名容器是请求该页时生成的 Page 类的实例。

    可以使用此属性确定特定服务器控件所在的命名容器。

     

    【方法六】如果模板列中有CheckBox控件的情况,通过CheckBox1_CheckedChanged事件中,获取GridView中的当前行。

    CheckBox chk = (CheckBox)sender;

    DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;

    GridViewRow gvr = (GridViewRow)dcf.Parent;

     

    【方法七】

    <asp:GridView ID="gvTest" runat="server">         

            <Columns>

            <asp:TemplateField>

            <ItemTemplate>

            DisplayIndex : <%# Container.DisplayIndex %>  || DataItemIndex : <%# Container.DataItemIndex %><br />

            </ItemTemplate>

            </asp:TemplateField>

            </Columns>

    </asp:GridView>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值