C1FlexGrid For Winforms has always been an easy to use grid control with the capability to manipulate Grid Styles. We can easily apply alternate row styling using Alternate property. However, it lacks the ability to maintain the alternate row styles for C1FlexGrid with hidden rows. This situation specifically arrives when C1Flexgrid rows are hidden through code or user applies filter on it. Hence, in this blog we look to overcome this limitation by handling few of the grid events. Implementation has been summarized in the following steps:-
The following code snippet should get the implementation going in your application:-
c1FlexGrid1.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.SingleColumn;
c1FlexGrid1.AllowFiltering = true;
// specifying style
C1.Win.C1FlexGrid.CellStyle cs;
cs = c1FlexGrid1.Styles.Add("alt");
cs.BackColor = Color.Red;
private void btnHideRows_Click(object sender, EventArgs e)
{
// hide some rows from the grid
for (int r = 1; r < c1FlexGrid1.Rows.Count; r += 3)
c1FlexGrid1.Rows[r].Visible = false;
}
private void btnApplyAltStyles_Click(object sender, EventArgs e)
{
// apply alternate row style
ApplyAltStyle();
}
void ApplyAltStyle()
{
// function to apply alternate row style
bool alt = false;
for (int r = 1; r < c1FlexGrid1.Rows.Count; r++)
{
if (c1FlexGrid1.Rows[r].Visible)
{
alt = !alt;
c1FlexGrid1.Rows[r].Style = alt ? c1FlexGrid1.Styles["alt"] : null;
}
}
}
void c1FlexGrid1_AfterSort(object sender, C1.Win.C1FlexGrid.SortColEventArgs e)
{
// apply alternate row style once sorting is done
ApplyAltStyle();
}
void c1FlexGrid1_AfterFilter(object sender, EventArgs e)
{
// apply alternate row style once filtering is done
ApplyAltStyle();
}
Click on the image below to see the working of the above implementation:- Download the samples for complete implementation. DownloadSample_CS