C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

DataGridView Column Header Text with AutoEllipses(...) in C#


Posted Date: 20 Oct 2008    Resource Type: Code Snippets    Category: DataGridView
Author: Saravanan KanagarasuMember Level: Gold    
Rating: 1 out of 5Points: 10



This code for Show DataGridView Column Header Name with "..."(ie. Append Column Name with "...") while resize the column


private void dgvEmployee_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs eventArgs)
{
DataGridViewColumn currentColumn = eventArgs.Column;
int columnWidth = currentColumn.Width;
int columnWidthDifference = (currentColumn.HeaderCell.SortGlyphDirection
!= System.Windows.Forms.SortOrder.None) ? 26 : 10;
string continueOperator = "...";
string columnName = currentColumn.Name;
string headerWithSingleChar = columnName.Substring(0, 1) + continueOperator;
string headerText;

int charRemove = 2;
while (currentColumn.HeaderCell.ContentBounds.Width > columnWidth - columnWidthDifference)
{
if (columnName.Length != charRemove)
{
headerText = columnName.Substring(0, (columnName.Length - charRemove)) + continueOperator;
SetColumnHeader(currentColumn, headerText);
}
else
{
headerText = (columnWidthDifference != 10) ? string.Empty : headerWithSingleChar;
SetColumnHeader(currentColumn, headerText);
return;
}

charRemove++;
}

int charShow = 1;
while (currentColumn.HeaderCell.ContentBounds.Width < columnWidth - columnWidthDifference)
{
if (columnName.Length != charShow)
{
headerText = columnName.Substring(0, charShow) + continueOperator;
SetColumnHeader(currentColumn, headerText);
}
else
{
SetColumnHeader(currentColumn, columnName);
return;
}

if (currentColumn.HeaderCell.ContentBounds.Width > columnWidth - columnWidthDifference)
{
if (charShow != 1)
{
headerText = columnName.Substring(0, charShow - 1) + continueOperator;
SetColumnHeader(currentColumn, headerText);
}
else
{
headerText = (columnWidthDifference != 10) ? string.Empty : headerWithSingleChar;
SetColumnHeader(currentColumn, headerText);
}
return;
}

charShow++;
}
}

private void SetColumnHeader(DataGridViewColumn currentColumn, string columnHeaderText)
{
dgvEmployee.CellValueChanged -= new DataGridViewCellEventHandler(dgvdgvEmployee_CellValueChanged);
currentColumn.HeaderText = columnHeaderText;
dgvEmployee.CellValueChanged += new DataGridViewCellEventHandler(dgvdgvEmployee_CellValueChanged);
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Resize DataGridView Column header  .  DataGridView column header with "..."  .  DataGridView Column Header Text with AutoEllipses(...) in C#  .  Append DataGridView Column header with "..."  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to Edit,Delete,Update in Datagrid in vs2008?
Previous Resource: Jump to nth row or move up/down row on single click
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use