循环选择行C#DevExpress

时间:2017-07-31 11:19:35

标签: c# count devexpress selected

我有一个为所有行设置列值的函数:

enter image description here

设置此代码的代码:

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

现在的问题是,我希望它只为所选的行设置它。

我尝试使用.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);
                }
            }

如果我选择3个底行,则前3个会更新。为什么是这样?

1 个答案:

答案 0 :(得分:1)

您要将所有选定的行添加到selectedRows ArrayList。但在那之后,你没有用它做任何事情。 我想你想要的是(我从未使用devexpress控件)正在使用那些选定的RowHandle将它传递给GetDetailView方法。根据{{​​3}}文档,该方法返回所选行的int句柄,因此您的代码应如下所示:

首先,您必须保存DataRow句柄,而不是DataRow本身,因此您必须在此行中更改代码:

selectedRows.Add(gridView.GetDataRow(gridView.GetSelectedRow‌​s()[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);
     } 
}