C # DataGridView using record sharing

Source: Internet
Author: User

Recently used DataGridView, the problems encountered and some knowledge recorded, so that later use can be quickly remembered. 1, add line numberSometimes when we use DataGridView , we are asked to add the upstream number before each line of data, while using it to accurately understand the current data collection. Here I learned that there are two ways to add line numbers, can be displayed normally, there is a need for reference. The first type:when you have set up the data source to complete the following processing:
 for int 0 // Line number      {           int1;            = j.tostring ();      }

With the above processing, you can simply add the line number. Simple seems to be the loop for each line to add, the idea is relatively concise.

The second type: This is done using DataGridView's own event, the event is rowpostpaint, we can add the corresponding processing method in the event to add the line number, the corresponding code is:
1 Private voidDatagridview1_rowpostpaint (Objectsender, Datagridviewrowpostpainteventargs e)2         {3System.Drawing. Rectangle Rectangle =NewSystem.Drawing.Rectangle
(E.rowbounds.location.x,4 E.ROWBOUNDS.LOCATION.Y,5Datagridview1.rowheaderswidth-4,6 e.rowbounds.height);7 8Textrenderer.drawtext (E.graphics,
(E.rowindex +1). ToString (),9 DataGridView1.RowHeadersDefaultCellStyle.Font,Ten Rectangle, One DataGridView1.RowHeadersDefaultCellStyle.ForeColor, ATextformatflags.verticalcenter |TextFormatFlags. right); -}

This method can also be displayed normally.

  The above two ways I have through the practice of testing, can be normal display, the other way I have not studied for the time being.

2. Highlight a line

There may be a requirement in our actual project that when a certain piece of data satisfies a certain condition, the entire line of the data or the current unit highlighting displayed, because the principle is the same, one of the cases is recorded here. The specific implementation is very simple, if you see the above to add the line number, here can be very good understanding, the principle is the same, this is the processing of different properties, specifically: 
1  for(inti =0; i < DataGridView2.Rows.Count; i++)//Line number2       {3           if(Datagridview2.rows[i]. cells["FLAG"]. value.tostring () = ="2") {4              This. datagridview2.rows[i]. Defaultcellstyle.backcolor=color.red;5               }6}

In fact, it is the same as the first of the above, the processing properties are not the same.

3. Add check boxSometimes we are asked to, we can choose one or more rows of the dataset, although we can do it with the CTRL key, but in the actual work, we prefer the check box as a form. And this form of implementation as long as we add a column for selection on it, when we add this column, select columntype as  Datagridviewcheckboxcolumn, we can also choose a variety of forms as needed. 4. Some mistakes made1.The table header does not correspond to the database and the displayed data is not displayed as a rule When we edit the field we will generally edit two properties, one is HeaderText, one is DataPropertyName, theprevious one is for display on the interface, The latter is consistent with the fields in the set dataset. If the next one is not set, an issue does not appear as expected. By the way, there is a small trick, and when we don't want to show a column, we can set the property visible to false. 2.Error getting cell data by property fieldIf we are going to take this form (datagridview2.rows[i]. cells["FLAG" ]. Value.tostring ()) to get the data for the cell, we simply set the DataPropertyName property to be not enough, we also set the Name property to flag (corresponding to the specific field settings). 5. Description   in the future use, if there are some things to share, I will continue to add.
   

C # DataGridView using record sharing

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.