循环遍历选定的行 C# DevExpress
Loop through selected rows C# DevExpress
我有一个函数可以为所有行设置列值:
设置这个的代码:
//Update the engineers for all rows
Btn_ValidateClick_ItemClick(object sender,ItemClickEventArgs e)
{
UpdateTotalTime(gridView);
}
private void UpdateEngineers(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
//Column name that need to be updated (set)
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns.ColumnByFieldName("Engineers");
try
{
int dataRowCount = View.DataRowCount;
for (int i = 0; i < dataRowCount; i++)
{
GridView detail = (GridView)gridView.GetDetailView(i, 0);
string language = gridView.GetRowCellValue(i, "Language").ToString();
for (int y = 0; y < gridView.GetDetailView(i, 0).RowCount; y++)
{
//Add all values found in a detail column to an arraylist
values.Add(detail.GetRowCellValue(y, "EngineerInitials").ToString());
}
if (values.Count >0 )
object t = //string join ...
View.SetRowCellValue(i, col, t);
}
else
{
object t = "No engineers"
View.SetRowCellValue(i, col, t);
}
}
}
}
}
问题是现在,我只想为 selected 的行设置它。
我尝试使用 .GetSelectedRows()
函数并将行添加到 ArrayList
,但这确实不允许定制:
private void UpdateTotalTime(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
ArrayList selectedRows = new ArrayList();
for (int i = 0; i < gridView.SelectedRowsCount; i++)
{
if (gridView.GetSelectedRows()[i] >= 0)
selectedRows.Add(gridView.GetDataRow(gridView.GetSelectedRows()[i]));
}
try
{
int count = View.GetSelectedRows().Count();
for (int i = 0; i < selectedRows.Count; i++)
{
//This gets the first row of the count, not the first selected row
GridView detail = (GridView)gridView.GetDetailView(i,0);
}
}
如果我 select 底部的 3 行,前 3 行会更新。为什么是这样?
您正在将所有选定的行添加到您的 selectedRows
ArrayList
。但在那之后,你就不会用它做任何事了。
我猜你想要什么(我从未使用过 devexpress
控件)是使用那些 selectedrows RowHandle
将其传递给 GetDetailView
方法。根据 GetSelectedRows
文档,方法 returns 是所选行的 int 句柄,因此您的代码应如下所示:
首先,您必须保存 DataRow
句柄,而不是 DataRow
本身,因此您必须在代码中更改此行:
selectedRows.Add(gridView.GetDataRow(gridView.GetSelectedRows()[i]));
进入这个:
selectedRows.Add(gridView.GetSelectedRows()[i]);
然后,将循环更改为:
for (int i = 0; i < selectedRows.Count; i++)
{
int rowHandle = (int)selectedRows[i];
GridView detail = (GridView)gridView.GetDetailView(rowHandle,0);
}
事实上,您可以在一个循环中完成所有操作:
private void UpdateTotalTime(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
for (int i = 0; i < gridView.SelectedRowsCount; i++)
{
int rowHandle = gridView.GetSelectedRows()[i];
GridView detail = (GridView)gridView.GetDetailView(rowHandle,0);
}
}
我有一个函数可以为所有行设置列值:
设置这个的代码:
//Update the engineers for all rows
Btn_ValidateClick_ItemClick(object sender,ItemClickEventArgs e)
{
UpdateTotalTime(gridView);
}
private void UpdateEngineers(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
//Column name that need to be updated (set)
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns.ColumnByFieldName("Engineers");
try
{
int dataRowCount = View.DataRowCount;
for (int i = 0; i < dataRowCount; i++)
{
GridView detail = (GridView)gridView.GetDetailView(i, 0);
string language = gridView.GetRowCellValue(i, "Language").ToString();
for (int y = 0; y < gridView.GetDetailView(i, 0).RowCount; y++)
{
//Add all values found in a detail column to an arraylist
values.Add(detail.GetRowCellValue(y, "EngineerInitials").ToString());
}
if (values.Count >0 )
object t = //string join ...
View.SetRowCellValue(i, col, t);
}
else
{
object t = "No engineers"
View.SetRowCellValue(i, col, t);
}
}
}
}
}
问题是现在,我只想为 selected 的行设置它。
我尝试使用 .GetSelectedRows()
函数并将行添加到 ArrayList
,但这确实不允许定制:
private void UpdateTotalTime(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
ArrayList selectedRows = new ArrayList();
for (int i = 0; i < gridView.SelectedRowsCount; i++)
{
if (gridView.GetSelectedRows()[i] >= 0)
selectedRows.Add(gridView.GetDataRow(gridView.GetSelectedRows()[i]));
}
try
{
int count = View.GetSelectedRows().Count();
for (int i = 0; i < selectedRows.Count; i++)
{
//This gets the first row of the count, not the first selected row
GridView detail = (GridView)gridView.GetDetailView(i,0);
}
}
如果我 select 底部的 3 行,前 3 行会更新。为什么是这样?
您正在将所有选定的行添加到您的 selectedRows
ArrayList
。但在那之后,你就不会用它做任何事了。
我猜你想要什么(我从未使用过 devexpress
控件)是使用那些 selectedrows RowHandle
将其传递给 GetDetailView
方法。根据 GetSelectedRows
文档,方法 returns 是所选行的 int 句柄,因此您的代码应如下所示:
首先,您必须保存 DataRow
句柄,而不是 DataRow
本身,因此您必须在代码中更改此行:
selectedRows.Add(gridView.GetDataRow(gridView.GetSelectedRows()[i]));
进入这个:
selectedRows.Add(gridView.GetSelectedRows()[i]);
然后,将循环更改为:
for (int i = 0; i < selectedRows.Count; i++)
{
int rowHandle = (int)selectedRows[i];
GridView detail = (GridView)gridView.GetDetailView(rowHandle,0);
}
事实上,您可以在一个循环中完成所有操作:
private void UpdateTotalTime(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
for (int i = 0; i < gridView.SelectedRowsCount; i++)
{
int rowHandle = gridView.GetSelectedRows()[i];
GridView detail = (GridView)gridView.GetDetailView(rowHandle,0);
}
}