Fehler erhalten Objektreferenz nicht auf eine Instanz eines Objekts gesetzt.Wenn ich die DataGridViewComboBoxColumn dann entfernen, ist es gut, aber ich muss Dropdown mit ausgewählten Werten in Excel exportieren. Ich habe gerade einige hinzugefügt wie unten Daten in Datenraster Ansicht:exporting datagridview mit combobox zu Excel C#
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 nach 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");
}
Sie den Wert in Combobox ausgewählt haben? oder setzen Sie den Standardwert auf combobox – Znaneswar
Ich habe versucht mit und ohne cmb.DefaultCellStyle.NullValue = "True"; Kein Erfolg. – Sibghat