exporting datagridview with combobox to excel c#

823 views Asked by At

Getting error Object reference not set to an instance of an object.If i remove the DataGridViewComboBoxColumn then it is exporting fine but i need to export dropdown with selected values in excel.I have just added some data in datagrid view as below:

 dataGridView1.ColumnCount = 3;
        dataGridView1.Columns[0].Name = "Product ID";
        dataGridView1.Columns[1].Name = "Product Name";
        dataGridView1.Columns[2].Name = "Product Price";

        string[] row = new string[] { "1", "Product 1", "1000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "2", "Product 2", "2000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "3", "Product 3", "3000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "4", "Product 4", "4000" };
        dataGridView1.Rows.Add(row);

        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.HeaderText = "Select Data";
        cmb.Name = "cmb";
        cmb.MaxDropDownItems = 4;
        cmb.Items.Add("True");
        cmb.Items.Add("False");
        dataGridView1.Columns.Add(cmb);

//Export to Excel

        worksheet = workbook.ActiveSheet;

            worksheet.Name = "test";

            int cellRowIndex = 1;
            int cellColumnIndex = 1;
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }
            MessageBox.Show(dataGridView1.ColumnCount.ToString());



            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {

                        worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

                    cellColumnIndex++;
                }
                cellColumnIndex = 1;
                cellRowIndex++;
            }

            //Getting the location and file name of the excel to save from user.
            SaveFileDialog saveDialog = new SaveFileDialog();
            //saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
            saveDialog.Filter = "Excel files (All files (*.*)|*.*";
            saveDialog.FilterIndex = 2;

            if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                workbook.SaveAs(saveDialog.FileName);
                MessageBox.Show("Export Successful");
            }
1

There are 1 answers

8
Znaneswar On

Try this to retrieve selected value from combobox

dataGridView1.Rows[0].Cells[i].FormattedValue.ToString();

you can use same to retrieve from cell value instead of Rows[0].Cells[i].value

In your case

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {

                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].FormattedValue.ToString();

                cellColumnIndex++;
            }
            cellColumnIndex = 1;
            cellRowIndex++;
        }