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

 【转自】 http://hi.baidu.com/motiansen/blog/item/8191b3ef6cb155eace1b3e99.html

 

private void frmLog_Load(object sender, System.EventArgs e)
{
   //设置DataGrid的列宽
   InitDataGridColumnHeader();

   //GetResult();
}

private void InitDataGridColumnHeader()
{
   DataGridTableStyle dts=new DataGridTableStyle();

   //注意:必须加上这一句,否则自定义列格式无法使用,"Table"为表名,如果你的表名为"OrdersList",则写"OrdersList"
   dts.MappingName="Table";
   hrgLog.TableStyles.Add(dts);
   hrgLog.TableStyles[0].GridColumnStyles.Clear();

   //========================设置表头栏位===========================
   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);    
}

你可能感兴趣的:(0016-00507,C#.net,datagrid,winform,c#,table,object)