简体   繁体   English

在行数据绑定事件中获取gridview行的最佳方法?

[英]Best way to fetch gridview row on row databound event?

Hi I was just wondering what is the best way to fetch gridview data using row databound event of gridview. 嗨,我只是想知道使用gridview的行数据绑定事件来获取gridview数据的最佳方法是什么。 I am previously used to Eval but read its not recommended as it uses reflection. 我以前习惯了Eval,但不建议阅读,因为它使用了反射。

What do you mean "Fetch" the data, by the time the the RowDataBound event has triggered, there already has to be a row of data, that is why the event has executed. 在RowDataBound事件触发时,您是什么意思“获取”数据,已经必须有一行数据,这就是事件已执行的原因。 If you want to access and mapipulate the data then it is in e.Row.DataItem . 如果要访问和映射数据,则该数据位于e.Row.DataItem

Edit 编辑

To answer your comment, using Eval in the markup and putting code in the RowDataBound event handler and accessing e.Row.DataItem tend to be used under different cirumstances. 为了回答您的意见,在标记中使用Eval并将代码放入RowDataBound事件处理程序中并访问e.Row.DataItem往往会在不同情况下使用。 If all you want to do is take the data and bind it to the property of a control then using Eval() (or Bind() for that matter) is fine. 如果您只想获取数据并将其绑定到控件的属性,则可以使用Eval()(或Bind())。 However if you want to do something more complex then you might need to do it in the RowDataBound event handler. 但是,如果要执行更复杂的操作,则可能需要在RowDataBound事件处理程序中执行此操作。 For exam-ple you might have a grid of customer accounts and ballances. 例如,您可能有一个客户帐户和余额平衡表。 For customers where thir ballances are overdue you might want to turn the row red to highlight the fact that thier accounts are overdue. 对于过期未付清余额的客户,您可能需要将行改为红色以突出显示其帐户已过期的事实。 You could not do that using Eval or Bind in the markup so you would check e.Row.DataItem in the RowDataBound event handler and then decide whether to change the colour of the row. 您无法在标记中使用Eval或Bind来执行此操作,因此您将在RowDataBound事件处理程序中检查e.Row.DataItem ,然后决定是否更改行的颜色。

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

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