我正在使用 dot net 2.0 和 c#。
我有一个要求,我必须找到数据网格的行,然后将其作为函数中的参数传递,您能否提供在 datagird 中查找行的代码。谢谢
你可以试试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>
您可以循环您的 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);
}
}
}
您只需将数据网格放入 ItemDataBound 并调用 Event ItemDataBound_click 写这个
string rownumber = e.Item.FindControl("your id for the label") As Label
并在转换等效数据类型后使用,您将其用作参数。