C # Share usage records of the DataGridView,
When using the DataGridView recently, you can record the problems and knowledge you have encountered so that you can quickly think of them later.1. Add a row numberSometimes, when using the DataGridView, we are asked to add a row number before each row of data. While using the data, we can accurately understand the current data set. Here I have learned that there are two ways to add a row number, which can be displayed normally. For more information, see.First:After setting the data source, perform the following operations:
For (int I = 0; I <dataGridView2.Rows. count; I ++) // The row number {int j = I + 1; dataGridView2.Rows [I]. headerCell. value = j. toString ();}
With the above processing, you can simply add the row number. In a simple way, it is a loop to add each line. The idea is concise.
Second:This method is to use the event that comes with the DataGridview for processing. This event is RowPostPaint. we can add the corresponding processing method to this event to add the row number. The corresponding code is:
1 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 2 { 3 System.Drawing. Rectangle rectangle = new System.Drawing.Rectangle
(e.RowBounds.Location.X, 4 e.RowBounds.Location.Y, 5 dataGridView1.RowHeadersWidth - 4, 6 e.RowBounds.Height); 7 8 TextRenderer.DrawText(e.Graphics,
(e.RowIndex + 1).ToString(), 9 dataGridView1.RowHeadersDefaultCellStyle.Font,10 rectangle,11 dataGridView1.RowHeadersDefaultCellStyle.ForeColor,12 TextFormatFlags.VerticalCenter | TextFormatFlags .Right);13 }
This method can also be displayed normally.
The above two methods have passed the practice test and can be displayed normally. I have not studied other methods for the time being.
2. highlight a line
There may be such requirements in our actual project. When a piece of data meets a certain condition, the entire row of the data or the current cell is highlighted, because the principles are the same, we will record one of them here. The specific implementation is very simple. If you have read the above line numbers, you can have a good understanding here. The principles are the same. This is because the processing attributes are different, specifically:
1 for (int I = 0; I <dataGridView2.Rows. count; I ++) // row number 2 {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 one above, and the processing attributes are different.
3. Add check boxesSometimes we will be asked that we can select one or more rows of the dataset. Although we can use the ctrl key for implementation, in actual work, we prefer the checkbox format. To implement this form, you only need to add a column for selection. When you add this column, select ColumnType as DataGridViewCheckBoxColumn. You can also select multiple forms as needed.
4. Some mistakes made1. the header does not correspond to the database. The displayed data is not displayed according to rules. When editing fields, we usually edit two attributes: HeaderText and DataPropertyName, the first one is used for display on the interface, and the last one is consistent with the fields in the Set dataset. If the subsequent settings are not set, the problem is not displayed as expected. By the way, there is a small trick. When we don't want to display a column, we can set the Visible attribute to False. 2. An error occurred while retrieving cell data through attribute fields. Suppose we want to perform this form (dataGridView2.Rows [I]. cells ["FLAG"]. value. toString () is used to obtain the data of cells. It is not enough to set the DataPropertyName attribute alone. We also need to set the Name attribute to FLAG (based on specific fields ).
5. Description
In future use, if there are other things that can be shared, I will continue to add them.