2012-08-15 53 views
0

我试图在DevExpress网格中单击按钮时显示图片。网格是动态创建的。它只要我试图让其中图像名称存储我收到以下错误的单元格的值显示在网格精细的数据,但:无法从动态创建的Devexpress Grid中获取rowindex

"Index was outside the bounds of the array."

下面是我到目前为止有:

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); 
} 
+0

的WinForms或WebForms的??? – 2012-08-16 08:22:39

回答

0

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

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

为了避免IndexOutOfRangeException我建议你使用下面的代码:

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

相关的帮助链接:
Identifying Rows and Cards
Obtaining and Setting Cell Values
ColumnView.GetSelectedRows
ColumnView.GetFocusedRowCellValue

+0

嗨,试过你的“(字符串)view.GetFocusedRowCellValue”的建议,但它也没有工作。在进一步调查之后,似乎最后创建的网格得到了重点关注。我试图把“grid.focus();”在一个事件处理程序中专注于被点击的网格,但似乎什么都不做。 – Wraith972 2012-08-20 07:50:19

+0

@ Wraith972这是非常奇怪的...(字符串)view.GetFocusedRowCellValue(fieldname)完美地工作给我。我正在使用基于您的代码段的代码。 – DmitryG 2012-08-20 08:02:50