Datagridview adjust column width and using () Usage

Source: Internet
Author: User

Original post and discussion: http://bbs.bc-cn.net/dispbbs.asp? Boardid = 117 & id = 178684

*/Configure */--------------------------------------------------------------------------------------
*/From: programming ChinaHttp://www.bc-cn.net
*/Author: Shi Fei Yi E-mail: shifeixuan@yeah.com QQ: 591608560
*/Time:-10-18 programming Forum debut
*/Statement: respect the work of the author. Reprinted in this section.
*/Configure */--------------------------------------------------------------------------------------

Recently, I saw a few friends in C # asking the datagridview how to adjust the column width. I made a summary and provided it for your reference.

The datagridview datagrihas an attribute autosizecolumnmode, which has several enumerated values:
Allcells adjusts the column width to fit the content of all cells in the column, including the header cells.
Allcellsexceptheader adjusts the column width to fit the content of all cells in the column, excluding the title cells.
Columnheader adjusts the column width to fit the content of the column title cell.
Displayedcells adjusts the column width to fit the content of all cells in the column of the row displayed on the current screen, including the title cell.
Displayedcellsexceptheader adjusts the column width to fit the content of all cells in the column of the row displayed on the current screen, excluding the title cell.
Fill adjusts the column width so that the width of all columns is filled in the display area of the control. You only need to scroll horizontally to ensure that the column width is greater than the value of datagridviewcolumn. minimumwidth. The relative column width is determined by the attribute value of the relative datagridviewcolumn. fillweight attribute.
The none column width is not automatically adjusted.
The size adjustment of the notset column is inherited from the datagridview. autosizecolumnsmode attribute.

If you want to automatically set the column width, you must set the value of the datagridview to fill.

Next, bind the Data source: This. dgvmain. datasource = ds2.tables [0];

then: This. dgvmain. columns [0]. fillweight = 8; // The relative width of the first column is 8%
This. dgvmain. columns [1]. fillweight = 22; // The relative width of the second column is 22%
This. dgvmain. columns [2]. fillweight = 70; // The relative width of the third column is 70%
Note: The value here is relative to the current total width of the dview. therefore, the effects of form maximization and reduction are different. but the proportion remains unchanged

By the way, how can I set a header for each column:
Set the title field (set columnsheadersvisible to true first)
This. dgvmain. Columns [0]. headertext = "no ";
This. dgvmain. Columns [1]. headertext = "date ";
This. dgvmain. Columns [2]. headertext = "title ";

-----------------------------

We also want to introduce using () in the connection count library.Code. Many friends have never used it. I strongly recommend using. It helps us manage database connections, which is very secure and simple

Private dataset getconn ()
{
String connstr = configurationmanager. connectionstrings ["mysqlconnection"]. connectionstring;
String sqlstr = "select * From userinfo ";
Dataset DS = new dataset ();
Using (sqlconnection conn = new sqlconnection (connstr ))
{
Sqlcommand cmd = new sqlcommand (sqlstr, Conn );
Cmd. commandtype = commandtype. text;
Sqldataadapter Ada = new sqldataadapter ();
Ada. selectcommand = cmd;
Ada. Fill (DS );
}
Return Ds;
}

This is the usage. It includes all the operations for connecting to the database, executing SQL commands, and returning a dataset using the adapter.
In this way. We don't need to worry about whether its conn resources are closed. When we don't need it. The system automatically helps us to disable security.

What's more, have you noticed? I did not write conn. open ()
Yes. You can leave this sentence empty.
But there is a condition: When we use an adapter, we do not need to write open ().
Otherwise, write it.

By the way, if you are interested. You can download microsoft's rare open-source DLL-sqlhelper
It encapsulates all database operation methods. Including addition, deletion, modification, and query. We only need to reference and call.
This is the official stuff. It is worth a try. I read his source code. Using ()

I am sending a topic post here for the first time.

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.