This question is asked here many times but their answers are not working for me.
I am using C# DataGridView to show the data in tabular form. I have some 8/9 columns and around 25 to 30 rows. For each column, I am setting customized width (Some percentage of total screen width and it is different for each column). To achieve it I have set following property
view1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
Also, since my application is such that I can't show all the rows at same time but the no. of rows defined by user at a time and for that I am using scrollindex property in timer to scroll view. Also I have customized row height (Some percentage of total screen height). So to do not show all the rows at same time I am using following property
view1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
For wrapping I am using
view1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
But the above property wraps the only words which have space in between but not the single long word.
So how to wrap the single long word with above row and column size properties? In lots of places I am getting advice to use
view1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
view1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
But whenever I use anything other than 'none' in AutoSizeColumnsMode my column's customized width gets changed and if I use anything other than 'none' in AutoSizeRowsMode , it shows all the rows at a time and my row's customized height gets changed.
Anybody knows how to solve this problem?
Note: It is a desktop application (Windows Form Application).
Use DataGridView CellPainting event. Sample code:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
//dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
//dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
//dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[0].Value = "CELL123456789012345679801234567890";
row.Cells[1].Value = "CELL 1";
row.Cells[2].Value = "CELL 2";
row.Cells[3].Value = "CELL 3";
row.Cells[4].Value = "CELL 4";
row.Cells[5].Value = "CELL 5";
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.Value == null)
return;
var s = e.Graphics.MeasureString(e.Value.ToString(), dataGridView1.Font);
if (s.Width > dataGridView1.Columns[e.ColumnIndex].Width)
{
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
e.Graphics.DrawString(e.Value.ToString(), dataGridView1.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault);
dataGridView1.Rows[e.RowIndex].Height = (int)(s.Height * Math.Ceiling(s.Width / dataGridView1.Columns[e.ColumnIndex].Width));
e.Handled = true;
}
}
}
Improved code:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.Value == null)
return;
var s = e.Graphics.MeasureString(e.Value.ToString(), dataGridView1.Font);
if (s.Width > dataGridView1.Columns[e.ColumnIndex].Width)
{
using (Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Graphics.DrawString(e.Value.ToString(), dataGridView1.Font, Brushes.Black, e.CellBounds, StringFormat.GenericDefault);
dataGridView1.Rows[e.RowIndex].Height = (int)(s.Height * Math.Ceiling(s.Width / dataGridView1.Columns[e.ColumnIndex].Width));
e.Handled = true;
}
}
}
}
Try something like
view.Columns[int_Desired_Column_Index].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With