c# - Entity Framework 数据绑定(bind)通过循环

标签 c# entity-framework

我是 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/

相关文章:

entity-framework - 按集合属性对动态 LINQ 进行排序

c# - 首次使用 EF6 Code First 运行应用程序时播种数据库出现问题

c# - 努力抛出 InvalidOperationException : Sequence contains more than one matching element

c# - List.Find<T>() 即使谓词匹配也返回 null

c# - 如何突出显示 ListView 中的项目?

c# - Visual Studio 如何在本地运行 Web 服务?

c# - ASP.NET MVC 5 页面在数据库调用时挂起

c# - 序列化对象的自定义 $type 值

c# - 具有延迟执行和 SQL 端分组的 EF Core Group By

c# - Entity Framework 到远程服务器(连接字符串)