在.net CF中設置DataGrid中列的寬度
這是我在使用.net精簡框架(2.0)時,編程實現設置DataGrid中列寬度用到的一些方法
Code
1 /**//// <summary>
2 /// 設置DataGrid的TableStyle
3 /// </summary>
4 /// <param name="dataGrid1"></param>
5 private void SetDataGridStyle(DataGrid dataGrid1)
6 {
7 tablestyle = new DataGridTableStyle();
8 tablestyle.MappingName = "Compare";
9 AddCloStyle(tablestyle, "item_no", "ItemNo.");
10 AddCloStyle(tablestyle, "Diff", "Diff.", 30);
11 AddCloStyle(tablestyle, "qty", "Qty", 30);
12 AddCloStyle(tablestyle, "scanQty", "scanQty");
13 AddCloStyle(tablestyle, "shop", "Shop");
14 dataGrid1.TableStyles.Add(tablestyle);
15 }
16 /**//// <summary>
17 /// 向TableStyle中添加ColumnStyle
18 /// </summary>
19 /// <param name="tabS">TableStyle實例</param>
20 /// <param name="mappingName">在數據源中列的名字</param>
21 /// <param name="headerText">設置列的標題</param>
22 /// <param name="width">設置列的寬度</param>
23 private void AddCloStyle(DataGridTableStyle tabS, string mappingName, string headerText, int width)
24 {
25 DataGridColumnStyle cloStyle = new DataGridTextBoxColumn();
26 cloStyle.MappingName = mappingName;
27 cloStyle.HeaderText = headerText;
28 cloStyle.Width = width;
29 tabS.GridColumnStyles.Add(cloStyle);
30 }
31 /**//// <summary>
32 /// 向TableStyle中添加ColumnStyle
33 /// </summary>
34 /// <param name="tabS">TableStyle實例</param>
35 /// <param name="mappingName">在數據源中列的名字</param>
36 /// <param name="headerText">設置列的標題</param>
37 private void AddCloStyle(DataGridTableStyle tabS, string mappingName, string headerText)
38 {
39 DataGridColumnStyle cloStyle = new DataGridTextBoxColumn();
40 cloStyle.MappingName = mappingName;
41 cloStyle.HeaderText = headerText;
42 tabS.GridColumnStyles.Add(cloStyle);
43 }
1 /**//// <summary>
2 /// 設置DataGrid的TableStyle
3 /// </summary>
4 /// <param name="dataGrid1"></param>
5 private void SetDataGridStyle(DataGrid dataGrid1)
6 {
7 tablestyle = new DataGridTableStyle();
8 tablestyle.MappingName = "Compare";
9 AddCloStyle(tablestyle, "item_no", "ItemNo.");
10 AddCloStyle(tablestyle, "Diff", "Diff.", 30);
11 AddCloStyle(tablestyle, "qty", "Qty", 30);
12 AddCloStyle(tablestyle, "scanQty", "scanQty");
13 AddCloStyle(tablestyle, "shop", "Shop");
14 dataGrid1.TableStyles.Add(tablestyle);
15 }
16 /**//// <summary>
17 /// 向TableStyle中添加ColumnStyle
18 /// </summary>
19 /// <param name="tabS">TableStyle實例</param>
20 /// <param name="mappingName">在數據源中列的名字</param>
21 /// <param name="headerText">設置列的標題</param>
22 /// <param name="width">設置列的寬度</param>
23 private void AddCloStyle(DataGridTableStyle tabS, string mappingName, string headerText, int width)
24 {
25 DataGridColumnStyle cloStyle = new DataGridTextBoxColumn();
26 cloStyle.MappingName = mappingName;
27 cloStyle.HeaderText = headerText;
28 cloStyle.Width = width;
29 tabS.GridColumnStyles.Add(cloStyle);
30 }
31 /**//// <summary>
32 /// 向TableStyle中添加ColumnStyle
33 /// </summary>
34 /// <param name="tabS">TableStyle實例</param>
35 /// <param name="mappingName">在數據源中列的名字</param>
36 /// <param name="headerText">設置列的標題</param>
37 private void AddCloStyle(DataGridTableStyle tabS, string mappingName, string headerText)
38 {
39 DataGridColumnStyle cloStyle = new DataGridTextBoxColumn();
40 cloStyle.MappingName = mappingName;
41 cloStyle.HeaderText = headerText;
42 tabS.GridColumnStyles.Add(cloStyle);
43 }
可以在DataGrid中的Paint事件中添加這個方法隱藏行首:
Code
private void dataGrid1_Paint(object sender, PaintEventArgs e)
{
if (dataGrid1.DataSource != null)
{
dataGrid1.RowHeadersVisible = false;
}
}
private void dataGrid1_Paint(object sender, PaintEventArgs e)
{
if (dataGrid1.DataSource != null)
{
dataGrid1.RowHeadersVisible = false;
}
}