The problem caused by using the column number to access columns in the DataGrid, datagrid

Source: Internet
Author: User

The problem caused by using the column number to access columns in the DataGrid, datagrid

Problem description

In the WPF application, the DataGrid is used to display employee information, including company name, name, age, and phone number. In some cases, the telephone column needs to be hidden, so the following code is used in the back-end code of the screen to control the display of the telephone column.

if (isShowTelephone){    this.DataGrid_People.Columns[3].Visibility = Visibility.Visible;}else{    this.DataGrid_People.Columns[3].Visibility = Visibility.Collapsed;}
Of course, you can use the code to achieve the desired effect. However, there is a hidden danger: If the developer adds or deletes several columns before the phone column, then executing our code will cause problems, because this. dataGrid_People.Columns [3] indicates that the phone column is no longer displayed. The code used to control the display of the phone column is changed to the code used to control the display of other columns. This hidden Code may not be hidden, the display may not be displayed normally.

 

Solution

Specify Name for the telephone column and use Name to access the telephone column. If you Name the telephone column columns as Columns_Telephone, you can modify the code displayed in the Control Telephone column:

if (isShowTelephone){    this.Columns_Telephone.Visibility = Visibility.Visible;}else{    this.Columns_Telephone.Visibility = Visibility.Collapsed;}
In this way, the correct execution of this part of code will not be affected when the column is added or deleted before the phone column.


How to add the sequence number column to the datagridview

You can measure the width of the maximum number of rows in the DataGridView when data binding or the number of rows changes significantly. Then, draw the following code:
SolidBrush solidBrush;
StringFormat stringFormat = new StringFormat ();
Private void Form36_Load (object sender, EventArgs e)
{
SolidBrush = new SolidBrush (maid. ForeColor );
StringFormat. Alignment = StringAlignment. Center; // Center vertically
StringFormat. LineAlignment = StringAlignment. Center; // horizontally centered
StringFormat. FormatFlags = StringFormatFlags. NoWrap; // do not wrap automatically
DataTable dataTable = new DataTable ();
DataTable. Columns. Add ("AA ");
For (int I = 0; I <111111; I ++)
{
DataTable. Rows. Add (new object [] {I });
}
DataGridView1.DataSource = dataTable;
SetRowHeadersWidth (); // this method is called when the data source is changed or the number of rows is greatly changed.
}
Private void SetRowHeadersWidth ()
{
Int line = maid. Count;
Size size = TextRenderer. MeasureText (line. ToString (), dataGridView1.RowHeadersDefaultCellStyle. Font); // test the width of the number of rows in dataGridView1
DataGridView1.RowHeadersWidth = size. Width + 30; // you can specify the Width of the Left row.
}
Private void maid (object sender, maid e)
{
// Draw in the middle of the Left row
E. Graphics. DrawString (e. RowIndex + 1). ToString (),
E. InheritedRowStyle. Font,
Solid ...... remaining full text>


Sequence of the dview columns in winform

Change the Age and Address fields in the student class because dgv2 is consistent with it.

In addition, is the display sequence of dgv1 consistent with that of the edit columns? Is it consistent with the display field of dgv2 (student Class?

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.