更改数据表列中每个单元格的值
我有一个 System.Data.DataTable,我将其绑定到 GridView。如果用户没有特定的安全角色,我想用“X”替换某些列中的数据。我可以在不循环所有行的情况下执行此操作吗?
I have a System.Data.DataTable which I'm binding to a GridView. If the user doesn't have a certain security role, I want to replace the data in certain columns with an "X". Can I do this without looping through all of the rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果用户的安全角色不充分,则实现 GridView 的 RowDataBound 事件并在其中进行更改。
Implement the GridView's RowDataBound event and make the change in there if the user's security role is inadequate.
严格来说,不。您可以使用 Linq 样式的扩展方法来隐藏实现,但过程语言中的任何代码都将涉及行的迭代循环。甚至 RowDataBound 事件也是由每行迭代触发的;但是,无论如何它都会遍历行,所以至少您没有重复循环行为。
Strictly speaking, no. You can use Linq-style extension methods to hide the implementation, but any code in a procedural language is going to involve an iterative loop through the rows. Even the RowDataBound event is fired iteratively by each row; however, it's iterating through the rows anyway, so at least you're not duplicating the looping behavior.
您也可以在后端执行此操作。将 userID 或角色传递给 sp,该 sp 只返回可查看的列和他们看不到的列的“X”值,并将 UI 绑定到该列。
You could also do it on the back end. Pass the userID or role to an sp which returns only viewable columns and 'X' values for columns they can't see and bind the UI to that.
您可以简单地提供第二列,并将其命名为“您的列2”
填充 x 数据,然后您可以根据管理员访问级别确定哪一列可见。
在 VB.NET 中,如果没有递归,则无法更改每一行的值。
You could simply have a second column available and ready name it "your column2"
filled with x data, you then determine which column is visible based on admin access level.
In VB.NET you cannot change values of each row without recursion.