2012-08-15 35 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