我是 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。
请帮助我使用示例代码来理解这些东西。谢谢
最佳答案
这是一个人为设计的代码示例,用于展示您如何实现您的要求。
// 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";
}
}
关于c# - Entity Framework 数据绑定(bind)通过循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5190169/