求助:如何设定DATAGRID的列名,用WINFORM? 求助:如何设定DATAGRID的列名,用WINFORM?谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 設置DataGrid的列標題 /// </summary> /// <param name="dg">需要設置的DataGrid實例</param> /// <param name="HeaderText">列標題數組</param> public static void SetDataGrid(ref System.Windows.Forms.DataGrid dg,string[] HeaderText) { System.Windows.Forms.DataGridTableStyle ts; FormatDataGridTableStyle(out ts,((DataView)dg.DataSource).Table.TableName,((DataView)dg.DataSource).Table.Columns,HeaderText); dg.TableStyles.Clear(); dg.TableStyles.Add(ts); } 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); } } } c.HeaderText = biaoming.Columns[i],ColumnName; 是不是还存在另外一种方法:创建一个DATASET表,然后对他格式? anyway 谢谢大家!一会就给分!我先测试一下! 登录问题,谢谢大侠们 有关C/S结构程式开发设计问题 关于WEB里如何在DATAGRID操作CHECKBOX winform怎么设置一个button控制启动停止 关于C# MP3 专辑图片问题 新手问题 在C#里 如何通过代码直接 对Windows剪贴板进行操作 请问怎样解决这个关于进程的问题? 在C#中如何取得Oracle中Function类型为Cursor的return值 请教一个数据库的问题 又现datagrid问题 帮忙看下字符搜索的代码
/// 設置DataGrid的列標題
/// </summary>
/// <param name="dg">需要設置的DataGrid實例</param>
/// <param name="HeaderText">列標題數組</param>
public static void SetDataGrid(ref System.Windows.Forms.DataGrid dg,string[] HeaderText)
{
System.Windows.Forms.DataGridTableStyle ts;
FormatDataGridTableStyle(out ts,((DataView)dg.DataSource).Table.TableName,((DataView)dg.DataSource).Table.Columns,HeaderText);
dg.TableStyles.Clear();
dg.TableStyles.Add(ts);
} 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);
}
}
}