Import Excel in Winforn and display it, then get the contents of the multiple-checkbox selection

scene

Use NPOI to import Excel and assign it to the DataTable, then display it on the DataGrdView, add a check box, and get the contents of the selected row.

Winform uses NPOI to import Excel and assign it to DataTable:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100064821

Effect

 

Realization

On the basis of the successful import and assignment to DataTables above, add a multi-check box on the DataGridView.

The following code is added to the click event of the second button to generate the header of the data source dataTable to add a multi-select column.

 

DataGridViewColumn checkCol = new DataGridViewCheckBoxColumn();
this.dataGridView1.Columns.Add(checkCol);

 

Then in the click event of the third button

private void button4_Click(object sender, EventArgs e)
        {
            
            
            int strCount = 0;
            //First, do the first loop to get the number of rows selected
            for (int i = 0; i <dataGridView1.Rows.Count; i++)
            {
                //If selected
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    strCount++;
                }
            }
            //Create a new selected length array to store each row
            string[] str = new string[strCount];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    //Gets the first cell of the current row + @ +The content of the second cell
                    str[i] = dataGridView1.Rows[i].Cells[1].Value.ToString() + "@" + dataGridView1.Rows[i].Cells[2].Value.ToString();
                }
            }
            //Output the contents of all selected rows
            for (int i = 0; i < str.Length; i++)
            {
                MessageBox.Show(string.Format("Acquired{0}The article is:",i+1) +str[i]);
            }
            
        }

 

Sample Code Download

https://download.csdn.net/download/badao_liumang_qizhi/11612423

Keywords: C# Excel

Added by Swerve1000 on Sun, 06 Oct 2019 06:24:51 +0300