Hi Probiere mal folgendes aus:
public void SizeColumns(DataGrid myGrid, int xRows, int aWidth)
{
Graphics Graphics = myGrid.CreateGraphics();
DataGridTableStyle tblStyle = new DataGridTableStyle();
DataTable myTable = (DataTable)myGrid.DataSource;
if (xRows == -1)
xRows = myTable.Rows.Count;
else
xRows = System.Math.Min(xRows, myTable.Rows.Count);
myGrid.TableStyles.Clear();
tblStyle.MappingName = myTable.TableName;
DataGridTextBoxColumn colStyle;
int xWidth;
for (int ColCur = 0; ColCur < myTable.Columns.Count; ColCur++)
{
DataColumn dataColumn = myTable.Columns[ColCur];
colStyle = new DataGridTextBoxColumn();
colStyle.TextBox.Enabled = true;
colStyle.HeaderText = dataColumn.ColumnName;
colStyle.MappingName = dataColumn.ColumnName;
xWidth = (int)(Graphics.MeasureString(colStyle.HeaderText, myGrid.Font).Width);
DataRow dataRow;
for (int iRow = 0; iRow < xRows; iRow++)
{
dataRow = myTable.Rows[iRow];
if (dataRow[dataColumn.ColumnName] != null)
{
int iColWidth = Convert.ToInt32((Graphics.MeasureString(dataRow.ItemArray[ColCur].ToString(),myGrid.Font).Width));
xWidth = Convert.ToInt32(System.Math.Max(xWidth, iColWidth));
}
}
colStyle.Width = xWidth +aWidth;
tblStyle.GridColumnStyles.Add(colStyle);
}
myGrid.TableStyles.Add(tblStyle);
Graphics.Dispose();
}
Viele Grüße / with kind regards
Strasser Shinja,
www.netug.de - .net User Group
www.devtrain.de - asp.net Portal
www.ppedv.de - Brötchengeber
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!