WPF Loop with DataGrid Rows - c#

First: this is my code at the moment
class Plan
{
public string Linie { get; set; }
public string Kurs { get; set; }
public string Abfahrt { get; set; }
public string Von { get; set; }
public string Nach { get; set; }
public string Ankunft { get; set; }
public string Pause { get; set; }
public Plan(string Linie, string Kurs, string Abfahrt, string Von, string Nach, string Ankunft, string Pause)
{
this.Linie = Linie;
this.Kurs = Kurs;
this.Abfahrt = Abfahrt;
this.Von = Von;
this.Nach = Nach;
this.Ankunft = Ankunft;
this.Pause = Pause;
}
private System.Collections.ObjectModel.ObservableCollection<Plan> _items = new System.Collections.ObjectModel.ObservableCollection<Plan>();
Plan.ItemsSource = _items;
So: I want a loop, that do something foreach row. So, as example: A loop that show a MessageBox, which contains each cell (I mean [as example] the Linie of the seleceted row an so on)
I am happy about each answer!
Thank a lot!

You could make a list of Plan.
To create list:
List<Plan> ListPlan=new List<Plan>();
ListPlan.Add(new Plan("","","",...);
To read list:
foreach (Plan p in ListPlan)
{
MessageBox.Show(p.Linie)
}

Related

working with DataTabe, List<> and DataGridView inside nested For Next

Im trying to get information out of certain List inside For Next loop to be able to place the data in a DataTable so I can show it in a DataGridView. Well I have been searching and trying for hours but I dont gett the result I want. I think I'm almost there but something I'm doing wrong. Is ther eanybody willing to help me?
private void Form4_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
//Set first column names
//dt.Columns.Add("Sample No");
for (int i = 0; i <= frmMain.iAnaChan-1; i++)
//Loop to add analog channel columns
{
dt.Columns.Add(frmMain.AnalogChannelList[i].ch_id.ToString());
}
DataRow row = dt.NewRow();
for (int sam = 1; sam <= frmMain.TotSamples ; sam++)
{
for (int chan = 0; chan <= frmMain.iAnaChan -1; chan++)
{
row[chan] = frmMain.channeldata[sam-1].sSampleVal.ToString();
dt.Rows.Add(row);
}
}
dataGridViewSampleData.DataSource = dt;
}
Class
public class aChannelList
{
//Class for retreiving all Analog Channel information
public string An { get; set; }
public string ch_id { get; set; }
public string ph { get; set; }
public string ccbm { get; set; }
public string uu { get; set; }
public string chanMult { get; set; }
public string chanOffset { get; set; }
public string skew { get; set; }
public string minRange { get; set; }
public string maxRange { get; set; }
public string primVal { get; set; }
public string secVal { get; set; }
public string scalingID { get; set; }
}
public class SampledData
{
//This class retreives the relevant information into a list to give the relevant channel data visible into a datagrid for information
public string sSampleNo { get; set; }
public string sTimeStamp { get; set; }
public string sSampleVal { get; set; }
}
iAnaChan is an integer than should set the amount of "channels" i.e. columns I need. And this works correct if I don't run the below code.
Than I need to start rows, the amount of rows have the values of frmMain.TotSamples and place them inside a a For Next loop so it will place the rows automatically. Than I try to add in the columns the sample values in the repectivly columns and add a row. After all loops have been passed thru I try to place it in a DataTable. But unfortunatly this doesn't work and I really would like to know what I am doing wrong?
You can create public ObservableCollection<object> Items { get; set; } into which you will add the items using a foreach loop
foreach (var item in dataCollection)
Items.Add(item)
Then just set DataGrid's ItemSource to Items. DataGrid is able to autogenerate itself this way.

How do i parse this name class to winform button function c#

class FullNameClass
{
public string xFullName { get; set; }
public string lcFirstName { get; set; }
public string lcMiddleName { get; set; }
public string lcLastname { get; set; }
public string lcSuffix { get; set; }
public string getfirstanme(string xFullName)
{
string[] words = xFullName.ToUpper().Split(' ');
int numberOfWords = words.Length;
if (numberOfWords == 3)
{
if (numberOfWords == 3)
{
string[] fullname = xFullName.Split(new Char[] { ' ' });
if (fullname[1].Length > 2)
{
string firstname = fullname[0] + " " + fullname[1];
lcFirstName = firstname;
lcMiddleName = null;
lcLastname = null;
lcSuffix = null;
return lcFirstName;
}
itry to use this code but it shows blank
in Form under button functions
FullNameClass fullname = new FullNameClass();
fullname.xFullName = "Robert A Smith";
Messagebox.show(fullname.lcFirstName);
Hope someone will help me on this
by the way im newbie in coding and also learn from reading only
please be patient about my code
You need to call the method in you case:
MessageBox.Show(fullname.getfirstanme(fullname.xFullName));
But, there is a lot to be improved here.
Besides the obvious typo's, the FullName class should be redesigned.
For starters, all these properties:
public string xFullName { get; set; }
public string lcFirstName { get; set; }
public string lcMiddleName { get; set; }
public string lcLastname { get; set; }
public string lcSuffix { get; set; }
are both set and get.
There is nothing fancy about them: if you dont set them, you wont get a result.
A common way to approach this problem is to think about how you want your object to behave.
Let me give you an example. Let's say, you know upfront you'll always use the fullname as input. Then you can apply this to a constructor:
class FullNameClass
{
public string lcFirstName { get; } //read only
public FullNameClass(string fullName) // required to initialize the class
{
// set the firstname property with your logic
lcFirstName = getfirstanme(fullName);
//you can apply this pattern for the other properties as well
}
}

CSV file read result value set to entitiy object

I'm try to update OFAC Sanction list to my database by using following URL.
URL :
http://www.treasury.gov/ofac/downloads/sdn.csv
This csv file has no any header and can download as a file. So I'm split all the record and get as a string Array. I have a problem with how can I assign those results to my entity object to save.
This is my Code :
List<SanctionOFACEntries> sanctionOFACList = new List<SanctionOFACEntries>();
List<string> splitted = new List<string>();
string fileList = GetCSVFileContent(url);
string[] tempStr;
tempStr = Regex.Matches(fileList, "(?:\"(?<m>[^\"]*)\")|(?<m>[^,]+)").Cast<Match>().Select(m => m.Value).ToArray(); ;
int i = 0;
foreach (string item in tempStr)
{
i += 1;
SanctionOFACEntries sanctionOFAC = new SanctionOFACEntries();
if (i != 1)
{
sanctionOFAC.Name = tempStr[i];
}
}
This case that all the records are assign to array list correctly. If any one have this OFAC sample upload code send me .
Please help this.
Thanks.
I'm not 100% clear on your issue, but first of all, I'd recommend you use a csv library. I'm not clear on what your Regex is attempting to due, but I'm assuming it's trying to parse CSV. Using FileHelpers csv library, I've rewritten your code:
var engine = new FileHelperEngine<SanctionOFACEntries>();
var sanctionOFACList = engine.ReadStringAsList(GetCSVFileContent(url));
Far more straight forward. Note your SanctionOFACEntries class should look something like this Treasury Data Spec:
[DelimitedRecord(",")]
public class SanctionOFACEntries
{
public int ent_num
[FieldQuoted]
public string ent_num { get; set; }
[FieldQuoted]
public string SDN_Name { get; set; }
[FieldQuoted]
public string SDN_Type { get; set; }
[FieldQuoted]
public string Program { get; set; }
[FieldQuoted]
public string Title { get; set; }
[FieldQuoted]
public string Call_Sign { get; set; }
[FieldQuoted]
public string Vess_type { get; set; }
[FieldQuoted]
public string Tonnage { get; set; }
[FieldQuoted]
public string GRT { get; set; }
[FieldQuoted]
public string Vess_flag { get; set; }
[FieldQuoted]
public string Vess_owner { get; set; }
[FieldQuoted]
public string Remarks { get; set; }
}

Passing a generic list to a method

I have an application that will parse an excel file and add a column, then generate a new CSV file with the results. I am able to create a list of the items I want in the file, but I cannot figure out how to pass that list to the method that is generating the new file.
I have the following class:
public class LocationData
{
public string PostalCode { get; set; }
public string Partner { get; set; }
public string LocationID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Market { get; set; }
}
and the following code to get the data into a list:
LocationData Locationdata = new LocationData()
{
PostalCode = location[0],
Partner = location[1],
LocationID = location[2],
Name = location[3],
Country = location[4],
Market = repository.GetMarketsForPostalCode(location[0])
}
I also have the method to create the csv and I need to pass in the list info, but I get the error:
foreach statement cannot operate on variables of type 'app.LocationData' because 'app.LocationData' does not contain a public definition for 'GetEnumerator'
I think you are misunderstanding what a list is in C#. I think you need the List data type. Try this:
List<string> Locationdata = new List<string>()
{
location[0],
location[1],
location[2],
location[3],
location[4],
repository.GetMarketsForPostalCode(location[0])
};
Your csv function will look like this
public void GenerateCSV(List<LocationData> data)
{
foreach (LocationData d in data)
{
//put line in csv as
string s = d.PostalCode + "," d.Partner + _"," + d.LocationID...... + Environment.NewLine;
}
}
Your class declaration will remain same
public class LocationData
{
public string PostalCode { get; set; }
public string Partner { get; set; }
public string LocationID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Market { get; set; }
}
Now you need to add all the data in the list first
which you will do like this
List<LocationData> lst = new List<LocationData>();
LocationData ld = new LocationData();
ld.LocationID = "0";
ld.Market = "market";
lst.Add(ld);
........
GenerateCSV(lst);

Storing and retrieving from lists with custom structures in

I'm having some trouble storing and retrieving items into a list<> with a custom structure.
My structure looks like this:
public class list_rss_parameters
{
public string this_string { get; set; }
public string title_start { get; set; }
public string title_end { get; set; }
public string description_start { get; set; }
public string description_end { get; set; }
public string link_start { get; set; }
public string link_end { get; set; }
public string publish_date_start { get; set; }
public string publish_date_end { get; set; }
public string author_start { get; set; }
public string author_end { get; set; }
}
My stored procedure looks like this (and note that the variable names are the same as the custom Key names) Is this ok?
//this is the last part of a custom method that returns a list
List<list_rss_parameters> list_rss_items = new List<list_rss_parameters>();
list_rss_items.Add(new list_rss_parameters()
{
this_string = this_string,
title_start = title_start,
title_end = title_end,
description_start = description_start,
description_end = description_end,
link_start = link_start,
link_end = link_end,
publish_date_start = publish_date_start,
publish_date_end = publish_date_end,
author_start = author_start,
author_end = author_end
});
return list_rss_items;
If the above two setups are correct, how do I pull items out of the List once I return it?
List<list_rss_parameters> list_rss_parameters = new List<list_rss_parameters>();
list_rss_parameters = f_discover_rss_parameters(rss);
show(list_rss_parameters.Count.ToString());
show(list_rss_parameters[0].ToString()); //does not show this_string
show(list_rss_parameters[this_string'] //does not show this_string
show(list_rss_parameters[0][this_string'];//does not show this_string
What am I doing wrong?
You want the this_string property of the first item in your list it seems:
show(list_rss_parameters[0].this_string);
Or show all of them:
foreach(var item in list_rss_parameters)
{
Console.WriteLine(item.this_string);
}
As a side note your property names don't match the PascalCase naming convention for properties in .NET - so this_string really should be ThisString.

Categories