In my code in POI class I want to implement some method, at first it will take data from .xls file.Then it will show city and population name in the datagridview table.Now from datagridview table it will take only the city name which will show in the 1st Combobox. Then if someone click on the 1st combobox item it will show some places in that city on the second combobox. However I wrote the code. Now I got puzzled how to call it from Form1.cs and how it will work, because at this moment it is not working at all.
My code for POI class is
public class POI
{
Form f;
public static ComboBox Combo_list1 = new ComboBox();
public static ComboBox Combo_list2 = new ComboBox();
public static DataGridView dataTable = new DataGridView();
Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();
public void List(Form f)
{
f = new Form();
var startPath = Application.StartupPath;
string folderName = Path.Combine(startPath, "POI_List");
System.IO.Directory.CreateDirectory(folderName);
string SavedfileName = "POI_list.json";
var Saving_path = Path.Combine(folderName, SavedfileName);
string fileName = "Zensus_Gemeinden_org.xlsx";
var path = Path.Combine(startPath, fileName);
String name = "Gemeinden_31.12.2011_Vergleich";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
path + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select [3] as City,[4] as Population, * From [" + name + "$D7:E11300] Where [4] > 10000", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataTable.DataSource = data;
for (int i = 0; i < data.Rows.Count; i++)
{
Combo_list1.Items.Add(data.Rows[i]["City"]);
}
string Place_Json = "Place_List:" + JsonConvert.SerializeObject(data, Formatting.Indented);
File.WriteAllText(Saving_path, Place_Json);
foreach(string line in File.ReadLines("POIList.txt"))
{
string[] parts = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
poi.Add(parts[0], new List<string>());
poi[parts[0]] = new List<string>(parts.Skip(1));
}
}
public void Combo_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (Combo_list1.SelectedItem != null)
{
string txt = Combo_list1.SelectedItem.ToString();
if (poi.ContainsKey(txt))
{
List<string> points = poi[txt];
Combo_list2.Items.Clear();
Combo_list2.Items.AddRange(points.ToArray());
}
}
}
}
I want to call both method in my form1.cs class. but it is not working. Form1.cs look like this-
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
POI.Combo_list1 = comboBox1;
POI.dataTable = dataGridView1;
//POI.List();
//POI
}
private void button1_Click(object sender, EventArgs e)
{
}
}
sorry for such a long code.Answer is greatly appreciate.