Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I would like to loop through each column of a datagridview control.
If the header contains the word "hello" i.e. "you hello", then it is to be replaced with a new word such as "great"
This is what I have but not sure how to replace
C#
foreach (DataGridViewColumn col in dgv.Columns)
            {
                string strColumn = col.HeaderText.ToString();
                
                if (strColumn.ToLower().Contains("hello"))
                {
			//col.HeaderCell.Value = ""

Any suggestions please?
Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 6-Feb-12 3:33am    
Why? Why not putting it right in first place?
--SA

The property System.Windows.Forms.DataGridViewColumn.HeaderText has the type string, so HeaderText.ToString() in your code is pointless.

Also, this property is read-write; and it makes the whole question pointless. You can assign a string value to it, not just read. This is the solution you wanted.

Please see: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.headertext.aspx[^].

Finally, think at my comment to the question: why? This is a rhetorical question; just think about it. :-)

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 6-Feb-12 6:36am    
My 5
Try:
C#
foreach (DataGridViewColumn col in dgv.Columns)
{
   col.HeaderText = col.HeaderText.Replace("hello", "great");
}
 
Share this answer
 
headercell.value is required to solve this.
Thank you
 
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