以下通过两个比较简单的实例来说明如何设置Windows.Forms.DataGrid的列标题和列宽:
实例1:
1
//设置标题
2
SetDataTableHeader(table2);
3
dgManifestInfor.DataSource = table2;
4
5
DataGridTableStyle dgtStyle = new DataGridTableStyle();
6
dgtStyle.MappingName = "Table1"; //必须对应关联的DataTable的名称,这里因为table2的TableName是Table1
7
8
dgManifestInfor.TableStyles.Add(dgtStyle);
9
10
dgtStyle.GridColumnStyles["容器号"].Width = 100; //对应关联的Datable的列名
11
dgtStyle.GridColumnStyles["运单前缀"].Width = 70;
12
dgtStyle.GridColumnStyles["运单号"].Width = 100;
13
dgtStyle.GridColumnStyles["件数"].Width = 40;
14
dgtStyle.GridColumnStyles["重量"].Width = 70;
15
dgtStyle.GridColumnStyles["品名"].Width = 100;
16
dgtStyle.GridColumnStyles["代理人"].Width = 50;
17
18
private void SetDataTableHeader(DataTable table)
19
{
20
table.Columns["Container"].ColumnName = "容器号";
21
table.Columns["Awb_Prefix"].ColumnName = "运单前缀";
22
table.Columns["Awb_No"].ColumnName = "运单号";
23
table.Columns["PC"].ColumnName = "件数";
24
table.Columns["Weight"].ColumnName = "重量";
25
table.Columns["Goods"].ColumnName = "品名";
26
table.Columns["AgentCode"].ColumnName = "代理人";
27
//table.Columns["Remarks"].ColumnName = "备注";
28
29
}
30
31
32
//设置标题2
SetDataTableHeader(table2);3
dgManifestInfor.DataSource = table2;4

5
DataGridTableStyle dgtStyle = new DataGridTableStyle();6
dgtStyle.MappingName = "Table1"; //必须对应关联的DataTable的名称,这里因为table2的TableName是Table17

8
dgManifestInfor.TableStyles.Add(dgtStyle);9

10
dgtStyle.GridColumnStyles["容器号"].Width = 100; //对应关联的Datable的列名11
dgtStyle.GridColumnStyles["运单前缀"].Width = 70;12
dgtStyle.GridColumnStyles["运单号"].Width = 100;13
dgtStyle.GridColumnStyles["件数"].Width = 40;14
dgtStyle.GridColumnStyles["重量"].Width = 70;15
dgtStyle.GridColumnStyles["品名"].Width = 100;16
dgtStyle.GridColumnStyles["代理人"].Width = 50;17

18
private void SetDataTableHeader(DataTable table)19
{20
table.Columns["Container"].ColumnName = "容器号";21
table.Columns["Awb_Prefix"].ColumnName = "运单前缀";22
table.Columns["Awb_No"].ColumnName = "运单号";23
table.Columns["PC"].ColumnName = "件数";24
table.Columns["Weight"].ColumnName = "重量";25
table.Columns["Goods"].ColumnName = "品名";26
table.Columns["AgentCode"].ColumnName = "代理人";27
//table.Columns["Remarks"].ColumnName = "备注";28

29
}30

31

32

