Click here to Skip to main content
15,910,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, greetings from Brazil! I'm a beginner programmer developing a C# WinForms solution in Visual Studio 2015. I have a DataGridView with 35 columns and I was wondering how to programatically do a FOR loop to set the header text alignment to center only from column 4 through 20. I know how to do it column by column but I'm sure there must be a faster / smarter way to do it. See below the code I'm using. Thank you very much for your time and help!

What I have tried:

alunos_detDataGridView.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;

alunos_detDataGridView.Columns[5].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;

and so forth ...
Posted
Updated 4-Jun-17 15:41pm

1 solution

The documentation for the DataGridView is available at; MSDN - DataGridView Class
[^]
By reading this you can learn about the control, the available constructors, methods, properties and events. The documentation will typically contain examples and "how-to" information
This is how you learn to become a developer - by reading and learning.
Please don't think I am being rude or nasty but the saying if you give a man a fish... is how most developers will see this question.
Making a loop is not hard - link to documentation; for Keyword (C# Reference)[^] - you need an initializer, a condition and an iterator
You should therefore using something like;
C#
for(int initializer = 0; initializer < myGrid.<Property>; initializer++)
{
    // do work
}


Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900