Hi I have a small problem. I am coding a program that displays the columns of an Excel file file and next to it the columns of an Access database in two seperate listviews. You select the sheet and table to display with a combo box. The Access columns display fine but the columns of the Excel sheet won’t display no matter what I do but the sheets appear in the combo box without any problems but the columns won’t appear no matter what I do. Is there any specific way that I can do this that works because I couldn’t find anything on the Internet.
This is the part that is supposed to insert it
{
{
// lvwDestination.Columns.Add(_dt.Columns[i].ColumnName);
lvwSource.Columns.Add("Fields in Database", -2, HorizontalAlignment.Left);
for (int i = 0; i < _Sourcedt.Rows.Count; i++)
{
ListViewItem lvwItem = new ListViewItem("" + _Sourcedt.Columns[i].ColumnName, 0);
lvwSource.Items.AddRange(new ListViewItem[] { lvwItem });
}
}
}
Another part also that I may have messed up it worked for access but not for excel. I also modified the Select statement little
private void cboSourceName_SelectedIndexChanged(object sender, EventArgs e)
{
// create the adapter and fill the DataSet
OleDbDataAdapter adapter =
new OleDbDataAdapter("Select * from " + cboSourceName.Text +"" + "", _SourceConn);
lvwSource.Columns.Clear();
lvwSource.Items.Clear();
_Sourcedt.Columns.Clear();//Contains all the columns in selected table
try
{
adapter.Fill(_Sourcedt);
}
catch
{
}
LoadColumns2();
}
And also the connect part
private void btnConnectSource_Click(object sender, EventArgs e)
{
try
{
string connString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + txtSourcePath.Text + ";" +
"Extended Properties=Excel 8.0";
//"Password=" + txtPWSource + ";";
_SourceConn = new OleDbConnection(connString);
// _OleConn[1].ConnectionString = connString;
_SourceConn.Open();
_Sourcedt = new DataTable();
LoadCombo2();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Listview is not a great idea to use in this case. The best way to do it is to populate your data from Excel into a Datagrid