0

单击 DevExpress 网格中的按钮时,我试图显示图片。网格是动态创建的。它可以很好地显示网格中的数据,但是一旦我尝试获取存储图像名称的单元格的值,我就会收到以下错误:

“指数数组的边界之外。”

以下是我到目前为止的内容:

public GridControl CreateGrid(string[] ColNames, string[] FieldNames, string SqlData, params object[] pars) {
    grid = new GridControl();
    view = new GridView();

    grid.Dock = DockStyle.Fill;
    grid.ViewCollection.Add(view);
    grid.MainView = view;

    view.GridControl = grid;
    view.OptionsView.ShowGroupPanel = false;
    view.OptionsView.ShowAutoFilterRow = false;
    view.OptionsBehavior.Editable = true;
    view.OptionsBehavior.ReadOnly = true;
    view.OptionsSelection.MultiSelect = true;
    view.OptionsSelection.MultiSelect = true;

    for(int i = 0; i < ColNames.Length; i++) {
        GridColumn column = view.Columns.Add();
        column.Caption = ColNames[i];
        column.FieldName = FieldNames[i];
        column.Visible = true;
    }

    table = GlobalDBCTM.DataTableForSql(SqlData, pars);
    grid.DataSource = table;
    grid.BringToFront();
    grid.Tag = view;

    RepositoryItemButtonEdit btnPhoto = new RepositoryItemButtonEdit();
    btnPhoto.AutoHeight = false;
    btnPhoto.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
    btnPhoto.Buttons[0].Image = Properties.Resources.copy;
    btnPhoto.TextEditStyle = TextEditStyles.HideTextEditor;
    btnPhoto.ButtonClick += new ButtonPressedEventHandler(btnPhoto_ButtonClick);
    view.Columns[8].ColumnEdit = btnPhoto;

    grids.Add(grid);

    return grid;
}
void btnPhoto_ButtonClick(object sender, EventArgs e) {
    var id = view.GetSelectedRows()[0];
    var photopath = (string)view.GetRowCellValue(id, view.Columns["PhotoPath"]);

    var path = Globals.PhotoRootPath + "\\" + photopath + ".jpg";
    System.Diagnostics.Process.Start(path);
}
4

1 回答 1

0

我相信问题的原因在于这一行:

var id = view.GetSelectedRows()[0]; // issue when there are no selected rows

为了避免 IndexOutOfRangeException 我建议您使用以下代码:

string photoPath = (string)view.GetFocusedRowCellValue("PhotoPath");

相关帮助链接:
识别行和卡片
获取和设置单元格值
ColumnView.GetSelectedRows
ColumnView.GetFocusedRowCellValue

于 2012-08-16T08:10:18.643 回答