很多东西,由不知道,到看到,到调试好,经历了太多的搜索,测试,修改,我怕我遗忘,暂记于此。
方法一:
//调用
private void InsertNull()

{
try

{
………………
DataTable Dtab = myDs.Tables[0];
dataGrid1.DataSource =Dtab;

// 把数字值替换成字符串显示
ChangeSomeView(Dtab);

//设定列名 中文显示
SetHeaderName(Dtab.TableName); //处理难点 tablename必须对应才能显示出中文列名
}
catch (Exception err)

{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

/**中文列名****/#region/**中文列名****/

/**//// <summary>
/// 将DataGrid中的列名替换成中文
/// </summary>
private void SetHeaderName(string tablename)

{
//重新定制datagrid1的列名
SetupGrid(this.dataGrid1, m_sAryColName, m_sAryColText,m_sAryColWidth,tablename);
}
#endregion

/***设置表头***/#region/***设置表头***/
private void SetupGrid(DataGrid AGrid,string[] sAryColName,string[] sAryColText,
int[] sAryColWidth,string ATableName)

{
try

{
//列名,列题头,列宽
int iCount=sAryColText.Length; //sAryColName.Length;
//
DataGridTableStyle tblStyle= new DataGridTableStyle();
tblStyle.MappingName= ATableName;
DataGridTextBoxColumn[] fldStyleAry=new DataGridTextBoxColumn[iCount];
for(int i=0;i<fldStyleAry.Length;i++)

{
fldStyleAry[i]=new DataGridTextBoxColumn();
}
tblStyle.GridColumnStyles.AddRange(fldStyleAry);
AGrid.TableStyles.Clear();
AGrid.TableStyles.Add(tblStyle);
tblStyle.DataGrid=AGrid;
tblStyle.ReadOnly=true;
for(int i=0;i<sAryColName.Length;i++)

{
DataGridTextBoxColumn NumberColumn = fldStyleAry[i];
NumberColumn.MappingName = sAryColName[i];
NumberColumn.HeaderText = sAryColText[i];
NumberColumn.Width = AGrid.Width/iCount; //平均分配 原来 =sAryColWidth[i];
}
}
catch

{
throw;
}
}
//数据库中的字段
string [] m_sAryColName=new String[]

{
"ID","Type", "Customer_name","Sex","Identity_card' ……………… };
//对应到Grid上的中文名称
string[] m_sAryColText=new String[]

{
"顺序号", "类型","客户姓名","性别", "身份证号 ………… };
//显示的宽度
int[] m_sAryColWidth=new int[]

{
20,20,100,10,150,150,150,150,150,150,200,150,200,150 //乱给的
};
#endregion

方法二:select ID as 顺序号 ,cstr(Type) as 类型 …… 完成列名显示中文的功能。
另外的一个小技巧 :select ID ,cstr(Type) as Type 将Type 由INT 提取的时候cstr,然后才能完成替换后的回写

/**//// <summary>
/// 把数字值替换成字符串显示 1---学生表
/// </summary>
/// <param name="Dtab">DataTable Dtab</param>
private void ChangeSomeView(DataTable Dtab)

{
foreach(DataRow Row in Dtab.Rows)

{
object IDType=Row[1];
string operStr=IDType.ToString();
switch(operStr)

{
case "1" :
Row[1] = "学生表" ;
break;
…… }
}
}