3

経度と緯度の値を含む dataGridView があります。

値を に渡して使用する必要がありますDoSomethingMethod()

私はdataGridViewに慣れていないので、dataGridViewからすべての値をコピーして処理しますList<>

以下は、dataGridView内のデータを操作する私のコードです

Public Main()
{
    List<double> lst_Long = new List<double>();
    List<double> lst_Latt = new List<double>();

    for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
    {
        lst_Long.Add(Convert.ToDouble(dataGridView1.Rows[rows].Cells["Long"].Value));
        lst_Latt.Add(Convert.ToDouble(dataGridView1.Rows[rows].Cells["Latt"].Value));            
    }

    int ColumnCount = lst_Long.Count - 1;
    int RowCount = lst_Row.Count - 1;

    //Pass the list to DoSomethingMethod
    DoSomethingMethod(lst_Long , lst_Latt, ColumnCount, RowCount);
}

DoSomethingMethod(List<double> lst_long, List<double> lst_latt, int ColumnCount, int RowCount)
{
    for (int iColumn = 0; iColumn < ColumnCount; iColumn++)
    {
        for (int iRow = 0; iRow < RowCount; iRow++)
        {
             // access my latitude value and longitude value here
             double myX = lst_latt[iRow]
             double myY = lst_long[iRow]

            // do some processing here with the value, lets say....
            double myNewX = myX + 10;
            double myNewY = myY + 10;

            PassNewDataToAnotherMethod( myNewX , myNewY );
        }

    }
}

Columns["Long"]dataGridView からセル値を直接使用して取得し、 dataGridViewColumns["Latt"]からリストに値をコピーせずに同様のことを行う他の方法があるかどうかを尋ねたいと思いますか?

ありがとう〜

更新:以下は私のDGVテーブルです(データはランダムであり、実際のものではありません)

ここに画像の説明を入力

自分の回答を更新すると、この質問は終了します

// Pass the whole dataGridView1 into method and access the value through row count.
DoSomethingMethod(DataGridView dataGridView1)
{
    for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
    {
        double myX = Convert.ToDouble(dataGridView1.Rows[rows].Cells["Latt"].Value);
        double myY = Convert.ToDouble(dataGridView1.Rows[rows].Cells["Long"].Value);
    }
}
4

3 に答える 3

1

あなたが実際に何を達成しようとしているのかわかりません。これがもう1つの解決策です。このソリューションがお役に立てば幸いです。

以下に示すように、linq を使用してタプルのリストを作成し、それを NewDoSomething メソッドに送信できます。

    Public Main()
    {   
        //Generate a list of Tuples using linq
        var tuples = from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                     select new Tuple<double, double>(
                         Convert.ToDouble(row.Cells["longitude"].Value), 
                         Convert.ToDouble(row.Cells["latitude"].Value));

        //Pass the tuple list to DoSomethingMethod
        DoSomethingMethod(tuples);
    }

    private void DoSomeThing(IEnumerable<Tuple<double, double>> tuples)
    {
        //Iterate through the tuples
        foreach (Tuple<double, double> tuple in tuples)
        {
            double myX = tuple.Item1;
            double myY = tuple.Item2;
        }
    }

もちろん、このコードは改良することができます。これは、使用できるアプローチを示しているだけです。

于 2013-06-04T07:03:10.287 に答える
0

DataTableをデータソースとして使用している場合は、次のことを試してください。

public void Main()
{
    DoSomethingMethod((DataTable)dataGridView1.DataSource);
}

private void DoSomethingMethod(DataTable dataTable)
{
    foreach (DataRow row in dataTable.Rows)
    {
        double myX = Convert.ToDouble(row["Latt"]);
        double myY = Convert.ToDouble(row["Long"]);
    }
}
于 2013-06-04T07:15:09.583 に答える