2013-01-03 145 views
1

獲取的價值我用這個代碼來生成在我的GridView從DevExpress的孩子的GridView

string sql = "Sql Query"; 
string sqlCredit= "Sql Query"; 
string sqlCreditPayment = "Sql Query"; 

    SqlDataAdapter da = new SqlDataAdapter(); 

    DataSet ds = new DataSet(); 
    ds.EnforceConstraints = false; 
    ds.DataSetName = "Receivables"; 

    ds.Tables.Add((con.ShowResult(sql, ref da)).Tables[0].Copy()); 
    ds.Tables[0].TableName = "dtReceivables"; 

    ds.Tables.Add((con.ShowResult(sqlCredit, ref da)).Tables[0].Copy()); 
    ds.Tables[1].TableName = "dtCredit"; 
    ds.Tables[1].Columns[1].ColumnMapping = MappingType.Hidden; 
    ds.Tables[1].Columns[7].ColumnMapping = MappingType.Hidden; 

    ds.Tables.Add((con.ShowResult(sqlCreditPayment, ref da)).Tables[0].Copy()); 
    ds.Tables[2].TableName = "dtCreditPayment"; 
    ds.Tables[2].Columns[0].ColumnMapping = MappingType.Hidden; 

    DataRelation dr0 = new DataRelation("CreditList", ds.Tables[0].Columns["Id"], ds.Tables[1].Columns["DocSupplierId"]); 

    ds.Relations.Add(dr0); 


    DataRelation dr1 = new DataRelation("CreditPaymentList", ds.Tables[1].Columns["Id"], ds.Tables[2].Columns["SourceId"]); 
    ds.Relations.Add(dr1); 



    slipDashBoard.DataSource = ds.Tables["dtReceivables"]; 

    slipDashBoard.ForceInitialize(); 
    gridView1.BestFitColumns(); 

傢伙的數據並輸出。請幫助。當我點擊GridView的孩子時,我想實現這樣的事情。日Thnx預先

enter image description here

回答

6

在這種情況下的主要思想是獲得該被點擊在GridView類的一個實例。 XtraGrid會創建在設計時創建的模式View的克隆,並使用這些克隆來顯示數據。這裏是一個應該工作的代碼:

GridView gridView = sender as GridView; 
var value = gridView.GetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["Num")); 
MessageBox.Show(value.ToString()); 

因爲你的孩子的GridView會自動創建,有兩種方法:

1)處理GridControl的Click事件處理程序:

private void gridControl1_Click(object sender, EventArgs e) { 
    GridControl grid = sender as GridControl; 
    Point p = new Point(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y); 
    GridView gridView = grid.GetViewAt(p) as GridView; 
    if(gridView != null) 
     MessageBox.Show(gridView.GetFocusedRowCellDisplayText("Num")); 
} 

2)處理GridView1 MasterRowExpanded事件處理程序:

private void gridView1_MasterRowExpanded(object sender, CustomMasterRowEventArgs e) { 
     GridView master = sender as GridView; 
     GridView detail = master.GetDetailView(e.RowHandle, e.RelationIndex) as GridView; 
     detail.Click += new EventHandler(detail_Click); 
    } 

    void detail_Click(object sender, EventArgs e) { 
      GridView gridView = sender as GridView; 
var value = gridView.GetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["Num")); 
MessageBox.Show(value.ToString()); 
    } 
+0

我應該在哪裏添加這段代碼? gridView1_Click僅在父級gridView觸發。我的孩子gridview是在運行時創建的。 – Snippet

+0

@Snippet,我已經更新了答案 – platon

+0

非常感謝你的幫助.. – Snippet

0

如果您創建網格在運行時你有一個像gridview2的實例。現在你可以添加點擊事件gridview2.Click += new EventHandler(gridview2_Click);

然後你會得到某事。像這樣:

void view_Click(object sender, EventArgs e) 
    { 
     //take the code from platons post... 
    }