DataGridView of C#winform form control

DataGridView of C#winform form control

DataGridView is a control that can provide a powerful and flexible display of data in tabular form.
When we use the table in the form, we will find that after editing the column header of each column, the first example is blank, as shown in the figure.
Insert picture description hereHow to remove the blanks in the first column?
In fact, the first column blank is the row header of each row. When we don't need it, right-click the control and select properties, find RowHeadersVisible and change it to False. In the same way, the first row is also the column heading of each column. When you no longer need to display it, find the CoulmnHeadersVisible in the property and change it to False.
Insert picture description here
To add data to the table, you can use the Colunms and Rows attributes.

        private void Form1_Load(object sender, EventArgs e)
        {
    
    
            string[] row1 = new string[] {
    
     "0001", "小吕", "男", "18890234567" };
            string[] row2 = new string[] {
    
     "0002", "小张", "男", "18890232343" };
            string[] row3 = new string[] {
    
     "0003", "小王", "女", "18890238798" };
            string[] row4 = new string[] {
    
     "0004", "小朱", "男", "18890234566" };
            object[] rows = new object[] {
    
     row1, row2, row3, row4 };
            foreach (string[] i in rows)//使用foreach语句循环添加
            {
    
    
                dataGridView1.Rows.Add(i);//向控件中添加数据
            }
        }

Effect after running:
Insert picture description hereAt this time, you will find multiple blank rows. When you click the blank cell, you can manually add data. If you don't want to display the blank row, you can find AllowUserAddToRows in the properties of the control and change it to False.
Insert picture description hereClicking on the added data can also be edited. If we do not want to edit the added data, we only need to change ReadOnly to True in the properties.

Attachment:
DataGridViewSelectionMode enumeration value and its description

Enumerated value Description
CellSelect One or more cells can be selected
CoulmnHeaderSelect This column can be selected by clicking the header cell of the column, and this cell can be individually selected by clicking a cell
FullCoulmnSelect Select the entire column by clicking on the header of the column or the cells contained in the column
FullRowSelect Select the entire row by clicking on the header of the row or the cells contained in the column
RowHeaderSelect This row can be selected by clicking the header cell of the row, and this cell can be individually selected by clicking a cell

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/108186348