C# WinForm中DataGrid列设置(转载)

前端之家收集整理的这篇文章主要介绍了C# WinForm中DataGrid列设置(转载)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

写winForm的程序,难免要用DataGrid,自然也就需要设置列格式啊,标题之类的!但是经常列标题设置后没反应,好恶心!
<p style="FONT-SIZE: 10pt">  这几天做了个程序,自己研究了一下,主要有有一个地方要注意!那就是下面代码中的“红色”字部分!以下代码不需要在控件上做任何设置,照着写就能搞定!以前好象还有人花5000分买这个问题的解决答案的,哎,怎么我没碰上啊!


<div class="code" style="FONT-SIZE: 10pt">
<p style="FONT-SIZE: 10pt">private void frmLog_Load(object sender,System.EventArgs e)  {   //设置DataGrid的列宽   InitDataGridColumnHeader();


<p style="FONT-SIZE: 10pt">   //GetResult();  }


<p style="FONT-SIZE: 10pt">private void InitDataGridColumnHeader()  {   DataGridTableStyle dts=new DataGridTableStyle();


<p style="FONT-SIZE: 10pt">   //注意:必须加上这一句,否则自定义列格式无法使用   dts.MappingName="Table";


<p style="FONT-SIZE: 10pt">   hrgLog.TableStyles.Add(dts);


<p style="FONT-SIZE: 10pt">   hrgLog.TableStyles[0].GridColumnStyles.Clear();


<p style="FONT-SIZE: 10pt">   //========================设置表头栏位===========================   DataGridTableStyle dtsLog = new DataGridTableStyle();         DataGridTextBoxColumn colID = new DataGridTextBoxColumn();   colID.Width=80;    colID.HeaderText = "记录序号";   colID.MappingName = "ID";


<p style="FONT-SIZE: 10pt">   hrgLog.TableStyles[0].GridColumnStyles.Add(colID);        DataGridTextBoxColumn colLog = new DataGridTextBoxColumn();   colLog.Width=200;    colLog.HeaderText = "日志内容";   colLog.MappingName = "LogMessage";


<p style="FONT-SIZE: 10pt">   hrgLog.TableStyles[0].GridColumnStyles.Add(colLog);    


<p style="FONT-SIZE: 10pt">   DataGridTextBoxColumn colTime = new DataGridTextBoxColumn();   colTime.Width=100;    colTime.HeaderText = "记录时间";   colTime.MappingName = "LogTime";


<p style="FONT-SIZE: 10pt">   hrgLog.TableStyles[0].GridColumnStyles.Add(colTime);   


<p style="FONT-SIZE: 10pt">   DataGridTextBoxColumn colCatalog = new DataGridTextBoxColumn();   colCatalog.Width=100;    colCatalog.HeaderText = "日志类别";   colCatalog.MappingName = "LogCatalog";     hrgLog.TableStyles[0].GridColumnStyles.Add(colCatalog);       }

猜你在找的C#相关文章