加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:39:04 所属栏目:百科 来源:网络整理
导读:写winForm的程序,难免要用DataGrid,自然也就需要设置列格式啊,标题之类的!但是经常列标题设置后没反应,好恶心! p style="FONT-SIZE: 10pt" 这几天做了个程序,自己研究了一下,主要有有一个地方要注意!那就是下面代码中的“红色”字部分!以下代码不

写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);???? ? }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读