I am creating a winform that has has a tab control with three tabs which each contain their own dataGridViews. I have an excel spreadsheet that contains data for all three tables. These all have the same headers (Name, Type, Date, Cost). There are three different categories of type
, these being hire, relocate, and service. I would like to create a method that inserts data from the file to the three dataGridViews based on some sort of where clause that separates them based off their type.
Here is what I have so far. Ive added some comments to help aid my question:
if (System.IO.File.Exists(@"C:\temp\Activity.csv"))
{
string[] lines = File.ReadAllLines(path + file);
string[] data;
for (int i = 0; i < lines.Length; i++)
{
data = lines[i].ToString().Split(',');
string[] row = new string[data.Length];
for (int j = 0; j < data.Length; j++)
{
row[j] = data[j].Trim();
}
//dgv1 is the dataGridView for hire
form.dgv1.Rows.Add(row); //I want to add the excel row to this datagrid if the 'type' column = hire
//dgv2 is the dataGridView for service
form.dgv2.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = service
//dgv3 is the dataGridView for relocate
form.dgv3.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = relocate
}
}
Here is an example of what the file looks like: | Name | Type | Date | Cost | | ----- | -------- | ---------- | ---- | | Jack | service | 18/03/2021 | 499 | | John | hire | 23/02/2021 | 199 | | Steve | hire | 01/11/2020 | 249 | | Suzie | relocate | 21/03/2021 | 44 |
It would do something like this:
if (Type = hire)
{
dgv1.Rows.Add(row);
}
if (Type = service) {
dgv2.Rows.Add(row);
}
if (Type = relocate)
{
dgv3.Rows.Add(row);
}
But I'm not sure how to code this
I'm not sure what of your above code is working, but if your row
variable is getting populated with the correct values from your columns, then you should be able to just check the value of the second item in your row
array to get the type. The second item in your row
array should correspond to the type.
So you should be able to do something like this:
if (System.IO.File.Exists(@"C:\temp\Activity.csv"))
{
string[] lines = File.ReadAllLines(path + file);
string[] data;
for (int i = 0; i < lines.Length; i++)
{
data = lines[i].ToString().Split(',');
string[] row = new string[data.Length];
for (int j = 0; j < data.Length; j++)
{
row[j] = data[j].Trim();
}
//dgv1 is the dataGridView for hire
if (row[1] == "hire")
{
form.dgv1.Rows.Add(row); //I want to add the excel row to this datagrid if the 'type' column = hire
}
//dgv2 is the dataGridView for service
else if (row[1] == "service")
{
form.dgv2.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = service
}
//dgv3 is the dataGridView for relocate
else if (row[1] == "relocate")
{
form.dgv3.Rows.Add(row);//I want to add the excel row to this datagrid if the 'type' column = relocate
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.