简体   繁体   English

如何在GridView中动态检索行的列索引,ID和行索引

[英]How to dynamically retrieve the column index, id and the row index of a row in gridview

I have a dynamically created grdview with 4 columns and n number of rows. 我有一个动态创建的grdview,其中包含4列和n行。 Each column has a Button control with different Id s. 每列都有一个具有不同IdButton控件。 The control has a Click event. 该控件具有Click事件。 When the event fires I want to find the row index and the cell index or the Id of that button at the run time 事件触发时,我想在运行时找到row索引和cell格索引或该按钮的Id

In this article will explain how to get the ASP.Net GridView Row and its RowIndex client side using JavaScript. 本文将解释如何使用JavaScript获取ASP.Net GridView行及其RowIndex客户端。 I will also explain how we can find the GridView Cells and the controls inside the GridView Template Fields client side using JavaScript. 我还将说明如何使用JavaScript在GridView模板字段客户端中找到GridView单元和控件。

    <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type = "text/javascript">
        function GetSelectedRow(lnk) {
            var row = lnk.parentNode.parentNode;
            var rowIndex = row.rowIndex - 1;
            var customerId = row.cells[0].innerHTML;
            var city = row.cells[1].getElementsByTagName("input")[0].value;
            alert("RowIndex: " + rowIndex + " CustomerId: " + customerId + " City:" + city);
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns = "false" AllowPaging = "true" OnPageIndexChanging = "PageIndexChanging">
        <Columns>
        <asp:BoundField DataField = "CustomerID" HeaderText = "CustomerID" />
        <asp:TemplateField HeaderText = "City">
            <ItemTemplate>
                <asp:TextBox ID="txtCity" runat="server" Text = '<%# Eval("City") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

to find the Index of row u can simply do this 找到行的索引,您可以简单地做到这一点

GridViewRow gvr = e.Row;

or 要么

int index = Convert.ToInt32(e.RowIndex);

and to find the value of the control for example text of the text box u can do this 并找到控件的值,例如文本框的文本,您可以执行此操作

Label lb_ID = (TextBox)GridInvoice.Rows[index].Cells[1].FindControl("Label_ID");

where index is row index and cell[1] is cell index ie if u have 4 cells in your grid then index starts from cell[0]... cell[3]. 其中index是行索引,cell [1]是单元索引,即,如果您的网格中有4个单元,则索引从cell [0] ... cell [3]开始。 Here cell[1] means control is in cell 2 of ur grid . 这里cell [1]表示控制在ur grid的单元2中。 if u have grid values binding form database and you have some unique id like primary key coming also and u are performing opreration on this value in database then From Frond end add this property to gridview ( DatakeyNames="ur primary key or uniqe value comming form database "; and now if u have button or link button control ( or any other control which raises event in grid view then on the event write this code which will automatically capture primary key and row index for u. 如果您有绑定表格数据库的网格值,并且您还拥有一些唯一的ID(例如主键),并且您正在对该数据库中的值执行操作,则从Frond端将此属性添加到gridview(DatakeyNames =“ ur主键或uniqe值格式数据库”;现在,如果您具有按钮或链接按钮控件(或在网格视图中引发事件的任何其他控件,则在事件上编写此代码,它将自动捕获u的主键和行索引。

   protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        GridViewRow gvrows = ((LinkButton)sender).NamingContainer as GridViewRow;
      int32  Key = (Convert.ToInt32(grvCityList.DataKeys[gvrows.RowIndex].Values[0].ToString()));

Now do ur logic here 

    }

hope u will get what u are look for 希望你能得到你想要的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM