如何计算不同 datagridview 中的行值?

我在两个不同的数据网格中有两个数据,我想从行中获取值,以便我依靠欧氏距离公式。我可以获取第二个 datagridview 中的值,但无法获取第一个 datagridview 中的数据。


我试图写 foreach,但我仍然在那里


foreach (DataGridViewRow row1 in dataGridView1.Rows)

{

    foreach (DataGridViewRow row in dataGridView2.Rows)

    {


        double NilaiPixelGrid1 = Convert.ToDouble(row1.Cells[2].Value.ToString());

        double NilaiRedGrid1 = Convert.ToDouble(row1.Cells[3].Value.ToString());

        double NilaiGreenGrid1 = Convert.ToDouble(row1.Cells[4].Value.ToString());

        double NilaiBlueGrid1 = Convert.ToDouble(row1.Cells[5].Value.ToString());


        double NilaiPixel = Convert.ToDouble(row.Cells[2].Value.ToString());

        double NilaiRed = Convert.ToDouble(row.Cells[3].Value.ToString());

        double NilaiGreen = Convert.ToDouble(row.Cells[4].Value.ToString());

        double NilaiBlue = Convert.ToDouble(row.Cells[5].Value.ToString());



        double dist = Math.Pow((NilaiPixelGrid1 - NilaiPixel), 2) +

                    Math.Pow((NilaiRedGrid1 - NilaiRed), 2) +

                    Math.Pow((NilaiGreenGrid1 - NilaiGreen), 2) +

                    Math.Pow((NilaiBlueGrid1 - NilaiBlue), 2);

    }

}

System.NullReferenceException:“未将对象引用设置为对象的实例。”


梦里花落0921
浏览 250回答 2
2回答

凤凰求蛊

安全运行以避免null异常  public double ConvertToDouble(Object obj) {     double convertedValue = 0;     if(obj == null) return convertedValue ;      Double.TryParse(obj.ToString(), out convertedValue);     return convertedValue;  }  foreach (DataGridViewRow outerRow in dataGridView1.Rows) {                      String distValue = "";                    double NilaiPixelGrid1 = ConvertToDouble(outerRow.Cells[2].Value);                    double NilaiRedGrid1 = ConvertToDouble(outerRow.Cells[3].Value);                    double NilaiGreenGrid1 = ConvertToDouble(outerRow.Cells[4].Value);                    double NilaiBlueGrid1 = ConvertToDouble(outerRow.Cells[5].Value); //inner loop   foreach (DataGridViewRow innerRow in dataGridView2.Rows) {                     double NilaiPixel = ConvertToDouble(innerRow.Cells[2].Value);                    double NilaiRed = ConvertToDouble(innerRow.Cells[3].Value);                    double NilaiGreen = ConvertToDouble(innerRow.Cells[4].Value);                    double NilaiBlue = ConvertToDouble(innerRow.Cells[5].Value);                    double dist = Math.Pow((NilaiPixelGrid1 - NilaiPixel), 2) +                    Math.Pow((NilaiRedGrid1 - NilaiRed), 2) +                    Math.Pow((NilaiGreenGrid1 - NilaiGreen), 2) +                    Math.Pow((NilaiBlueGrid1 - NilaiBlue), 2);               //use dist value               distValue += " " + dist;  }   Console.WriteLine(distValue); }

回首忆惘然

如果您没有注意到,那么行计数从 0 开始,而不是从 1 开始。如何计算欧氏距离的示例public static double EuclideanDistance(Tuple<double, double> first, Tuple<double, double> second){&nbsp; var difItem1 = first.Item1 - second.Item1;&nbsp; var difItem2 = first.Item2 - second.Item2;&nbsp; return Math.Sqrt(difItem1 * difItem1 + difItem2 * difItem2);}如果您有更多维度,那么您可以创建过载。public static double EuclideanDistance(Tuple<double, double, double, double> first, Tuple<double, double, double, double> second){&nbsp; &nbsp; var difItem1 = first.Item1 - second.Item1;&nbsp; &nbsp; var difItem2 = first.Item2 - second.Item2;&nbsp; &nbsp; var difItem3 = first.Item3 - second.Item3;&nbsp; &nbsp; var difItem4 = first.Item4 - second.Item4;&nbsp; &nbsp; return Math.Sqrt(difItem1 * difItem1 + difItem2 * difItem2 + difItem3 * difItem3 + difItem4 * difItem4);}所以我可以创建一个数据集:DataTable table1 = new DataTable("From");table1.Columns.Add("a", typeof(double));table1.Columns.Add("r", typeof(double));table1.Columns.Add("g", typeof(double));table1.Columns.Add("b", typeof(double));table1.Rows.Add(255, 23, 234, 55);table1.Rows.Add(255, 26, 234, 55);DataTable table2 = new DataTable("To");table2.Columns.Add("a", typeof(double));table2.Columns.Add("r", typeof(double));table2.Columns.Add("g", typeof(double));table2.Columns.Add("b", typeof(double));table2.Rows.Add(255, 23, 231, 7);table2.Rows.Add(255, 27, 231, 7);DataSet set = new DataSet("Distance");set.Tables.Add(table1);set.Tables.Add(table2);然后循环这些值并计算距离并将其打印出来var results = new List<double>();for (int i = 0; i < Math.Min(set.Tables["From"].Rows.Count, set.Tables["To"].Rows.Count); i++){&nbsp; &nbsp; var from = Tuple.Create(&nbsp; &nbsp; &nbsp; &nbsp; (double)set.Tables["From"].Rows[i]["a"], (double)set.Tables["From"].Rows[i]["r"],&nbsp; &nbsp; &nbsp; &nbsp; (double)set.Tables["From"].Rows[i]["g"], (double)set.Tables["From"].Rows[i]["b"]);&nbsp; &nbsp; var to = Tuple.Create(&nbsp; &nbsp; &nbsp; &nbsp; (double)set.Tables["To"].Rows[i]["a"], (double)set.Tables["To"].Rows[i]["r"],&nbsp; &nbsp; &nbsp; &nbsp; (double)set.Tables["To"].Rows[i]["g"], (double)set.Tables["To"].Rows[i]["b"]);&nbsp; &nbsp; results.Add(EuclideanDistance(from, to));}Console.WriteLine("Results are: " + string.Join(", ", results));在你的情况下,你还可以设置DataGridView1.DataSource = set;DataGridView1.DataMember = "From";DataGridView2.DataSource = set;DataGridView2.DataMember = "To";
打开App,查看更多内容
随时随地看视频慕课网APP