1.select field1 as 字段一,field2 as 字段二....from table2.DataGrid-->右键属性生成器-->列-->添加列-->修改页眉文本3.<asp:BoundColumn DataField="employeeid" ReadOnly="True" HeaderText="序列 号"></asp:BoundColumn>
........
........
调试欢乐多
如下
select name 姓名 from person.
另一种自定义DataGrid.
如下:
this.dgtbc1.Format = "";
this.dgtbc1.FormatInfo = null;
this.dgtbc1.HeaderText = "维护编码";
this.dgtbc1.MappingName = "CheckID";
this.dgtbc1.Width = 75;
其实在设计时也可以,你选中DataGrid,然后去属性对话框中选TableStales.右边的...
然后在出现的界面中选择添加,然后再在右边的GridColumnStyles中加字段,其中HeadText就是你想显示的中文名字,MappingName就是你数据库中的英文栏位的名字
1.利用SQL语句
2.直接对DataGrid的属性进行设置。
也就是【snof(snof) 】的方法。
至于第三中方法暂时还没想出来:)
/// <summary>
/// 靜態方法:格式化列樣式模板
/// </summary>
/// <param name="ts">傳出一個列樣式的實例</param>
/// <param name="TableName">表名稱,如果是強類型表就是物理表名稱</param>
/// <param name="dcc">數據列集合</param>
/// <param name="HeaderText">列標題數組</param>
public static void FormatDataGridTableStyle(out System.Windows.Forms.DataGridTableStyle ts,string TableName,System.Data.DataColumnCollection dcc,string[] HeaderText)
{
// 捕獲列數輿列標題個數不對應的錯誤
if(dcc.Count != HeaderText.Length)
{
// Throw exception
GUIException oe = new GUIException("界面層FormatDataGridTableStyle錯誤,要顯示的列數輿給定的標題數不一致");
throw oe;
} ts = new System.Windows.Forms.DataGridTableStyle(); // Define a style
ts.MappingName = TableName; // 邊歷所有的列,並設置標題
for(int i=0;i<dcc.Count;i++)
{
if(dcc[i].DataType == typeof(System.Boolean))
{
System.Windows.Forms.DataGridBoolColumn c = new System.Windows.Forms.DataGridBoolColumn();
c.HeaderText = HeaderText[i];
c.MappingName = dcc[i].ColumnName;
ts.PreferredColumnWidth = 25 * HeaderText[i].Length;
ts.GridColumnStyles.Add(c);
}
else
{
System.Windows.Forms.DataGridTextBoxColumn c = new System.Windows.Forms.DataGridTextBoxColumn();
c.HeaderText = HeaderText[i];
c.MappingName = dcc[i].ColumnName;
ts.PreferredColumnWidth = 25 * HeaderText[i].Length;
ts.GridColumnStyles.Add(c);
}
}
} /// <summary>
/// 靜態方法:格式化列樣式模板
/// </summary>
/// <param name="ts">傳出一個列樣式的實例</param>
/// <param name="TableName">表名稱,如果是強類型表就是物理表名稱</param>
/// <param name="dcc">數據列集合</param>
/// <param name="HeaderText">需要顯示的列標題數組</param>
/// <param name="HideColumnName">需要隱藏的列名數組</param>
public static void FormatDataGridTableStyle(out System.Windows.Forms.DataGridTableStyle ts,string TableName,DataColumnCollection dcc,string[] HeaderText,string[] HideColumnName)
{
// 捕獲列數輿列標題個數不對應的錯誤
if(dcc.Count != HeaderText.Length)
{
// Throw exception
GUIException oe = new GUIException("界面層FormatDataGridTableStyle錯誤,要顯示的列數輿給定的標題數不一致");
throw oe;
} ts = new System.Windows.Forms.DataGridTableStyle(); // Define a style
ts.MappingName = TableName; // 邊歷所有的列,並設置標題
for(int i=0;i<dcc.Count;i++)
{
if(dcc[i].DataType == typeof(System.Boolean))
{
System.Windows.Forms.DataGridBoolColumn c = new System.Windows.Forms.DataGridBoolColumn();
c.HeaderText = HeaderText[i];
c.MappingName = dcc[i].ColumnName;
ts.PreferredColumnWidth = 25 * HeaderText[i].Length;
for(int j=0;j<HideColumnName.Length;j++)
{
if(dcc[i].ColumnName == HideColumnName[j])
{
ts.PreferredColumnWidth = 0;
break;
}
}
ts.GridColumnStyles.Add(c);
}
else
{
System.Windows.Forms.DataGridTextBoxColumn c = new System.Windows.Forms.DataGridTextBoxColumn();
c.HeaderText = HeaderText[i];
c.MappingName = dcc[i].ColumnName;
ts.PreferredColumnWidth = 25 * HeaderText[i].Length;
for(int j=0;j<HideColumnName.Length;j++)
{
if(dcc[i].ColumnName == HideColumnName[j])
{
ts.PreferredColumnWidth = 0;
break;
}
}
ts.GridColumnStyles.Add(c);
}
}
} /// <summary>
/// 靜態方法:設置DataGrid的列標題
/// </summary>
/// <param name="dg">需要設置的DataGrid實例</param>
/// <param name="TableName">表名稱,如果是強類型表就是物理表名稱</param>
/// <param name="dcc">數據列集合</param>
/// <param name="HeaderText">列標題數組</param>
public static void SetDataGrid(ref System.Windows.Forms.DataGrid dg,string TableName,DataColumnCollection dcc,string[] HeaderText)
{
System.Windows.Forms.DataGridTableStyle ts;
FormatDataGridTableStyle(out ts,TableName,dcc,HeaderText);
dg.TableStyles.Clear();
dg.TableStyles.Add(ts);
} /// <summary>
/// 靜態方法:設置DataGrid的列標題
/// </summary>
/// <param name="dg">需要設置的DataGrid實例</param>
/// <param name="TableName">表名稱,如果是強類型表就是物理表名稱</param>
/// <param name="dcc">數據列集合</param>
/// <param name="HeaderText">列標題數組</param>
/// <param name="HideColumnName">需要隱藏的列名數組</param>
public static void SetDataGrid(ref System.Windows.Forms.DataGrid dg,string TableName,DataColumnCollection dcc,string[] HeaderText,string[] HideColumnName)
{
System.Windows.Forms.DataGridTableStyle ts;
FormatDataGridTableStyle(out ts,TableName,dcc,HeaderText,HideColumnName);
dg.TableStyles.Clear();
dg.TableStyles.Add(ts);
}