繁体   English   中英

如何在asp.net 2.0 C#中查找datagrid行

[英]how to find datagrid row in asp.net 2.0 C#

我正在使用dot net 2.0和c#。

我有一个要求,我必须查找datagrid的行,然后将其作为函数中的参数传递,能否请我为datagird中的findind行提供代码。 谢谢

您可以ItemDataBound event on your datagrid尝试使用ItemDataBound event on your datagrid

void Item_Bound(Object sender, DataGridItemEventArgs e) 
{
      if((e.Item.ItemType == ListItemType.Item) || 
             (e.Item.ItemType == ListItemType.AlternatingItem))
         {
             var control = (Label)e.Item.FindControl("YourLabel");
             control.Text="pass your value";
         }
}


<asp:DataGrid id="DataGrid1" 
   runat="server" 
   AutoGenerateColumns="False" OnItemDataBound="Item_Bound">
   <Columns>
      <asp:TemplateColumn HeaderText="Sample">
         <ItemTemplate>
            <asp:Label id="YourLabel" runat="server"/>
         </ItemTemplate>
      </asp:TemplateColumn>
   </Columns>
</asp:DataGrid>

您只需将数据网格放入ItemDataBound并调用事件ItemDataBound_click

string rownumber = e.Item.FindControl("your id for the label") As Label 

并在转换了等效数据类型后使用,您可以将其用作参数。

您可以循环您的gridview / datagrid行,并在满足某些条件时执行所需的任何操作。

    foreach (GridViewRow gvr in gvDemo.Rows) {
        if (gvr.DataItem != null) { 

            //depending on how or what you want to find 
            //check a key
            if (gvDemo.DataKeys[gvr.RowIndex].Values[0] == "xx") { 

            }
            //or a field value
            if (gvr.Cells[0].ToString() == "123") { 

            }

            //or first cast your row data item back to a known class
            CarBrand carBrand = (CarBrand)gvr.DataItem;
            if (carBrand.Name == "Porsche") { 
            //
            }

            //or pass the whole row to whatever function
            if (xx == yy) {
                DoSomething(gvr);
            }

        }
    }

暂无
暂无

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

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