实例2:
1
public void SizeColumnsToContent(DataGrid dataGrid, int nRowsToScan)
2
{
3
// Create graphics object for measuring widths.
4
Graphics Graphics = dataGrid.CreateGraphics();
5
6
// Define new table style.
7
DataGridTableStyle tableStyle = new DataGridTableStyle();
8
9
try
10
{
11
DataTable dataTable = (DataTable)dataGrid.DataSource;
12
13
if (-1 == nRowsToScan)
14
{
15
nRowsToScan = dataTable.Rows.Count;
16
}
17
else
18
{
19
// Can only scan rows if they exist.
20
nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);
21
}
22
23
// Clear any existing table styles.
24
dataGrid.TableStyles.Clear();
25
26
// Use mapping name that is defined in the data source.
27
tableStyle.MappingName = dataTable.TableName; //the mapping name must be the same as table name of the data source
28
29
// Now create the column styles within the table style.
30
DataGridTextBoxColumn columnStyle;
31
int iWidth;
32
33
34
35
for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++)
36
{
37
DataColumn dataColumn = dataTable.Columns[iCurrCol];
38
columnStyle = new DataGridTextBoxColumn();
39
40
columnStyle.TextBox.Enabled = true;
41
columnStyle.HeaderText = dataColumn.ColumnName;
42
columnStyle.MappingName = dataColumn.ColumnName;
43
44
// Set width to header text width.
45
iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);
46
47
// Change width, if data width is wider than header text width.
48
// Check the width of the data in the first X rows.
49
DataRow dataRow;
50
for (int iRow = 0; iRow < nRowsToScan; iRow++)
51
{
52
dataRow = dataTable.Rows[iRow];
53
54
if (null != dataRow[dataColumn.ColumnName])
55
{
56
int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);
57
int iColHight = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Height);
58
iWidth = (int)System.Math.Max(iWidth, iColWidth);
59
}
60
}
61
columnStyle.Width = iWidth + 4;
62
63
tableStyle.GridColumnStyles.Add(columnStyle);
64
}
65
// Add the new table style to the data grid.
66
dataGrid.TableStyles.Add(tableStyle);
67
}
68
catch (Exception e)
69
{
70
MessageBox.Show(e.Message);
71
}
72
finally
73
{
74
Graphics.Dispose();
75
}
76
}
public void SizeColumnsToContent(DataGrid dataGrid, int nRowsToScan)2
{3
// Create graphics object for measuring widths. 4
Graphics Graphics = dataGrid.CreateGraphics();5

6
// Define new table style. 7
DataGridTableStyle tableStyle = new DataGridTableStyle();8

9
try10
{11
DataTable dataTable = (DataTable)dataGrid.DataSource;12

13
if (-1 == nRowsToScan)14
{15
nRowsToScan = dataTable.Rows.Count;16
}17
else18
{19
// Can only scan rows if they exist. 20
nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count);21
}22

23
// Clear any existing table styles. 24
dataGrid.TableStyles.Clear();25

26
// Use mapping name that is defined in the data source. 27
tableStyle.MappingName = dataTable.TableName; //the mapping name must be the same as table name of the data source 28

29
// Now create the column styles within the table style. 30
DataGridTextBoxColumn columnStyle;31
int iWidth;32

33

34

35
for (int iCurrCol = 0; iCurrCol < dataTable.Columns.Count; iCurrCol++)36
{37
DataColumn dataColumn = dataTable.Columns[iCurrCol];38
columnStyle = new DataGridTextBoxColumn();39

40
columnStyle.TextBox.Enabled = true;41
columnStyle.HeaderText = dataColumn.ColumnName;42
columnStyle.MappingName = dataColumn.ColumnName;43

44
// Set width to header text width. 45
iWidth = (int)(Graphics.MeasureString(columnStyle.HeaderText, dataGrid.Font).Width);46

47
// Change width, if data width is wider than header text width. 48
// Check the width of the data in the first X rows. 49
DataRow dataRow;50
for (int iRow = 0; iRow < nRowsToScan; iRow++)51
{52
dataRow = dataTable.Rows[iRow];53

54
if (null != dataRow[dataColumn.ColumnName])55
{56
int iColWidth = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Width);57
int iColHight = (int)(Graphics.MeasureString(dataRow.ItemArray[iCurrCol].ToString(), dataGrid.Font).Height);58
iWidth = (int)System.Math.Max(iWidth, iColWidth);59
}60
}61
columnStyle.Width = iWidth + 4;62

63
tableStyle.GridColumnStyles.Add(columnStyle);64
}65
// Add the new table style to the data grid. 66
dataGrid.TableStyles.Add(tableStyle);67
}68
catch (Exception e)69
{70
MessageBox.Show(e.Message);71
}72
finally73
{74
Graphics.Dispose();75
}76
}
其中dataGrid 就是你的dataGrid, nRowsToScan 你传入-1就可以了

浙公网安备 33010602011771号