Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is it possible to expand the datagridview cell height as the user is typing on run time.
Posted
Updated 12-Jul-11 4:08am
v3
Comments
Sergey Alexandrovich Kryukov 12-Jul-11 13:40pm    
Please do not re-post. Communicate on the page of your original question of the topic.
--SA

1 solution

First you have to set particular cell - WrapMode property should be true.
Then you can use Paint function for datagridview.
this function automatically increased row height whatever you typed in cell

This is one of sample paint function.
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
try
{
int mergeindex = ColumnIndex - m_nLeftColumn;
int i;
int nWidth;
int nWidthLeft;
string strText;
Pen pen = new Pen(Brushes.Black);
// Draw the background
graphics.FillRectangle(new SolidBrush(SystemColors.Control), cellBounds);
// Draw the separator for rows
graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);
// Draw the right vertical line for the cell
if (ColumnIndex == m_nRightColumn)
graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom);
// Draw the text
RectangleF rectDest = RectangleF.Empty;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
// Determine the total width of the merged cell
nWidth = 0;
for (i = m_nLeftColumn; i <= m_nRightColumn; i++)
nWidth += this.OwningRow.Cells[i].Size.Width;
// Determine the width before the current cell.
nWidthLeft = 0;
for (i = m_nLeftColumn; i < ColumnIndex; i++)
nWidthLeft += this.OwningRow.Cells[i].Size.Width;
// Retrieve the text to be displayed
strText = this.OwningRow.Cells[m_nLeftColumn].Value.ToString();
rectDest = new RectangleF(cellBounds.Left - nWidthLeft, cellBounds.Top, nWidth, cellBounds.Height);
graphics.DrawString(strText, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
}
 
Share this answer
 
v2
Comments
divesh12 12-Jul-11 10:53am    
Yes its increased but after press Enter.I want just like this comment box in DataGridview.

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