C# 如何将dataGridView中的整列值传递到方法中?
我有一个包含经度和纬度值的dataGridView 我需要通过将值传递到C# 如何将dataGridView中的整列值传递到方法中?,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个包含经度和纬度值的dataGridView 我需要通过将值传递到DoSomethingMethod()来使用它 我不熟悉dataGridView,因此我复制dataGridView中的所有值,并使用List 下面是我在dataGridView中操作数据的代码 Public Main() { List<double> lst_Long = new List<double>(); List<double> lst_Latt = new Li
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 );
}
}
}
实现这一点的一种方法(我想到的)是将BindingSource
与自定义模型一起使用,所以您不需要对行进行操作,而需要对数据绑定模型进行操作。诸如此类:
public class Location
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
//绑定网格
var bs = new BindingSource();
bs.DataSource = listOfLocations; // List<Location>
dataGridView1.DataSource = bs; // thanks to binding source all changes in datagrid are
// propagated to underlying data source (ie. List<Location>
var bs=new BindingSource();
bs.DataSource=位置列表;//列表
dataGridView1.DataSource=bs;//由于绑定源,datagrid中的所有更改都是
//传播到基础数据源(即列表
然后,您可以只操作位置列表,而无需自己访问单元格中的值。我不确定您实际想要实现的目标。这里还有一个解决方案。希望此解决方案对您有用 您可以使用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;
}
}
Public Main()
{
//使用linq生成元组列表
var tuples=来自dataGridView1.Rows.Cast()中的行
选择新元组(
Convert.ToDouble(行单元格[“经度”].Value),
Convert.ToDouble(行单元格[“纬度”].Value));
//将元组列表传递给DoSomethingMethod
DoSomethingMethod(元组);
}
私有void DoSomeThing(IEnumerable元组)
{
//遍历元组
foreach(元组中的元组)
{
double myX=tuple.Item1;
double myY=tuple.Item2;
}
}
当然,这段代码可以改进。这只是显示了您可以使用的方法。如果您使用
数据表作为数据源,您可以尝试以下方法:
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"]);
}
}
您需要在DoSomethingMethod
方法中处理所有DataGridView
的单元格吗?一行后面的对象是什么?不同的说法是:DGV的数据源是什么?您应该尝试使用原始对象而不是研磨中的值。是的。我需要。实际上我可以直接将整个“DataGridView”传递到方法。并访问“DataGridView”中的值,这对我也很好。你不能使用绑定网格进行处理的源吗?可能源是用户手动输入的。我的值在DataGridView表中,我需要读取数据并处理数据。可能我应该更新更多详细信息。我理解。我的方法允许您在模型上而不是网格上操作,但用户可以访问/更改/删除网格中的值,这些值将自动传播到模型中。@jhyap这是正确的方法。不要将视图实现作为业务逻辑的基本要素。您应该能够在没有网格的情况下实现所需的功能。只需要数据和方法。网格只是一个工具,可以通过用户输入显示和修改数据。想象一下,如果有一天你决定使用第三方网格组件会发生什么。位置列表实际上来自excel文件。我使用OleDbConnection
打开excel文件,并将excel表绑定到dataGridView1。数据源de>。因此,我在dataGridView1
中有我的数据表,我可以使用它。但是您提醒我注意逻辑问题,我应该直接从文件中访问数据,而不是将其放入dataGridView并访问它。Erm..这应该是另一个问题:)@jhyapOleDbConnection
填充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"]);
}
}