Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework databind through loop

i am new in EF.when we work with datareader or dataset then sometime we populate control's value in a loop. like

  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
  }

so i just want to know when i work with EF then how can i iterate in loop and populate controls with value.

another question is how to chek null in EF.

please help me with sample code to understand the things. thanks

like image 560
Thomas Avatar asked Mar 09 '26 19:03

Thomas


2 Answers

Here is a contrived code example to show how you might achieve what you require.

// 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";
    }
}
like image 109
Martin Avatar answered Mar 11 '26 09:03

Martin


This will help:

http://www.datasprings.com/resources/articles-information/a-quick-guide-to-using-entity-framework

like image 43
Mamta D Avatar answered Mar 11 '26 07:03

Mamta D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!