how to specify 6 columns and name them in datagridview so when I import an excel sheet with missing columns it adds the missing columns, keep the content empty and arrange the columns based on what I specify in datagridview note: column name does not change
for example I specify column A,B,C,D in datagridview the excel sheet it has the same column name but some of them are missing for example (A,C)
this is what I did for importing excel sheet in to the application
namespace excelreaderdesktop
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fil = new OpenFileDialog();
fil.ShowDialog();
string path = fil.FileName.ToString();
ExcelFileReader(path);
}
public void ExcelFileReader(string path)
{
var stream = File.Open(path, FileMode.Open, FileAccess.Read);
var reader = ExcelReaderFactory.CreateReader(stream);
var result = reader.AsDataSet();
var tables = result.Tables.Cast<DataTable>();
foreach(DataTable table in tables)
{
dataGridView1.DataSource = table;
}
}
}
}
Source: Windows Questions