Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Code Snippets » DataGridView »
DataGridView Column Header Text with AutoEllipses(...) in C#
|
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.
|
|