0

I need to set the text alignment for individual column headers in the ASP.net WebGrid. Found this, MVC3 WebGrid header alignment and various other people asking the same question, but no real satisfactory answer.

Not really up for replacing it with another grid unless it looks EXACTLY the same, takes the same parameters etc. Already been through another grid and made it work but the pagination UI controls were different and there was no control over the footer style, or cell style

Up for inheriting from WebGrid and rolling my own - but I am not a CSS/HTML expert AT ALL. IS there an easy way?

3
  • The easiest way is to learn CSS and apply it. Commented Apr 17, 2014 at 6:54
  • Helpful. The issue with the WebGrid is that the does not allow you apply any styling that affects the headers on a column-by-column basis - as anyone familiar with it would know. It's easy enough to apply the same style to all the column headers, but that's not the problem at hand. I said I wasn't an expert - I didn't say I was entirely ignorant. Commented Apr 17, 2014 at 8:04
  • 1
    Fair enough. I hope the reply I made below is more helpful to you. Commented Apr 17, 2014 at 13:59

2 Answers 2

1

You can do what you want via the CSS3 psuedo selector :nth-child. The following will style the 2nd column heading only:

th:nth-child(2){
    color:red;
}

It's not supported in IE 8 so if that is important to you, you can use jQuery to apply the style instead:

$(function () {
    $('th:nth-child(2)').css('color', 'red');
});
Sign up to request clarification or add additional context in comments.

1 Comment

I was trying for a general solution that could be applied to any column, on any grid, on any page. I did come up with my own way...
1

In the end, because I wanted this behaviour for any column in any grid in the web app (and because I am very comfortable with C# and reflection) I sub-classed WebGrid and created our own version which does allow styles to be specified when the grid is set up.

Because of how WebGrid is written, it did involve pulling out a couple of the private methods via a disassembler, so it may not be to everyone's taste, but I now have exactly what I wanted.

1 Comment

For future reference, the source code for the WebGrid is available here: aspnetwebstack.codeplex.com/SourceControl/latest#src/…. Saves having to use Reflector or similar...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.