将数据表行复制到另一个数据表

时间:2019-04-02 00:09:18

标签: c# datagridview

我试图从dataGridView5.Columns中获得排序的排名列,以适当地替换我的datagridview1列。

理想情况下,我想根据某种逻辑在2个数据表之间找到匹配项:

因此,如果“团队=对抗AND PG”包含PG RANK列标题,那么PG RANK = OPPAgainstRank。图片说一千个单词,所以希望它能比我更好地解释它。

谢谢

enter image description here

  public void DVP()
            {
                var doc = new HtmlWeb().Load("https://basketballmonster.com/dfsdvp.aspx");
                HtmlAgilityPack.HtmlNodeCollection teams = doc.DocumentNode.SelectNodes("//td[@class ='tdc']");
                HtmlAgilityPack.HtmlNodeCollection pg_points = doc.DocumentNode.SelectNodes(".//table[@class='datatable']//tr//td[4]");
                HtmlAgilityPack.HtmlNodeCollection sg_points = doc.DocumentNode.SelectNodes(".//table[@class='datatable']//tr//td[5]");
                HtmlAgilityPack.HtmlNodeCollection sf_points = doc.DocumentNode.SelectNodes(".//table[@class='datatable']//tr//td[6]");
                HtmlAgilityPack.HtmlNodeCollection pf_points = doc.DocumentNode.SelectNodes(".//table[@class='datatable']//tr//td[7]");
                HtmlAgilityPack.HtmlNodeCollection c_points = doc.DocumentNode.SelectNodes(".//table[@class='datatable']//tr//td[8]");
                DataTable dvp = new DataTable();
                dvp.Columns.Add("Teams", typeof(string));
                dvp.Columns.Add("PG", typeof(string));
                dvp.Columns.Add("PG RANK", typeof(int));
                dvp.Columns.Add("SG", typeof(string));
                dvp.Columns.Add("SG RANK", typeof(int));
                dvp.Columns.Add("SF", typeof(string));
                dvp.Columns.Add("SF RANK", typeof(int));
                dvp.Columns.Add("PF", typeof(string));
                dvp.Columns.Add("PF RANK", typeof(int));
                dvp.Columns.Add("C", typeof(string));
                dvp.Columns.Add("C RANK", typeof(int));

                for (int i = 0; i < teams.Count; i++)
                {
                    DataRow row = dvp.NewRow();
                    var aux = i / 30;
                    row["Teams"] = (teams[i].InnerText);
                    row["PG"] = (pg_points[i].InnerText);
                    row["SG"] = (sg_points[i].InnerText);
                    row["SF"] = (sf_points[i].InnerText);
                    row["PF"] = (pf_points[i].InnerText);
                    row["C"] = (c_points[i].InnerText);
                    dvp.Rows.Add(row);
                    dataGridView5.DataSource = null;
                    dataGridView5.DataSource = dvp;

                    foreach (DataGridViewRow row3 in dataGridView5.Rows)
                    {
                        //teams
                        if (row3.Cells["Teams"].Value.ToString().Contains("ATL"))
                        {
                            row3.Cells["Teams"].Value = "Atlanta Hawks";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("BOS"))
                        {
                            row3.Cells["Teams"].Value = "Boston Celtics";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("BKN"))
                        {
                            row3.Cells["Teams"].Value = "Brooklyn Nets";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("CHA"))
                        {
                            row3.Cells["Teams"].Value = "Charlotte Hornets";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("CHI"))
                        {
                            row3.Cells["Teams"].Value = "Chicago Bulls";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("CLE"))
                        {
                            row3.Cells["Teams"].Value = "Cleveland Cavaliers";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("DAL"))
                        {
                            row3.Cells["Teams"].Value = "Dallas Mavericks";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("DEN"))
                        {
                            row3.Cells["Teams"].Value = "Denver Nuggets";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("DET"))
                        {
                            row3.Cells["Teams"].Value = "Detroit Pistons";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("GSW"))
                        {
                            row3.Cells["Teams"].Value = "Golden State Warriors";
                        }

                        if (row3.Cells["Teams"].Value.ToString().Contains("vs HOU"))
                        {
                            row3.Cells["Teams"].Value = "Houston Rockets";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs IND"))
                        {
                            row3.Cells["Teams"].Value = "Indiana Pacers";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs LAC"))
                        {
                            row3.Cells["Teams"].Value = "LA Clippers";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs LAL"))
                        {
                            row3.Cells["Teams"].Value = "Los Angeles Lakers";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs MEM"))
                        {
                            row3.Cells["Teams"].Value = "Memphis Grizzlies";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs MIA"))
                        {
                            row3.Cells["Teams"].Value = "Miami Heat";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs MIL"))
                        {
                            row3.Cells["Teams"].Value = "Milwaukee Bucks";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs MIN"))
                        {
                            row3.Cells["Teams"].Value = "Minnesota Timberwolves";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs NOR"))
                        {
                            row3.Cells["Teams"].Value = "New Orleans Pelicans";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs NYK"))
                        {
                            row3.Cells["Teams"].Value = "New York Knicks";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs OKC"))
                        {
                            row3.Cells["Teams"].Value = "Oklahoma City Thunder";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs ORL"))
                        {
                            row3.Cells["Teams"].Value = "Orlando Magic";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs PHI"))
                        {
                            row3.Cells["Teams"].Value = "Philadelphia Sixers";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs PHO"))
                        {
                            row3.Cells["Teams"].Value = "Phoenix Suns";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs POR"))
                        {
                            row3.Cells["Teams"].Value = "Portland Trail Blazers";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs SAC"))
                        {
                            row3.Cells["Teams"].Value = "Sacramento Kings";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs SAS"))
                        {
                            row3.Cells["Teams"].Value = "San Antonio Spurs";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs TOR"))
                        {
                            row3.Cells["Teams"].Value = "Toronto Raptors";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs UTA"))
                        {
                            row3.Cells["Teams"].Value = "Utah Jazz";
                        }
                        if (row3.Cells["Teams"].Value.ToString().Contains("vs WAS"))
                        {
                            row3.Cells["Teams"].Value = "Washington Wizards";
                        }
                    }
                    foreach (DataGridViewRow row1 in dataGridView1.Rows)
                    {

                        if (row1.Cells["Against"].Value.ToString().Contains(row["Teams"].ToString()) && row1.Cells["Position"].Value.ToString() == "PG")
                        {
                            row1.Cells["OppAgainstRank"].Value = pg_points[i].InnerText;

                        }
                        if (row1.Cells["Against"].Value.ToString().Contains(row["Teams"].ToString()) && row1.Cells["Position"].Value.ToString() == "SG")
                        {
                            row1.Cells["OppAgainstRank"].Value = sg_points[i].InnerText;

                        }
                        if (row1.Cells["Against"].Value.ToString().Contains(row["Teams"].ToString()) && row1.Cells["Position"].Value.ToString() == "SF")
                        {
                            row1.Cells["OppAgainstRank"].Value = sf_points[i].InnerText;

                        }
                        if (row1.Cells["Against"].Value.ToString().Contains(row["Teams"].ToString()) && row1.Cells["Position"].Value.ToString() == "PF")
                        {
                            row1.Cells["OppAgainstRank"].Value = pf_points[i].InnerText;

                        }
                        if (row1.Cells["Against"].Value.ToString().Contains(row["Teams"].ToString()) && row1.Cells["Position"].Value.ToString() == "C")
                        {
                            row1.Cells["OppAgainstRank"].Value = c_points[i].InnerText;

                        }
                    }

                    // ADD RANKING

                    this.dataGridView5.Sort(this.dataGridView5.Columns["PG"], System.ComponentModel.ListSortDirection.Descending);
                    for (int x = 0; x < dataGridView5.Rows.Count; x++)
                    {

                        dataGridView5.Rows[x].Cells["PG RANK"].Value = Convert.ToString(x + 1);

                    }

                    this.dataGridView5.Sort(this.dataGridView5.Columns["SG"], System.ComponentModel.ListSortDirection.Descending);
                    for (int x = 0; x < dataGridView5.Rows.Count; x++)
                    {
                        dataGridView5.Rows[x].Cells["SG RANK"].Value = Convert.ToString(x + 1);

                    }
                    this.dataGridView5.Sort(this.dataGridView5.Columns["SF"], System.ComponentModel.ListSortDirection.Descending);
                    for (int x = 0; x < dataGridView5.Rows.Count; x++)
                    {
                        dataGridView5.Rows[x].Cells["SF RANK"].Value = Convert.ToString(x + 1);

                    }
                    this.dataGridView5.Sort(this.dataGridView5.Columns["PF"], System.ComponentModel.ListSortDirection.Descending);
                    for (int x = 0; x < dataGridView5.Rows.Count; x++)
                    {
                        dataGridView5.Rows[x].Cells["PF RANK"].Value = Convert.ToString(x + 1);

                    }
                    this.dataGridView5.Sort(this.dataGridView5.Columns["C"], System.ComponentModel.ListSortDirection.Descending);
                    for (int x = 0; x < dataGridView5.Rows.Count; x++)
                    {
                        dataGridView5.Rows[x].Cells["C RANK"].Value = Convert.ToString(x + 1);

                    }


                            }
                        } 

1 个答案:

答案 0 :(得分:0)

这不是最好的方法,但是我可以这样工作:

 foreach (DataGridViewRow row in dataGridView5.Rows)
            {

               if (row.Cells["PG RANK"] == row.Cells["PG RANK"] && row.Cells["Teams"].Value.Equals(row.Cells["Teams"].Value))
                    foreach (DataGridViewRow row2 in dataGridView1.Rows)
                    {
                        if (row2.Cells["Against"].Value.Equals(row.Cells["Teams"].Value) && row2.Cells["Position"].Value.Equals("PG"))
                            {
                            row2.Cells["OppAgainstRank"].Value = row.Cells["PG RANK"].Value;

                        }

                    }

            }




            foreach (DataGridViewRow row in dataGridView5.Rows)
            {

                if (row.Cells["SG RANK"] == row.Cells["SG RANK"] && row.Cells["Teams"].Value.Equals(row.Cells["Teams"].Value))
                    foreach (DataGridViewRow row2 in dataGridView1.Rows)
                    {
                        if (row2.Cells["Against"].Value.Equals(row.Cells["Teams"].Value) && row2.Cells["Position"].Value.Equals("SG"))
                        {
                            row2.Cells["OppAgainstRank"].Value = row.Cells["SG RANK"].Value;

                        }

                    }

            }


            foreach (DataGridViewRow row in dataGridView5.Rows)
            {

                if (row.Cells["SF RANK"] == row.Cells["SF RANK"] && row.Cells["Teams"].Value.Equals(row.Cells["Teams"].Value))
                    foreach (DataGridViewRow row2 in dataGridView1.Rows)
                    {
                        if (row2.Cells["Against"].Value.Equals(row.Cells["Teams"].Value) && row2.Cells["Position"].Value.Equals("SF"))
                        {
                            row2.Cells["OppAgainstRank"].Value = row.Cells["SF RANK"].Value;

                        }

                    }

            }




            foreach (DataGridViewRow row in dataGridView5.Rows)
            {

                if (row.Cells["PF RANK"] == row.Cells["PF RANK"] && row.Cells["Teams"].Value.Equals(row.Cells["Teams"].Value))
                    foreach (DataGridViewRow row2 in dataGridView1.Rows)
                    {
                        if (row2.Cells["Against"].Value.Equals(row.Cells["Teams"].Value) && row2.Cells["Position"].Value.Equals("PF"))
                        {
                            row2.Cells["OppAgainstRank"].Value = row.Cells["PF RANK"].Value;

                        }

                    }

            }
            foreach (DataGridViewRow row in dataGridView5.Rows)
            {

                if (row.Cells["C RANK"] == row.Cells["C RANK"] && row.Cells["Teams"].Value.Equals(row.Cells["Teams"].Value))
                    foreach (DataGridViewRow row2 in dataGridView1.Rows)
                    {
                        if (row2.Cells["Against"].Value.Equals(row.Cells["Teams"].Value) && row2.Cells["Position"].Value.Equals("C"))
                        {
                            row2.Cells["OppAgainstRank"].Value = row.Cells["C RANK"].Value;

                        }

                    }

            }