2016-07-26 9 views
1

Ich muss Werte aus zwei Spalten in eine Datagridview teilen. Sie können einen Screenshot von meinen Werten sehen hier:So teilen Sie Daten in Datagridview

enter image description here

Ich brauche Spalten Spiel und Ergebnis aufzuspalten eine Spalte für jeden Wert zu haben.

Dies ist mein Code:

Klasse:

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); 
        } 

       } 

      } 


     } 


     } 

Ich hoffe, dass Sie mir helfen können. Vielen Dank!

+0

Sind Sie versuchen, Werte aus der Datagridview-Recht zu bekommen? –

+0

Versuchen Sie, eine Pivot-Tabelle zu erstellen? In welcher Spalte möchten Sie pivotieren (Ergebnis oder Datum)? – jdweng

+0

@FatihTAN: Ich habe mein Foto bearbeitet. Zum Beispiel hätte ich gern "Broadmeadow in der ersten Spalte," Hamilton Olympic "in der zweiten Spalte," 1 "in der dritten und die andere" 1 "in der vierten Spalte. Ich hoffe, es ist jetzt klarer :) – Marci

Antwort

1

Ok, nachdem ich es versucht habe funktioniert das für mich. Wahrscheinlich ist sein unterschiedlicher Namespace, aber alle Komponenten haben denselben Namen. Klasse Form1

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]; 
      } 
     } 
    } 
} 
} 

Wetten Klasse

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; 
    } 

} 
} 
+0

ok, es ist jetzt work! Vielen Dank CH! – Marci