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?
Implement the GridView's RowDataBound event and make the change in there if the user's security role is inadequate.
void CusomGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(User.Role != "Admin")
{
e.Row.Cells[1].Text = "X";
}
}
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.
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.
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.