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 に答える