Hey team. Im very new to C# and kindly don't regret if there are mistakes.
To read 2nd and 3rd columns in a excel file in data table and display it in a list view with check boxes.
Thanks in advance for the help!!
What I have tried:
Created two functions separately. One for initialising OLEDB and other to read the file in data table. And finally using the button action the data table shall be called and the excel file shall be displayed in the list view with check boxes.
NOTE:
The excel file consists of 5 sheets and out of those 5 i want to read the 2nd and 3rd column of 2nd sheet.
clicking next button will navigate to next tab where list view box is present.
void InitializeOledbConnection(string filename, string extrn)
{
connString = "";
if (extrn == ".xlsx")
connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + glb + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
else
connString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + glb + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
Oledb = new OleDbConnection(connString);
}
private DataTable ReadFile()
{
schemaTable = new DataTable();
OleDbCommand Oledbcmd = new OleDbCommand();
Oledbcmd.Connection = Oledb;
Oledb.Open();
Oledbcmd.CommandText = "Select * from [CONTROL_TABLE$]";
OleDbDataReader dr = Oledbcmd.ExecuteReader();
ContentTable = new DataTable();
ContentTable = null;
if (dr.HasRows)
{
ContentTable = new DataTable();
ContentTable.Columns.Add("SIGNAL", typeof(string));
ContentTable.Columns.Add("ROUTE_BUTTON", typeof(string));
while (dr.Read())
{
if (dr[0].ToString().Trim() != string.Empty && dr[1].ToString().Trim() != string.Empty && dr[2].ToString().Trim() != string.Empty && dr[0].ToString().Trim() != " " && dr[1].ToString().Trim() != " " && dr[2].ToString().Trim() != " ")
{
ContentTable.Rows.Add(dr[0].ToString().Trim(), dr[1].ToString().Trim(), dr[2].ToString().Trim());
}
}
}
dr.Close();
Oledb.Close();
Oledb.Dispose();
return ContentTable;
}
private void t1NextTabRedirectButton_Click(object sender, EventArgs e)
{
tabControl.SelectedTab = routeNameTabPage;
{
string filePath = string.Empty;
string fileExt = string.Empty;
InitializeOledbConnection("C:\\gui_Files\\ICT_LAYOUT_AJJ.xlsx", ".xlsx");
DataTable tempTable = ReadFile();
{
for (int i = 0; i < schemaTable.Rows.Count; i++)
{
glb = schemaTable.Rows[i]["SIGNAL"].ToString();
glb = schemaTable.Rows[i]["ROUTE_BUTTON"].ToString();
}
routeNamesListView.Columns.Add("SIGNAL");
routeNamesListView.Columns.Add("ROUTE_BUTTON");
routeNamesListView.View = View.List;
routeNamesListView.CheckBoxes = true;
}
}
}