i try to write my own control.But my control' html has problem: Look like
My Search button look above. But i need below: how can i do that?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts; // Ekle
using System.Web.UI.WebControls; // Ekle
using System.Web.UI;
using System.Collections;
using System.Threading;
namespace MyFilterControl
{
public class FilterControl : WebPart
{
internal List<Content> list { get; set; }
public Button BtnSearch { get; set; }
public event EventHandler Click;
public string FilterButtonText { get; set; }
public String PhID { get; set; }
internal PlaceHolder PlhControl { get; set; }
private string controlWidth = "10";
public FilterControl()
{
list = new List<Content>();
BtnSearch = new Button();
PlhControl = new PlaceHolder();
PlhControl.ID = Guid.NewGuid().ToString();
if (string.IsNullOrEmpty(PhID))
{
BtnSearch.ID = "btnSearch";
}
else
BtnSearch.ID = PhID;
if (string.IsNullOrEmpty(FilterButtonText))
{
BtnSearch.Text = "Search";
}
else
BtnSearch.Text = FilterButtonText;
BtnSearch.Click += new EventHandler(BtnSearch_Click);
}
private Table mainTable = new Table();
protected override void CreateChildControls()
{
base.CreateChildControls();
TableRow tblRow = new TableRow();
TableCell tblCell1 = new TableCell();
PlhControl.Controls.Add(new LiteralControl("<table><tr>"));
tblCell1.Controls.Add(PlhControl);
TableCell tblCell2 = new TableCell();
tblCell2.Controls.Add(BtnSearch);
tblRow.Cells.Add(tblCell1);
tblRow.Cells.Add(tblCell2);
mainTable.Rows.Add(tblRow);
this.Controls.Add(mainTable);
}
void BtnSearch_Click(object sender, EventArgs e)
{
PlhControl.Controls.Add(new LiteralControl("</tr><table>"));
foreach (var item in PlhControl.Controls)
{
if (item is MyTextBoxControl)
{
MyTextBoxControl t1 = (MyTextBoxControl)item;
if (t1.Text != "")
{
Add(new Content() { FieldName = t1.ID, Data = t1.Text, ID = Guid.NewGuid().ToString(), dataType = t1.dataType, filter=t1.filter });
}
}
}
Click(this, e);
}
public string Query()
{
string Qry = String.Empty;
int i = 0;
foreach (var item in list)
{
switch (item.filter)
{
case Filter.BeginsWith: Qry += String.Format(item.FieldName.ToString() + ".StartsWith({0}) && ", "#" + i.ToString());
break;
case Filter.EndsWith: Qry += String.Format(item.FieldName.ToString() + ".EndsWith({0}) && ", "#" + i.ToString());
break;
case Filter.Contains: Qry += String.Format(item.FieldName.ToString() + ".Contains({0}) && ", "#"+i.ToString());
break;
case Filter.Default: Qry += String.Format("{0}=={1} && ", item.FieldName, "#" + i.ToString());
break;
default:
break;
}
i++;
}
Qry = Qry.TrimEnd('&', '&', ' ');
return Qry;
}
public object[] QueryParams()
{
ArrayList arrList = new ArrayList();
foreach (var item in list)
{
if (item.dataType == DataType.String)
arrList.Add(item.Data);
else if (item.dataType == DataType.Int32)
{
int intVal = Convert.ToInt32(item.Data);
arrList.Add(intVal);
}
}
object[] strArray = arrList.ToArray(typeof(object)) as object[];
return strArray;
}
public string id { get; set; }
public void AddItem(ContentItem content)
{
PlhControl.Controls.Add(new LiteralControl("<td style=\"text-align:left\">"));
PlhControl.Controls.Add(new Label() { ID = Guid.NewGuid().ToString(), Text = content.LabelName });
PlhControl.Controls.Add(new LiteralControl(":</td>"));
PlhControl.Controls.Add(new LiteralControl("<td style=\"text-align:left\">"));
PlhControl.Controls.Add(new MyTextBoxControl() { ID = content.FieldName, dataType = content.dataType, filter = content.filter });
PlhControl.Controls.Add(new LiteralControl("</td>"));
}
private void Add(Content content)
{
list.Add(new Content() { ID = content.ID, Data = content.Data, FieldName = content.FieldName, dataType=content.dataType, filter= content.filter });
}
}
internal class Content
{
internal string ID { get; set; }
public string Data { get; set; }
public string FieldName { get; set; }
public DataType dataType { get; set; }
public Filter filter { get; set; }
}
public class ContentItem
{
private string ID { get; set; }
public string LabelName { get; set; }
public string FieldName { get; set; }
public DataType dataType { get; set; }
public Filter filter { get; set; }
}
public class MyTextBoxControl : TextBox
{
public DataType dataType { get; set; }
public Filter filter { get; set; }
}
public enum DataType
{
String,Int32
}
public enum Filter
{
BeginsWith,EndsWith,Contains,Default
}
}
You can place each control in a table cell. Or you can place everything in a div with a fixed heigh.
Related
I'm currently trying to scrape a cannabis strain database as it is no longer being maintained. I seem to be running into an issue where table rows are skipped in my logic but it really doesn't make sense, it's like a break is being called when I'm iterating through the //tr elements of the table that is storing the chemical reports.
Is it something like the δ α symbols in the next row. I've tried regex replacing them out to now luck. Any help would be appreciated all code is in a single class console app.
Issue is in the table.ChildNodes not iterating all the way through. Located in the ParseChemical() method.
Sample Page: http://ocpdb.pythonanywhere.com/ocpdb/420/
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.Design;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Reflection.Emit;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Microsoft.VisualBasic.CompilerServices;
namespace CScrape
{
class Program
{
private static readonly HttpClient Client = new HttpClient();
private static readonly string BaseUri = "http://ocpdb.pythonanywhere.com/ocpdb/";
private static int _startId = 420;
private static int _endId = 1519;
private static List<Lab> _labs = new List<Lab>();
private static List<ChemicalItem> _chemicalItems = new List<ChemicalItem>();
private static List<UnitOfMeasure> _uoms = new List<UnitOfMeasure>();
private static List<Strain> _strains = new List<Strain>();
static void Main(string[] args)
{
Client.DefaultRequestHeaders.Accept.Clear();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
_uoms.AddRange(GetUoms());
for (var i = _startId; i <= _endId; i++)
{
var result = GetUri($"{BaseUri}{i}").Result;
_strains.Add(ParseChemical(result));
}
}
private static long AddChemicalItem(ChemicalItem item)
{
if (ChemicalExists(item.Symbol))
return _chemicalItems.FirstOrDefault(ci => ci.Symbol == item.Symbol)?.Id ?? -1;
item.Id = _chemicalItems.Count + 1;
_chemicalItems.Add(item);
return item.Id;
}
private static void UpdateChemicalItem(ChemicalItem item)
{
if (!ChemicalExists(item.Symbol)) return;
var index = _chemicalItems.IndexOf(item);
if (!(index >= 0)) return;
_chemicalItems.RemoveAt(index);
AddChemicalItem(item);
}
private static long AddLab(Lab lab)
{
if (LabExists(lab.Name))
return _labs.FirstOrDefault(l => l.Name == lab.Name)?.Id ?? -1;
lab.Id = _labs.Count + 1;
_labs.Add(lab);
return lab.Id;
}
private static async Task<string> GetUri(string uri)
{
var response = await Client.GetByteArrayAsync(uri);
return Encoding.UTF8.GetString(response, 0, response.Length - 1);
}
private static Strain ParseChemical(string html)
{
html = Regex.Replace(html, #"Δ", "Delta");
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var strain = new Strain();
strain.Reports ??= new List<ChemicalReport>();
try
{
strain.Name = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[1]/div[1]/h3/b").InnerText;
}
catch (Exception e)
{
// TODO: DOcument Exception
Console.WriteLine(e.Message);
}
if (string.IsNullOrWhiteSpace(strain.Name)) return null;
try
{
var ocpId = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[1]/div[2]/p/b/text()[1]");
strain.OcpId = SanitizeHtml(ocpId.InnerText).Split(':')[1];
}
catch (Exception e)
{
// TODO: Document Exception
Console.WriteLine(e.Message);
}
if (string.IsNullOrWhiteSpace(strain.OcpId)) return null;
try
{
var date = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[1]/div[2]/p/text()");
}
catch (Exception e)
{
// TODO: Document Exception
Console.WriteLine(e.Message);
}
var chemReport = new ChemicalReport();
chemReport.Items ??= new List<ReportItem>();
try
{
var table = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div/div[2]/div[1]/table/tbody");
var children = table.ChildNodes.ToList();
// On the sample page there are 200 children here
// However it only interates through the first few and then just breaks out of the loop
foreach (var child in children)
{
var name = child.Name;
if (child.Name == "tr")
{
var infos = child.SelectNodes("th|td");
foreach (var info in infos)
{
if(string.IsNullOrWhiteSpace(info.InnerText)) continue;
if (info.InnerText.Contains("Report")) continue;
if (double.TryParse(info.InnerText, out var isNumber))
{
var last = chemReport.Items.LastOrDefault();
if (last == null) continue;
if (last.Value <= 0.0000) last.Value = isNumber;
else
{
var further = chemReport.Items.ToArray()[chemReport.Items.Count - 2];
if (further.Value <= 0.0000)
further.Value = isNumber;
}
continue;
}
var _ = new ChemicalItem
{
Name = info.InnerText,
Symbol = info.InnerText
};
_.Id = AddChemicalItem(_);
var report = new ReportItem
{
Chemical = _,
ChemicalItemId = _.Id,
UnitOfMeasureId = 1,
UoM = GetUoms()[0]
};
chemReport.Items.Add(report);
}
}
}
strain.Reports.Add(chemReport);
}
catch (Exception e)
{
// TODO: Document exception
Console.Write(e.Message);
}
return strain;
}
private static List<UnitOfMeasure> GetUoms()
{
return new List<UnitOfMeasure>
{
new UnitOfMeasure {Name = "Milligrams per Gram", Symbol = "mg/g"},
new UnitOfMeasure {Name = "Percent", Symbol = "%"}
};
}
private static string SanitizeHtml(string text, string replacement = "")
{
return Regex.Replace(text, #"<[^>]+>| |α|\n|\t", replacement);
}
private static string GetLabName(string[] split)
{
var strip = split[0].Split(':')[1];
for(var i = 1; i < split.Length - 2; i ++)
{
if (string.IsNullOrWhiteSpace(split[i])) break;
strip += $" {split[i]}";
}
return strip;
}
private static string GetSampleId(string[] split)
{
var found = false;
foreach (var item in split)
{
if (found)
return item.Split(':')[1];
if (item == "Sample") found = true;
}
return "NA";
}
private static bool LabExists(string name)
{
return _labs.Any(lab => lab.Name == name);
}
private static bool ChemicalExists(string name)
{
return _chemicalItems.Any(ci => ci.Symbol == name);
}
private static ReportItem GetReportItem(string text)
{
if (string.IsNullOrWhiteSpace(text)) return null;
ReportItem ri = null;
try
{
var clean = SanitizeHtml(text);
var check = 0;
var split = clean.Split(':');
var label = split[0];
if (string.IsNullOrWhiteSpace(label)) return null;
if (double.TryParse(label, out var invalidType)) return null;
var val = string.Empty;
if (split.Length == 1)
{
if (split[0].Contains("Total"))
{
Regex re = new Regex(#"([a-zA-Z]+)(\d+)");
Match result = re.Match(split[0]);
label = result.Groups[1].Value;
val = result.Groups[2].Value;
}
}
if(split.Length > 1)
val = split[1];
if (!ChemicalExists(label)) AddChemicalItem(new ChemicalItem {Id = _chemicalItems.Count + 1,Symbol = label});
ri = new ReportItem();
ri.Chemical = _chemicalItems.FirstOrDefault(ci => ci.Symbol == label);
ri.UoM = val.Contains("%")
? _uoms.FirstOrDefault(uom => uom.Symbol == "%")
: _uoms.FirstOrDefault(uom => uom.Symbol == "mg/g");
if (string.IsNullOrWhiteSpace(val)) return ri;
var value = val.Contains("%") ? split[1].Substring(0, val.Length - 1) : val;
ri.Value = Convert.ToDouble(value);
}
catch (Exception e)
{
// TODO: Document Exception
Console.WriteLine(e.Message);
}
return ri;
}
//private static ChemicalItem GetChemicalItem(string text)
//{
//}
public class Strain
{
public long Id { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
public string OcpId { get; set; }
public bool IsHidden { get; set; } = false;
public virtual ICollection<ChemicalReport> Reports { get; set; }
}
public class Lab
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ChemicalReport> Reports { get; set; }
}
public class ChemicalReport
{
public long Id { get; set; }
[ForeignKey("Lab")]
public long LabId { get; set; }
public virtual Lab Lab { get; set; }
public string SampleId { get; set; }
public DateTime Created { get; set; }
public virtual ICollection<ReportItem> Items { get; set; }
[ForeignKey("Strain")]
public long StrainId { get; set; }
public virtual Strain Strain { get; set; }
}
public class ChemicalItem
{
public long Id { get; set; }
public string Name { get; set; }
public string Symbol { get; set; }
}
public class ReportItem
{
public long Id { get; set; }
[ForeignKey("Chemical")]
public long ChemicalItemId { get; set; }
public virtual ChemicalItem Chemical { get; set; }
public double Value { get; set; }
[ForeignKey("UoM")]
public long UnitOfMeasureId { get; set; }
public virtual UnitOfMeasure UoM { get; set; }
}
public class UnitOfMeasure
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Symbol { get; set; }
}
}
}
namespace Calendar
{
public partial class MainCalendar : Form
{
private JArray items;
private List<String> AMList = new List<String>();
private List<String> PMList = new List<String>();
private List<String> accessToCalendarFilepath = new List<String>();
private List<CalendarModel> people;
private List<List<CalendarModel>> managers = new List<List<CalendarModel>>();
private List<String> userSelection = new List<String>();
private bool authorizedAccess = false;
private String javaScriptFileContainingJSONObject = "";
public MainCalendar()
{
InitializeComponent();
var locationInformation = System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + "location.json";
using (StreamReader file = File.OpenText(locationInformation))
using (JsonTextReader reader = new JsonTextReader(file))
{
JArray o = (JArray)JToken.ReadFrom(reader);
items = o;
}
foreach (var item in items.Children())
{
var itemProperties = item.Children<JProperty>();
// you could do a foreach or a linq here depending on what you need to do exactly with the value
var myElement = itemProperties.FirstOrDefault(x => x.Name == "name");
var myElementValue = myElement.Value; ////This is a JValue type
if(myElementValue.ToString().Contains("AM"))
{
AMList.Add(myElementValue.ToString());
}
if (myElementValue.ToString().Contains("PM"))
{
PMList.Add(myElementValue.ToString());
}
}
mondayAM.DataSource = AMList.ToArray();
tuesdayAM.DataSource = AMList.ToArray();
wednesdayAM.DataSource = AMList.ToArray();
thursdayAM.DataSource = AMList.ToArray();
fridayAM.DataSource = AMList.ToArray();
mondayPM.DataSource = PMList.ToArray();
tuesdayPM.DataSource = PMList.ToArray();
wednesdayPM.DataSource = PMList.ToArray();
thursdayPM.DataSource = PMList.ToArray();
fridayPM.DataSource = PMList.ToArray();
loadAccessControl("accesscontrol.json");
dateTimePicker1.AlwaysChooseMonday(dateTimePicker1.Value);
String dateSelected = dateTimePicker1.Value.ToShortDateString();
findManagerForSelectedDate(dateSelected);
}
public void loadAccessControl(String fileName)
{
var accessControlInformation = Environment.CurrentDirectory + Path.DirectorySeparatorChar + fileName;
List<AccessControl> accounts = JsonConvert.DeserializeObject<List<AccessControl>>(File.ReadAllText(accessControlInformation));
foreach (AccessControl account in accounts)
{
Console.WriteLine(account.accountName);
if (account.accountName.ToLower().Contains(Environment.UserName.ToLower()))
{
foreach (CalendarFile file in account.files)
{
// Console.WriteLine(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "content" + Path.DirectorySeparatorChar + file.Filename);
accessToCalendarFilepath.Add(Environment.CurrentDirectory + Path.DirectorySeparatorChar + "content" + Path.DirectorySeparatorChar + file.Filename);
}
break;
}
}
contentsOfFile();
}
private void contentsOfFile()
{
String line;
foreach(var file in accessToCalendarFilepath)
{
StreamReader contentsOfJSONFile = new StreamReader(file);
while((line = contentsOfJSONFile.ReadLine()) != null)
{
if(line.Contains("var "))
{
javaScriptFileContainingJSONObject = javaScriptFileContainingJSONObject + "[";
}
else if(line.Contains("];"))
{
javaScriptFileContainingJSONObject = javaScriptFileContainingJSONObject + "]";
}
else
{
javaScriptFileContainingJSONObject = javaScriptFileContainingJSONObject + line;
}
}
people = JsonConvert.DeserializeObject<List<CalendarModel>>((string)javaScriptFileContainingJSONObject);
managers.Add(people);
javaScriptFileContainingJSONObject = "";
}
}
private void findManagerForSelectedDate(String dateSelected)
{
dateSelected = dateTimePicker1.Value.ToShortDateString();
List<String> managerNames = new List<String>();
foreach(var item in managers)
{
foreach (var subitem in item)
{
CalendarModel c = subitem;
Console.WriteLine(c.date);
c.name = new CultureInfo("en-US", false).TextInfo.ToTitleCase(c.name);
if (userSelection.Count > 0)
{
foreach (var addedUser in userSelection.ToArray())
{
if (!addedUser.Contains(c.name))
{
userSelection.Add(c.name); // CRASHING HERE
//{"Exception of type 'System.OutOfMemoryException' was thrown."}
}
}
}
else
{
userSelection.Add(c.name);
}
}
}
Console.WriteLine();
}
I keep running out of memory.
The CalendarModel class:
namespace Calendar
{
class CalendarModel
{
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("date")]
public string date { get; set; }
[JsonProperty("title")]
public string title { get; set; }
[JsonProperty("mondayAM")]
public string mondayAM { get; set; }
[JsonProperty("mondayPM")]
public string mondayPM { get; set; }
[JsonProperty("tuesdayAM")]
public string tuesdayAM { get; set; }
[JsonProperty("tuesdayPM")]
public string tuesdayPM { get; set; }
[JsonProperty("wednesdayAM")]
public string wednesdayAM { get; set; }
[JsonProperty("wednesdayPM")]
public string wednesdayPM { get; set; }
[JsonProperty("thursdayAM")]
public string thursdayAM { get; set; }
[JsonProperty("thursdayPM")]
public string thursdayPM { get; set; }
[JsonProperty("fridayAM")]
public string fridayAM { get; set; }
[JsonProperty("fridayPM")]
public string fridayPM { get; set; }
[JsonProperty("saturdayAM")]
public string saturdayAM { get; set; }
[JsonProperty("saturdayPM")]
public string saturdayPM { get; set; }
}
}
I keep crashing at
userSelection.Add(c.name)
Take a close look at what you are doing
foreach (var addedUser in userSelection.ToArray())
{
if (!addedUser.Contains(c.name))
{
userSelection.Add(c.name);
}
}
You are adding to userSelection in the userSelection loop
The test is on !addedUser.Contains
You should not even be able to do that but I think the ToArrray() is letting it happen
So you add Sally
Then then Mark
Then you add Mark again because in the loop Mark != Sally
You are not using List<String> managerNames = new List<String>();
private void findManagerForSelectedDate(String dateSelected)
{
dateSelected = dateTimePicker1.Value.ToShortDateString();
You pass in dateSelected, then overright with dateTimePicker1, and then you don't even use it
Most of you code makes very little sense to me
I'm getting this error on DataBind(), and I don't know why since there shouldn't be anything selected.
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DataTextField = "Title";
DdState.DataValueField = "Title";
DdState.Items.Insert(0, String.Empty);
if (DdState.SelectedItem != null)
{
DdState.SelectedItem.Selected = false;
}
DdState.DataBind();
private IEnumerable<IStateItem> UsStates
{
get
{
var statesFolder = _sitecoreService.GetItem<ISitecoreItem>(ItemReference.BcsUs_ProductData_States.Guid);
if (statesFolder == null)
return new List<IStateItem>();
List<IStateItem> usStates = _sitecoreService.QueryChildren<IStateItem>(statesFolder).OrderBy(s => s.Title).ToList();
return usStates;
}
}
I tried putting in DdState.SelectedIndex = 0 before the DataBind(), but then I got an error that the selected index did not exist. What's going on?
If the DataSource is a list its much easier to implement. So just "convert" the UsStates IEnumerable to a List an then add it to the data source.
DdState.DataSource = UsStates.ToList();
Then choose the property of a list item as binding.
OR
public Form1()
{
InitializeComponent();
DdState.Items.Clear();
DdState.DataSource = UsStates;
DdState.DisplayMember = "Statename";
DdState.SelectedIndex = 0;
}
private List<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
usStates.Add(new IStateItem("California","status1"));
usStates.Add(new IStateItem("Ohio", "status3"));
return usStates;
}
}
private class IStateItem
{
public IStateItem(string statename, string stateStatus)
{
Statename = statename;
StateStatus = stateStatus;
}
public string Statename { get; set; }
public string StateStatus { get; set; }
}
Could there be something wrong with your IStateItem class?
I copy/pasted your code in a new asp.net application, made my own IStateItem class and it works.
using System;
using System.Collections.Generic;
namespace TestIt
{
public partial class Form1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillTheList();
}
private void FillTheList()
{
ddl_TheList.Items.Clear();
ddl_TheList.DataSource = UsStates;
ddl_TheList.DataTextField = "statename";
ddl_TheList.DataValueField = "stateStatus";
//ddl_TheList.Items.Insert(0, String.Empty);
ddl_TheList.DataBind();
ddl_TheList.SelectedIndex = 0;
}
private IEnumerable<IStateItem> UsStates
{
get
{
List<IStateItem> usStates = new List<IStateItem>();
for (int i = 0; i < 10; i++)
{
usStates.Add(new IStateItem { statename = "state #" + i, stateStatus = "cool state bro" });
}
return usStates;
}
}
}
public class IStateItem
{
public string statename { get; set; }
public string stateStatus { get; set; }
}
}
I need to split values from two columns into a datagridview.
You can see a screenshot of my values here:
I need to split match and result columns to have a column for every value.
This is my code:
Class:
using System;
using System.Collections.Generic;
namespace bexscraping
{
public class Bet
{
public string Match { get; set; }
public string Result { get; set; }
public List<string> Odds { get; set; }
public string Date { get; set; }
public Bet()
{
Odds = new List<string>();
}
public override string ToString()
{
String MatchInfo = String.Format("{0}: {1} -> {2}", Date, Match, Result);
String OddsInfo = String.Empty;
foreach (string d in Odds)
OddsInfo += " | " + d;
return MatchInfo + "\n" + OddsInfo;
}
}
}
form1:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using HtmlAgilityPack;
namespace bexscraping
{
public partial class Form1 : Form
{
private List<Bet> Bets;
private Bet SelectedBet { get; set; }
public Form1()
{
InitializeComponent();
dataGridView1.SelectionChanged += DataGridView1_SelectionChanged;
}
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0) {
SelectedBet = (Bet)dataGridView1.SelectedRows[0].DataBoundItem;
if (SelectedBet.Odds.Count > 0) {
textBox1.Text = SelectedBet.Odds[0].ToString();
textBox2.Text = SelectedBet.Odds[1].ToString();
textBox3.Text = SelectedBet.Odds[2].ToString();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
LoadInfo();
if (Bets.Count > 0)
{
SelectedBet = Bets[0];
dataGridView1.DataSource = Bets;
if (SelectedBet.Odds.Count > 0)
{
textBox1.Text = SelectedBet.Odds[0].ToString();
textBox2.Text = SelectedBet.Odds[1].ToString();
textBox3.Text = SelectedBet.Odds[2].ToString();
}
}
}
private void LoadInfo()
{
string url = "http://www.betexplorer.com/soccer/australia/northern-nsw/results/";
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(url);
Bets = new List<Bet>();
// Lettura delle righe
var Rows = doc.DocumentNode.SelectNodes("//tr");
foreach (HtmlNode row in Rows)
{
if (!row.GetAttributeValue("class", "").Contains("rtitle"))
{
if (string.IsNullOrEmpty(row.InnerText))
continue;
Bet rowBet = new Bet();
foreach (HtmlNode node in row.ChildNodes)
{
string data_odd = node.GetAttributeValue("data-odd", "");
if (string.IsNullOrEmpty(data_odd))
{
if (node.GetAttributeValue("class", "").Contains(("first-cell")))
rowBet.Match = node.InnerText.Trim();
var matchTeam = rowBet.Match.Split("-", StringSplitOptions.RemoveEmptyEntries);
rowBet.Home = matchTeam[0];
rowBet.Host = matchTeam[1];
if (node.GetAttributeValue("class", "").Contains(("result")))
rowBet.Result = node.InnerText.Trim();
var matchPoints = rowBet.Result.Split(":", StringSplitOptions.RemoveEmptyEntries);
rowBet.HomePoints = int.Parse(matchPoints[0];
rowBet.HostPoints = int.Parse(matchPoints[1];
if (node.GetAttributeValue("class", "").Contains(("last-cell")))
rowBet.Date = node.InnerText.Trim();
}
else
{
rowBet.Odds.Add(data_odd);
}
}
if (!string.IsNullOrEmpty(rowBet.Match))
Bets.Add(rowBet);
}
}
}
}
}
I hope you can help me. Thanks!
Ok after I try it this is working solution for me.
Probably its diferent namespace but all components have same name.
Form1 class
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using HtmlAgilityPack;
namespace Test
{
public partial class Form1 : Form
{
private List<Bet> Bets;
public Form1()
{
InitializeComponent();
Form1_Load_1();
dataGridView1.SelectionChanged += DataGridView1_SelectionChanged;
}
private Bet SelectedBet { get; set; }
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
SelectedBet = (Bet) dataGridView1.SelectedRows[0].DataBoundItem;
if (SelectedBet.Odds.Count > 0)
{
textBox1.Text = SelectedBet.Odds[0];
textBox2.Text = SelectedBet.Odds[1];
textBox3.Text = SelectedBet.Odds[2];
}
}
}
private void LoadInfo()
{
var url = "http://www.betexplorer.com/soccer/australia/northern-nsw/results/";
var web = new HtmlWeb();
var doc = web.Load(url);
Bets = new List<Bet>();
// Lettura delle righe
var Rows = doc.DocumentNode.SelectNodes("//tr");
foreach (var row in Rows)
{
if (!row.GetAttributeValue("class", "").Contains("rtitle"))
{
if (string.IsNullOrEmpty(row.InnerText))
continue;
var rowBet = new Bet();
foreach (var node in row.ChildNodes)
{
var data_odd = node.GetAttributeValue("data-odd", "");
if (string.IsNullOrEmpty(data_odd))
{
if (node.GetAttributeValue("class", "").Contains("first-cell"))
{
rowBet.Match = node.InnerText.Trim();
var matchTeam = rowBet.Match.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
rowBet.Home = matchTeam[0];
rowBet.Host = matchTeam[1];
}
if (node.GetAttributeValue("class", "").Contains("result"))
{
rowBet.Result = node.InnerText.Trim();
var matchPoints = rowBet.Result.Split(new[] {':'}, StringSplitOptions.RemoveEmptyEntries);
int help;
if (int.TryParse(matchPoints[0], out help))
{
rowBet.HomePoints = help;
}
if (matchPoints.Length == 2 && int.TryParse(matchPoints[1], out help))
{
rowBet.HostPoints = help;
}
}
if (node.GetAttributeValue("class", "").Contains("last-cell"))
rowBet.Date = node.InnerText.Trim();
}
else
{
rowBet.Odds.Add(data_odd);
}
}
if (!string.IsNullOrEmpty(rowBet.Match))
Bets.Add(rowBet);
}
}
}
private void Form1_Load_1()
{
LoadInfo();
if (Bets.Count > 0)
{
SelectedBet = Bets[0];
dataGridView1.DataSource = Bets;
if (SelectedBet.Odds.Count > 0)
{
textBox1.Text = SelectedBet.Odds[0];
textBox2.Text = SelectedBet.Odds[1];
textBox3.Text = SelectedBet.Odds[2];
}
}
}
}
}
Bets class
using System;
using System.Collections.Generic;
namespace Test
{
class Bet
{
public string Match { get; set; }
public string Result { get; set; }
public List<string> Odds { get; set; }
public string Date { get; set; }
public string Home { get; set; }
public string Host { get; set; }
public int HomePoints { get; set; }
public int HostPoints { get; set; }
public Bet()
{
Odds = new List<string>();
}
public override string ToString()
{
String MatchInfo = String.Format("{0}: {1} -> {2}", Date, Match, Result);
String OddsInfo = String.Empty;
foreach (string d in Odds)
OddsInfo += " | " + d;
return MatchInfo + "\n" + OddsInfo;
}
}
}
Basically I have an AVL tree that stores instances of Country class. When I do an inorder traversal of the tree, I am able to see the country details correctly, however I wish to view and modify instances of the country class in a GUI. The issue I am having is I have no idea how to access the class data and display it in something like a listbox. Here is my Country class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace International_Trading_Data
{
class Country : IComparable
{
public string countryName { get; set; }
public double gdp { get; set; }
public double inflation { get; set; }
public double tradeBalance { get; set; }
public int hdiRanking { get; set; }
public LinkedList<string> tradePartners { get; set; }
public string f;
public Country (){
}
public Country(string cname, double g, double i, double t, int h, LinkedList<string> tp)
{
this.countryName = cname;
this.gdp = g;
this.inflation = i;
this.tradeBalance = t;
this.hdiRanking = h;
this.tradePartners = tp;
}
public int CompareTo(object obj)
{
Country temp = (Country)obj;
return countryName.CompareTo(temp.countryName);
}
public override string ToString()
{
foreach (string i in tradePartners)
f += i+",";
return countryName+" "+gdp+" "+" "+inflation+" "+tradeBalance+" "+ hdiRanking+ " "+f;
}
}
}
`
This is where I create instances of the country class:
public void loadFile()
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "CSV Files (*.csv)|*.csv";
open.FilterIndex = 1;
open.Multiselect = true;
if (open.ShowDialog() == DialogResult.OK)
{
string selectedFilePath = open.FileName;
const int MAX_SIZE = 5000;
string[] allLines = new string[MAX_SIZE];
allLines = File.ReadAllLines(selectedFilePath);
foreach (string line in allLines)
{
if (line.StartsWith("Country"))
{
headers = line.Split(',');
}
else
{
string[] columns = line.Split(',');
LinkedList<string> tradePartners = new LinkedList<string>();
string[] partners = columns[5].Split('[', ']', ';');
foreach (string i in partners)
{
if (i != "")
{
tradePartners.AddLast(i);
}
}
countries.InsertItem(new Country(columns[0], Double.Parse(columns[1]),Double.Parse(columns[2]), Double.Parse(columns[3]) ,int.Parse(columns[4]),tradePartners));
}
}
Here is the code for my inorder traversal:
public void InOrder()
{
inOrder(root);
}
private void inOrder(Node<T> tree)
{
if (tree != null)
{
inOrder(tree.Left);
System.Diagnostics.Debug.WriteLine(tree.Data.ToString());
inOrder(tree.Right);
}
This code produces the following output for a few test countries:
Argentina 3 22.7 0.6 45 Brazil,Chile,
Australia 3.3 2.2 -5 2 China,Japan,New_Zealand,
Brazil 3 5.2 -2.2 84 Chile,Argentina,USA,
So I know that my classes are bieng stored correctly in the avl tree.
I am not sure what you are using as a data structure for your countries collection, but assuming its a List for now, you can do the following (NOTE: this sample is just to demonstrate displaying information on a UI for manipulation):
public Form1()
{
InitializeComponent();
List<Country> countries = new List<Country>() {
new Country() { countryName = "Mattopia" , gdp = 1500, inflation = 1.5, f="hi"},
new Country { countryName = "coffeebandit", gdp = 2000, inflation = 1.2, f="hey" }};
listBox1.DisplayMember = "countryName";
listBox1.DataSource = countries;
}
public class Country
{
public string countryName { get; set; }
public double gdp { get; set; }
public double inflation { get; set; }
public double tradeBalance { get; set; }
public int hdiRanking { get; set; }
public LinkedList<string> tradePartners { get; set; }
public string f;
}
Then, you can use the selected index changed event to populate your fields:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Country country = (Country)listBox1.SelectedValue;
//fill up all other GUI controls
textBox1.Text = country.f;
textBox2.Text = country.inflation.ToString();
}
And if you want to process text changes:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Country country = (Country)listBox1.SelectedValue;
if (country != null)
{
country.f = textBox1.Text;
}
}
This will give you the following display:
This should demonstrate the basics of how to edit a class in a WinForms UI.
For more advanced examples, I would recommend using other Events to capture information as your needs change.