I want to delete certains rows based on a condition while binding the grid. I am using the below code. But 'item.Visible = false;'
is not working while 'item.Enabled = false;' is working. Any idea?
protected void userGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if (item["UserName"].Text.ToString() == "SuperAdmin")
{
item .Visible = false;
// item.Enabled = false;
}
}
}
5 Answers, 1 is accepted

I used 'item.Display = false;' instead of 'item.Visible= false;' and it did the trick.
But another issue props up. The <AlternatingItemStyle> and <ItemStyle> mess up. If you look at the grid you can see <ItemStyle> applied to consecutive rows and so as <AlternatingItemStyle>.

I have a similar issue.
If I hide the item, then it messes with the row color scheme.
Can I hide the item and still have the alternating row colors?
You can achieve this requirement using the following approach:
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = e.Item
as
GridDataItem;
item.CssClass = item.ItemIndex % 3 == 0 ?
"rgAltRow"
:
"rgRow"
;
}
}
Hope this helps.
You can also examine the sample provided in the following code-library for a client-side approach:
​http://www.telerik.com/support/code-library/quick-search---filtering-rows-depending-on-entered-text-instantly-when-paging-is-disabled#7QZA0lkbHkuCwOZn23V7Hg
​
Regards,
Eyup
Telerik

Unfortunately that does not help. I still get two consecutive rows with the same color.
Since the assignment of row color is based on ItemIndex, it still messes with the color.
Any other ideas?
Please try using the following approach and let me know about the result:
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if
(item.ItemIndex % 3 == 0)
{
item.Display =
false
;
}
}
}
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
GridDataItem[] visibleItems = RadGrid1.Items.OfType<GridDataItem>().Where(x => x.Display).ToArray();
for
(
int
i = 0; i < visibleItems.Length; i++)
{
visibleItems[i].CssClass = i % 2 == 0 ?
"rgRow"
:
"rgAltRow"
;
}
}
Regards,
Eyup
Telerik