我正在抓取一个网页,我将大部分值显示在datagridview中,只显示三个值到文本框中。我也希望将它们展示到datagridview中,但我不知道如何做到这一点。 这是我的代码:
Form1.cs的
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];
}
}
}
}
}
Bet.cs
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;
}
}
}
编辑:查看下面的图片:
我想删除文本框,并且我想再添加三列来显示那些值。
EDIT2:每一行都有不同的文本框值,当我抓取数据时,我希望获得该值并将其与其他值组合在一起。
答案 0 :(得分:1)
这可行吗?添加到您的Bet.cs
课程:
public string odd1
{
get { return Odds[0]; }
set { Odds[0] = value; }
}
public string oddX
{
get { return Odds[1]; }
set { Odds[1] = value; }
}
public string odd2
{
get { return Odds[2]; }
set { Odds[2] = value; }
}
确保仅在将各自的奇数添加到列表中时才调用它们。如果未添加奇数,则会出现Index out of bounds
。
我相信这些价值会出现。
我希望这会有所帮助。
修改强>
“我不明白我如何应用你的代码。你能解释一下吗?”当然。这样做:
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 string odd1 // Here
{
get { return Odds[0]; }
set { Odds[0] = value; }
}
public string oddX // Here
{
get { return Odds[1]; }
set { Odds[1] = value; }
}
public string odd2 // Here
{
get { return Odds[2]; }
set { Odds[2] = value; }
}
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;
}
}
我将三个值的类型更改为字符串而不是int。由于列表Odds
是字符串列表。
修改2
你能试试吗?
public string odd1
{
get { return (Odds.Count >= 3) ? Odds[0] : "error"; }
set { if (Odds.Count >= 3) Odds[0] = value; }
}
public string oddX
{
get { return (Odds.Count >= 3) ? Odds[1] : "error"; }
set { if (Odds.Count >= 3) Odds[1] = value; }
}
public string odd2
{
get { return (Odds.Count >= 3) ? Odds[2] : "error"; }
set { if (Odds.Count >= 3) Odds[2] = value; }
}