我是EF的新手。当我们使用datareader或数据集时,我们有时会在循环中填充控件的值。喜欢
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。
请帮我提供示例代码以了解相关内容。感谢
答案 0 :(得分: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";
}
}
答案 1 :(得分:1)