2012-09-05 95 views
0

如何将GridView中的DataItem“AmountCollected”更改为代码背后的其他内容。我想根据某些条件将DataItem更改为Balance,并且我想知道它是否可以完成?这是使用VS 2005使用C#和HTML。在代码后面更改GridView的DataItem - 可能吗?

在此先感谢!

<asp:TemplateField ItemStyle-Width="70" ItemStyle-HorizontalAlign="Center"> 
<ItemTemplate> 
    $<asp:Label ID="lblTotalCollected" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"AmountCollected") %>'></asp:Label> 
</ItemTemplate> 

回答

0

搭建RowDataBound事件为GridView。该事件将针对数据源中的每个项目运行,并且可以根据您的条件修改每行。

public void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType != DataControlRowType.DataRow) 
    { 
     return; 
    } 

    Label myLabel = e.Row.FindControl("lblTotalCollected") as Label; 
    MyClass myDataItem = e.Row.DataItem as MyClass; 
    if(...) 
    { 
     myLabel.Text = myDataItem.Balance; 
    } 
    else 
    { 
     myLabel.Text = myDataItem.AmountCollected; 
    } 
} 
+0

你能告诉我MyClass在这里到底是什么? – Ram

+0

这是你绑定到你的'GridView'的任何东西。意思是说,你可能调用了一些代码,比如'myGridView.DataSource = myList;',其中'myList'的类型是'List '。 RowDataBound事件中行的'DataItem'是该列表中的单个项目。如果你绑定了一个'DataSet',那么'DataItem'将会是'DataRow'类型。 –

+0

有你..谢谢你的时间! :-) – Ram

相关问题