![](/img/trans.png)
[英]First row of DataGrid is not finding on ItemDataBound in Asp.net c#
[英]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.