SPGridView and Right-aligning Header-text
·157 words·1 min
SPGridView is a really nice control. One of the problems I have encountered with it, is that when you set a column to show its header-text right-aligned, you can wait all you want, it will not happen.
Did some more searching in the internet today, and found a solution I based my final implementation on.
First, create a private generic List if integers for the application :
List<int> rightAlignCellsGridView = new List<int>() { };
Now, create the columns and set HeaderStyle.HorizontalAlign
to HorizontalAlign.Right
for the columns where you want the headertext right-aligned.
Next, after adding the SPGridView to the controls-collection, use the following:
foreach (DataControlField col in this.gridView.Columns.OfType<DataControlField>().Where(c =>
c.HeaderStyle.HorizontalAlign == HorizontalAlign.Right && c.Visible == true))
{
this.rightAlignColGridView.Add(this.gridView.Columns.IndexOf(col));
}
Next, add an event to the SPGridView’s RowDataBound-event and add the following code there:
switch (e.Row.RowType)
{
case DataControlRowType.Header:
foreach (int idx in rightAlignColGridView)
{
e.Row.Cells[idx].Style.Add("text-align", "right");
}
break;
default:
break;
}
Now, compile and watch the results…