2011-03-04 47 views
3

我是EF中的新成員。當我們使用數據讀取器或數據集時,我們有時會在循環中填充控件的值。像通過循環的實體框架數據綁定

datareader dr=getdata() 
    while(dr.read()) 
    { 
    // in this loop we can populate control with value from datareader 
    } 

    dataset ds =getdata() 
    for(int i=0;i<=ds.tables[0].rows.count-1;i++) 
    { 
    // in this loop we can populate control with value from dataset 
    } 

,所以我只是想知道,當我與EF工作,我又如何能在迭代循環和填充控制與價值。

另一個問題是如何在EF中刪除null。

請幫我用示例代碼來理解事物。感謝

回答

2

這裏是一個人爲的代碼示例,以顯示你怎麼可能實現你所需要的。

// Get all the cars 
List<Car> cars = context.Cars.ToList(); 

// Clear the DataViewGrid 
uiGrid.Rows.Clear(); 

// Populate the grid 
foreach (Car car in cars) 
{ 
    // Add a new row 
    int rowIndex = uiGrid.Rows.Add(); 
    DataGridViewRow newRow = uiGrid.Rows[rowIndex]; 

    // Populate the cells with data for this car 
    newRow.Cells["Make"].Value = car.Make; 
    newRow.Cells["Model"].Value = car.Model; 
    newRow.Cells["Description"].Value = car.Description; 

    // If the price is not null then add it to the price column 
    if (car.Price != null) 
    { 
     newRow.Cells["Price"].Value = car.Price; 
    } 
    else 
    { 
     newRow.Cells["Price"].Value = "No Price Available"; 
    } 
}