Entity Framework привязка данных через цикл - PullRequest
3 голосов
/ 04 марта 2011

Я новичок в 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.

Пожалуйста, помогите мне с примером кода, чтобы понять вещи.спасибо

Ответы [ 2 ]

2 голосов
/ 04 марта 2011

Вот пример надуманного кода, показывающий, как вы можете достичь того, что вам нужно.

// 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 голос
/ 04 марта 2011
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...