使用devexpress gridview自动在列中添加1到4之间的数字?

时间:2014-11-14 16:26:58

标签: loops gridview devexpress

我正在使用导入Excel工作表的DevExpress GridView制作一个c#Winform应用程序。在它上面有11列和许多行。我希望能够对它进行排序并将其分组,这就是为什么我需要使用按钮在名为(Break)的列中添加自动数字(1,2,3& 4)的原因。我搜索了很多并试图做一个循环,但我失败可以帮助我吗?

   DevExpress.XtraGrid.Columns.GridColumn col = gridView2.Columns.ColumnByFieldName("Break");
          for( int i = 0; (i < 5)
          {

          i++;


            col = i.ToString(); 

           }

1 个答案:

答案 0 :(得分:-1)

听起来您正在尝试将单元格值设置为i.ToString(),而是将其分配给实际的Break列,而不是将其设置为单元格的值。您需要遍历网格中的行。这还没有经过测试,但这些方面的内容应该有效:

Int32 count = 0;
public object GetNextNum()
{
    if (count >= 5)
        count = 0;
    count++;
    return count;
}

DevExpress.XtraGrid.Columns.GridColumn col = gridView2.Columns.ColumnByFieldName("Break");

for (int rowHandle = 0; rowHandle < gridView.RowCount; rowHandle++)
{
    grdView2.SetRowCellValue(rowHandle, col, GetNextNum())
}

请注意,这只会处理正行rowHandles,因此它不会为任何组标题设置单元格值,因为它们具有负行句柄(即顶部组的行句柄为-1。以下组句柄为 - 2,等等。)

查看这篇文章,其中有一个遍历带有和不带分组列的网格的示例,因为它可能很有用:https://www.devexpress.com/Support/Center/Question/Details/A1245

private static void TraverseRows(GridView gridView)
{
    if (gridView.GroupCount == 0)
        for (int rowHandle = 0; rowHandle < gridView.RowCount; rowHandle++)
        {
            foreach (GridColumn gc in gridView.Columns)
                WriteString(String.Format("{0} ", gridView.GetRowCellDisplayText(rowHandle, gc)));
            WriteString("\r\n");
        }
    else
    {
        // Get the list of grouped columns
        List<GridColumn> groupedColumnsList = new List<GridColumn>();
        foreach (GridColumn groupedColumn in gridView.GroupedColumns)
            groupedColumnsList.Add(groupedColumn);

        for (int rowHandle = -1; gridView.IsValidRowHandle(rowHandle); rowHandle--)
        {
            WriteString(gridView.GetGroupRowDisplayText(rowHandle) + "\r\n");

            if (gridView.GetChildRowHandle(rowHandle, 0) > -1)

                for (int childRowHandle = 0; childRowHandle < gridView.GetChildRowCount(rowHandle); childRowHandle++)
                {
                    WriteString("-- ");
                    foreach (GridColumn gc in gridView.Columns)
                    {
                        if (groupedColumnsList.Contains(gc)) continue;
                        WriteString(String.Format("{0} ", gridView.GetRowCellDisplayText(gridView.GetChildRowHandle(rowHandle, childRowHandle), gc)));
                    }
                    WriteString("\r\n");
                }
        }
    }
}

本文还解释了使用ColumnView.SetFocusedValue,ColumnView.SetFocusedRowCellValue,ColumnView.SetRowCellValue和ColumnView.EditingValue:https://documentation.devexpress.com/#windowsforms/CustomDocument753

在DevEpxress网格视图中设置单元格行值