1,934 questions
The code is adding the row once for each column. You should add row outside of the second for loop. Try something like this
for (int i = worksheet.Dimension.Start.Row; i <= worksheet.Dimension.End.Row; i++)
{
bool addRow = false;
//loop all columns except the last two columns in a row
for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column - 2; j++)
{
//add the cell data to the List
if (worksheet.Cells[i, 1].Text == comboBox1.Text && worksheet.Cells[i, j].Value != null)
{
addRow=true;
}
}
if(addRow)
{
dataGridView1.Rows.Add(worksheet.Cells[i, 1].Value.ToString(),worksheet.Cells[i, 2].Value.ToString(),worksheet.Cells[i, 3].Value.ToString(),worksheet.Cells[i, 4].Value.ToString());
}
}