简体   繁体   中英

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

I am using dot net 2.0 and c#.

I have a requirement where i have to find rows of datagrid and pass then as an argument in a function, could you please provide me code for findind a row in datagird. Thanks

You can try with 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>

You just put the datagrid to the ItemDataBound and and calling the Event ItemDataBound_click Write this

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

and use after converting the equivalent data type you use this as a parameter.

You could loop your gridview / datagrid rows and do whatever you want when some criteria matches what you are looking for.

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

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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