给DataGrid设置中文列名

很多东西,由不知道,到看到,到调试好,经历了太多的搜索,测试,修改,我怕我遗忘,暂记于此。
  方法一:
 //调用 
 
None.gif private   void   InsertNull()
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif       ………………
InBlock.gif    DataTable Dtab 
= myDs.Tables[0];
InBlock.gif    
InBlock.gif    dataGrid1.DataSource 
=Dtab;
InBlock.gif
InBlock.gif    
// 把数字值替换成字符串显示
InBlock.gif
    ChangeSomeView(Dtab);
InBlock.gif
InBlock.gif    
//设定列名 中文显示
InBlock.gif
    SetHeaderName(Dtab.TableName);   //处理难点  tablename必须对应才能显示出中文列名
InBlock.gif
 
ExpandedSubBlockEnd.gif  }

InBlock.gif   
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    MessageBox.Show(
this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif   }

InBlock.gif  
ExpandedBlockEnd.gif  }

None.gif   
ContractedBlock.gifExpandedBlockStart.gif
/**中文列名****/ #region/**中文列名****/
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 将DataGrid中的列名替换成中文
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private void SetHeaderName(string tablename)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//重新定制datagrid1的列名   
InBlock.gif
   SetupGrid(this.dataGrid1, m_sAryColName, m_sAryColText,m_sAryColWidth,tablename);   
InBlock.gif    
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  
#endregion

None.gif 
ContractedBlock.gifExpandedBlockStart.gif  
/***设置表头***/ #region/***设置表头***/
InBlock.gif  
private void SetupGrid(DataGrid AGrid,string[] sAryColName,string[] sAryColText,
InBlock.gif  
int[] sAryColWidth,string  ATableName)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif   
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{   
InBlock.gif    
//列名,列题头,列宽   
InBlock.gif
    int   iCount=sAryColText.Length;        //sAryColName.Length;   
InBlock.gif    
//   
InBlock.gif
    DataGridTableStyle   tblStyle= new   DataGridTableStyle();   
InBlock.gif    tblStyle.MappingName
= ATableName;   
InBlock.gif    DataGridTextBoxColumn[]   fldStyleAry
=new   DataGridTextBoxColumn[iCount];   
InBlock.gif    
for(int   i=0;i<fldStyleAry.Length;i++)   
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{   
InBlock.gif     fldStyleAry[i]
=new DataGridTextBoxColumn();   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif    tblStyle.GridColumnStyles.AddRange(fldStyleAry);   
InBlock.gif    
InBlock.gif    AGrid.TableStyles.Clear();   
InBlock.gif    AGrid.TableStyles.Add(tblStyle);   
InBlock.gif    tblStyle.DataGrid
=AGrid;   
InBlock.gif    tblStyle.ReadOnly
=true;   
InBlock.gif    
InBlock.gif    
for(int i=0;i<sAryColName.Length;i++)   
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{   
InBlock.gif     DataGridTextBoxColumn   NumberColumn   
=   fldStyleAry[i];   
InBlock.gif     NumberColumn.MappingName   
=   sAryColName[i];   
InBlock.gif     NumberColumn.HeaderText   
=   sAryColText[i];     
InBlock.gif     NumberColumn.Width   
= AGrid.Width/iCount;   //平均分配   原来 =sAryColWidth[i];
ExpandedSubBlockEnd.gif
    }
   
ExpandedSubBlockEnd.gif   }
   
InBlock.gif   
catch   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{   
InBlock.gif    
throw;   
ExpandedSubBlockEnd.gif   }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
//数据库中的字段   
InBlock.gif
  string [] m_sAryColName=new   String[]     
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{   
InBlock.gif  
"ID","Type""Customer_name","Sex","Identity_card' ……………… };   
InBlock.gif
    
InBlock.gif  
//对应到Grid上的中文名称   
InBlock.gif
  string[]   m_sAryColText=new   String[]     
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{   
InBlock.gif  
"顺序号""类型","客户姓名","性别""身份证号 ………… };   
InBlock.gif
    
InBlock.gif  
//显示的宽度   
InBlock.gif
  int[]   m_sAryColWidth=new   int[]     
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{   
InBlock.gif  
20,20,100,10,150,150,150,150,150,150,200,150,200,150     //乱给的
ExpandedSubBlockEnd.gif
 }
;  
ExpandedBlockEnd.gif  
#endregion

None.gif
 
方法二:select ID as 顺序号 ,cstr(Type) as  类型     ……  完成列名显示中文的功能。
 
另外的一个小技巧 :select ID ,cstr(Type) as Type   将Type 由INT  提取的时候cstr,然后才能完成替换后的回写 
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif  
/// 把数字值替换成字符串显示  1---学生表     
InBlock.gif  
/// </summary> 
ExpandedBlockEnd.gif  
/// <param name="Dtab">DataTable Dtab</param>

None.gif    private   void  ChangeSomeView(DataTable Dtab)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
foreach(DataRow Row in Dtab.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
object IDType=Row[1];
InBlock.gif    
string operStr=IDType.ToString();
InBlock.gif    
switch(operStr)       
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{         
InBlock.gif     
case   "1"  : 
InBlock.gif      Row[
1]  = "学生表"  ;
InBlock.gif      
break;
ExpandedSubBlockEnd.gif  ……    }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }
 

转载于:https://www.cnblogs.com/flashicp/archive/2007/04/02/697065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值