在使用 DataGrid 時,指定的資料集導入 DataGrid 顯示之後,會發現所有欄位的資料過長的地方都被切掉了,那是因為 DataGrid 並沒有自動對齊的功能,它僅僅是使用 DataGrid 的 width 去除以 column 數量,來作為每個 column 的寬度,挺笨的不是嗎?找了一下 Google,發現了一個非常好用的 funcion 可以簡單的作到 Autofit 的功能 (這個 “Autofit” 是在 Excel 的 Automation 用的 term),該篇文章原始出處在: http://www.windowsforms.com/Forums/ShowPost.aspx?tabIndex=1&tabId=41&PostID=3193 我這邊列出 C# 跟 VB.NET 的兩個不同版本。

[使用範例]

AutoFitDataGrid(DataGrid1, DataGrid1.DataSource, 10)
AutoFitDataGrid(DataGrid2, DataGrid2.DataSource(DataGrid2.DataMember), 5)
AutoFitDataGrid(DataGrid3, myDataTable)
Or
AutoFitDataGrid (DGItems, myDS.Tables[0], 2);

[C# 版本]

private void AutoFitDataGrid(DataGrid oDataGrid, DataTable oData, int nColumnPadding)
{
int nColumnWidth;
int nHeaderWidth;
Graphics g = null;
string strMax = “”;
string strValue;

DataGridTableStyle dgts = new DataGridTableStyle(true);
dgts.MappingName = oData.TableName;
oDataGrid.TableStyles.Clear();
oDataGrid.TableStyles.Add(dgts);

try
{
g = CreateGraphics();

foreach(DataGridColumnStyle oColumnStyle in oDataGrid.TableStyles[0].GridColumnStyles)
{
foreach(DataRow row in oData.Rows)
{
try
{
strValue = row[oColumnStyle.MappingName].ToString().Trim();
if(strMax.Length < strValue.Length)
strMax = strValue;
}
catch
{
strMax = “”;
}
}

nColumnWidth = g.MeasureString(strMax, oDataGrid.Font).ToSize().Width + nColumnPadding;
nHeaderWidth = g.MeasureString(oColumnStyle.HeaderText, oDataGrid.HeaderFont).ToSize().Width + nColumnPadding;

if(nHeaderWidth > nColumnWidth)
oColumnStyle.Width = nHeaderWidth;
else
oColumnStyle.Width = nColumnWidth;

strMax = “”;
}
}
catch
{
}
finally
{
if(g != null)
g.Dispose();
}
}

[VB.NET 版本]

Private Sub AutoFitDataGrid(ByRef oDataGrid As DataGrid, ByRef oData As DataTable, Optional ByVal nColumnPadding As Integer = 0)
Dim oColumnStyle As DataGridColumnStyle
Dim nColumnWidth, nHeaderWidth As Integer
Dim row As DataRow
Dim g As Graphics
Dim strMax As String = “”
Dim strValue As String
Try
‘Use the graphics object to measure the strings based on the DataGrid font.
g = CreateGraphics()
‘AutoFit every column in the DataGrid. Do this by grabbing the ColumnStyle.
For Each oColumnStyle In oDataGrid.TableStyles(0).GridColumnStyles
For Each row In oData.Rows
‘Get the max length string in the DataTable for this column
Try
strValue = Trim(CStr(row(oColumnStyle.MappingName)))
If strMax.Length < strValue.Length Then
strMax = strValue
End If
Catch
strMax = “”
End Try
Next
‘Calculate the width of the data and the header and add the optional column padding
nColumnWidth = g.MeasureString(strMax, oDataGrid.Font).ToSize.Width + nColumnPadding
nHeaderWidth = g.MeasureString(oColumnStyle.HeaderText, oDataGrid.HeaderFont).ToSize.Width + nColumnPadding
‘Set the width of the column to the larger of the two, that way we can always see our HeaderText.
If nHeaderWidth > nColumnWidth Then
oColumnStyle.Width = nHeaderWidth
Else
oColumnStyle.Width = nColumnWidth
End If
strMax = “”
Next
Catch
Finally
If Not g Is Nothing Then
g.Dispose()
End If
End Try
End Sub

最後修改日期: 2004-07-14

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。