当前位置: 移动技术网 > IT编程>开发语言>c# > C#设置WinForm中DataGrid列的方法(列宽/列标题等)

C#设置WinForm中DataGrid列的方法(列宽/列标题等)

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#设置winform中datagrid列的方法。分享给大家供大家参考。具体如下:

写winform的程序,难免要用datagrid,自然也就需要设置列格式啊,标题之类的!但是经常列标题设置后没反应,好恶心!

这几天做了个程序,自己研究了一下,主要有有一个地方要注意!那就是下面代码中dts.mappingname="table"; 这段!以下代码不需要在控件上做任何设置,照着写就能搞定!

private void frmlog_load(object sender, system.eventargs e)
{
  //设置datagrid的列宽
  initdatagridcolumnheader();
  //getresult();
}
private void initdatagridcolumnheader()
{
  datagridtablestyle dts=new datagridtablestyle(); 
  //注意:必须加上这一句,否则自定义列格式无法使用
  dts.mappingname="table"; 
  hrglog.tablestyles.add(dts);
  hrglog.tablestyles[0].gridcolumnstyles.clear();
  //========================设置表头栏位===========================
  datagridtablestyle dtslog = new datagridtablestyle();
  datagridtextboxcolumn colid = new datagridtextboxcolumn();
  colid.width=80; 
  colid.headertext = "记录序号";
  colid.mappingname = "id";
  hrglog.tablestyles[0].gridcolumnstyles.add(colid); 
  datagridtextboxcolumn collog = new datagridtextboxcolumn();
  collog.width=200; 
  collog.headertext = "日志内容";
  collog.mappingname = "logmessage";
  hrglog.tablestyles[0].gridcolumnstyles.add(collog); 
  datagridtextboxcolumn coltime = new datagridtextboxcolumn();
  coltime.width=100; 
  coltime.headertext = "记录时间";
  coltime.mappingname = "logtime";
  hrglog.tablestyles[0].gridcolumnstyles.add(coltime);  
  datagridtextboxcolumn colcatalog = new datagridtextboxcolumn();
  colcatalog.width=100; 
  colcatalog.headertext = "日志类别";
  colcatalog.mappingname = "logcatalog"; 
  hrglog.tablestyles[0].gridcolumnstyles.add(colcatalog);   
}

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网