C Method for Setting DataGrid Column in WinForm of Column Width and Column Header etc

  • 2021-07-10 20:35:02
  • OfStack

This article illustrates how C # sets the DataGrid column in WinForm. Share it for your reference. The details are as follows:

Write winForm program, it is inevitable to use DataGrid, naturally also need to set column format ah, title and so on! However, there is no response after setting the title frequently, which is disgusting!

These days, I made a program and studied it myself. There is mainly one place to pay attention to! That is dts. MappingName= "Table" in the following code; This paragraph! The following code does not need to do any settings on the control, and it can be done according to writing!


private void frmLog_Load(object sender, System.EventArgs e)
{
  // Settings DataGrid Column width of 
  InitDataGridColumnHeader();
  //GetResult();
}
private void InitDataGridColumnHeader()
{
  DataGridTableStyle dts=new DataGridTableStyle(); 
  // Note: This must be added 1 Sentence, or the custom column format cannot be used 
  dts.MappingName="Table"; 
  hrgLog.TableStyles.Add(dts);
  hrgLog.TableStyles[0].GridColumnStyles.Clear();
  //======================== Set the header field ===========================
  DataGridTableStyle dtsLog = new DataGridTableStyle();
  DataGridTextBoxColumn colID = new DataGridTextBoxColumn();
  colID.Width=80; 
  colID.HeaderText = " Record serial number ";
  colID.MappingName = "ID";
  hrgLog.TableStyles[0].GridColumnStyles.Add(colID); 
  DataGridTextBoxColumn colLog = new DataGridTextBoxColumn();
  colLog.Width=200; 
  colLog.HeaderText = " Log content ";
  colLog.MappingName = "LogMessage";
  hrgLog.TableStyles[0].GridColumnStyles.Add(colLog); 
  DataGridTextBoxColumn colTime = new DataGridTextBoxColumn();
  colTime.Width=100; 
  colTime.HeaderText = " Record time ";
  colTime.MappingName = "LogTime";
  hrgLog.TableStyles[0].GridColumnStyles.Add(colTime);  
  DataGridTextBoxColumn colCatalog = new DataGridTextBoxColumn();
  colCatalog.Width=100; 
  colCatalog.HeaderText = " Log category ";
  colCatalog.MappingName = "LogCatalog"; 
  hrgLog.TableStyles[0].GridColumnStyles.Add(colCatalog);   
}

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


Related articles: