DataGrid column Settings in C # WinForm (column widths/column headings, and so on)

Source: Internet
Author: User
The DataGrid writes WinForm procedure, unavoidably must use the DataGrid, naturally also needs to set the column type Ah, the title and so on!

But often the list of headings does not respond, likes and dislikes!

These days to do a program, their own research, there is a major place to pay attention! That's " Red " in the code below.

Word part! The following code does not need to make any settings on the control, it can be done by writing! It used to be like someone paid 5000 cents.

The answer to this question, hey, how I did not meet Ah!

private void Frmlog_load (object sender, System.EventArgs e)
{
Set the column width of the datagrid
Initdatagridcolumnheader ();

GetResult ();
}

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

Note: You must add this sentence, otherwise the custom column format cannot be used
DTS. Mappingname= "Table";

HRGLOG.TABLESTYLES.ADD (dts);

Hrglog.tablestyles[0]. Gridcolumnstyles.clear ();

======================== Set 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);

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.