SoFunction
Updated on 2025-03-07

C# method to set DataGrid column in WinForm (column width/column title, etc.)

This article describes the method of setting the DataGrid column in WinForm in C#. Share it for your reference. The details are as follows:

When writing winForm programs, you will inevitably need to use DataGrid, so naturally you need to set column formats, titles, etc.! But it often doesn’t respond after listing the title and setting it, which is so disgusting!

I have done a program these days and studied it myself. There is one thing to pay attention to! That is the following code ="Table"; this paragraph! The following code does not require any settings on the control, you can do it by writing it!

private void frmLog_Load(object sender,  e)
{
  //Set the column width of DataGrid  InitDataGridColumnHeader();
  //GetResult();
}
private void InitDataGridColumnHeader()
{
  DataGridTableStyle dts=new DataGridTableStyle(); 
  //Note: This sentence must be added, otherwise the custom column format will not be used  ="Table"; 
  (dts);
  [0].();
  //=============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================  DataGridTableStyle dtsLog = new DataGridTableStyle();
  DataGridTextBoxColumn colID = new DataGridTextBoxColumn();
  =80; 
   = "Record number";
   = "ID";
  [0].(colID); 
  DataGridTextBoxColumn colLog = new DataGridTextBoxColumn();
  =200; 
   = "Log content";
   = "LogMessage";
  [0].(colLog); 
  DataGridTextBoxColumn colTime = new DataGridTextBoxColumn();
  =100; 
   = "Record time";
   = "LogTime";
  [0].(colTime);  
  DataGridTextBoxColumn colCatalog = new DataGridTextBoxColumn();
  =100; 
   = "Log Category";
   = "LogCatalog"; 
  [0].(colCatalog);   
}

I hope this article will be helpful to everyone's C# programming.