0

I have two data in two different datagrides, I want to take the value from the row, for me to count on the euclidien distance formula. I can take the value in the second datagridview, but I can't get the data in the first datagridview.

i was try to write foreach, but i still over there

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: 'Object reference not set to an instance of an object.'

enter image description here

Gabriel Llorico
  • 1,532
  • 1
  • 12
  • 20

2 Answers2

0

If you have not noticed then row counts start from 0 and not 1.

An example how to calculate Eucleidian distance

public static double EuclideanDistance(Tuple<double, double> first, Tuple<double, double> second)
{
  var difItem1 = first.Item1 - second.Item1;
  var difItem2 = first.Item2 - second.Item2;
  return Math.Sqrt(difItem1 * difItem1 + difItem2 * difItem2);
}

if you have more dimensions then you can create an overload.

public static double EuclideanDistance(Tuple<double, double, double, double> first, Tuple<double, double, double, double> second)
{
    var difItem1 = first.Item1 - second.Item1;
    var difItem2 = first.Item2 - second.Item2;
    var difItem3 = first.Item3 - second.Item3;
    var difItem4 = first.Item4 - second.Item4;
    return Math.Sqrt(difItem1 * difItem1 + difItem2 * difItem2 + difItem3 * difItem3 + difItem4 * difItem4);
}

So i can create a dataset:

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

then loop over the values and calculate the distances and print them out

var results = new List<double>();
for (int i = 0; i < Math.Min(set.Tables["From"].Rows.Count, set.Tables["To"].Rows.Count); i++)
{
    var from = Tuple.Create(
        (double)set.Tables["From"].Rows[i]["a"], (double)set.Tables["From"].Rows[i]["r"],
        (double)set.Tables["From"].Rows[i]["g"], (double)set.Tables["From"].Rows[i]["b"]);
    var to = Tuple.Create(
        (double)set.Tables["To"].Rows[i]["a"], (double)set.Tables["To"].Rows[i]["r"],
        (double)set.Tables["To"].Rows[i]["g"], (double)set.Tables["To"].Rows[i]["b"]);
    results.Add(EuclideanDistance(from, to));
}

Console.WriteLine("Results are: " + string.Join(", ", results));

In your case you would also set

DataGridView1.DataSource = set;
DataGridView1.DataMember = "From";
DataGridView2.DataSource = set;
DataGridView2.DataMember = "To";
Margus
  • 18,332
  • 12
  • 51
  • 101
0

Run safely to avoid null exception

  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);
 }
Giddy Naya
  • 2,202
  • 10
  • 23