2013-04-03 65 views
0

我已經開發了一個XtraGrid控件。但是,當winform加載時,我無法獲得默認選定行的ID。我知道如何在用戶點擊網格時獲得它。如何獲得Devexpress XtraGrid控件選定行

下面是代碼快照:

private void Form1_Load(object sender, EventArgs e) 
    { 
    grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList()); 

    ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID")); 
    XtraMessageBox.Show(ID.ToString()); 
    } 


    public BindingSource bindData(object obj) 
    { 
     BindingSource ctBinding; 
     try 
     { 
      ctBinding = new BindingSource(); 

      ctBinding.DataSource = obj; 

      return ctBinding; 
     } 
     catch (Exception ex) 
     { 
      XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      return null; 
     } 
    }    
+0

'Table1'中的類型是什麼? –

+0

它是一個使用LINQ to SQL訪問的表對象 – aby

+0

您可以指定您遇到的問題。我不認爲我明白你的需要。 –

回答

0

如果我理解正確的話,你需要的東西是這樣的:

private void Form1_Shown(object sender, EventArgs e) 
    { 
    grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList()); 

    var item = gridView.GetFocusedRow() as YourDataType 
    if(item != null) 
    { 
     ID = item.ID; 
     XtraMessageBox.Show(ID.ToString()); 
    } 
    } 

假設你的bindData返回某種形式的類型集合。

** **更新

移動代碼form_Shown似乎這樣的伎倆。

+0

@aby這個答案適合你嗎? –

相關問題