Hi Datamast,
How are you?
After my further research, I think binding the data to an IQueryable collection makes the updating operations complicated. Furthermore, IQueryable collection does not support index-based insert which makes BindingSource.Insert method fails. To make the update operations work fine and enable the insert action, I bind the data to a List<T> of entities and modify some codes of the corresponding operations like binding, deleting, adding, inserting and the DrawItem event. Here is the detailed code example for your references:
=======================================================================
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Binding controls
textBox1.DataBindings.Add(new Binding("Text", bs.DataSource, "ID", true));
textBox2.DataBindings.Add(new Binding("Text", bs.DataSource, "Name", true));
}
TestDBEntities entities;
BindingSource bs = new BindingSource();
// Display the Added, Deleted and Modified entity entries
private void DisplayObjectEntries()
{
var entriesAdded = entities.ObjectStateManager.GetObjectStateEntries(EntityState.Added);
Console.WriteLine("Added Entry number: {0}", entriesAdded.Count());
foreach (var entry in entriesAdded)
{
if (entry.IsRelationship)
{
Console.WriteLine("RelationshipEntry {0}, State: {1}",
entry.EntitySet.Name, entry.State);
}
else
{
Invoice customer = entry.Entity as Invoice;
if (customer != null)
{
Console.WriteLine("Invoice {0}, State: {1}",
customer.ID, entry.State);
}
}
}
var entriesDeleted = entities.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted);
Console.WriteLine("Deleted Entry number: {0}", entriesDeleted.Count());
foreach (var entry in entriesDeleted)
{
if (entry.IsRelationship)
{
Console.WriteLine("RelationshipEntry {0}, State: {1}",
entry.EntitySet.Name, entry.State);
}
else
{
Invoice customer = entry.Entity as Invoice;
if (customer != null)
{
Console.WriteLine("Invoice {0}, State: {1}",
customer.ID, entry.State);
}
}
}
var entriesModified = entities.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
Console.WriteLine("Modified Entry number: {0}", entriesModified.Count());
foreach (var entry in entriesModified)
{
if (entry.IsRelationship)
{
Console.WriteLine("RelationshipEntry {0}, State: {1}",
entry.EntitySet.Name, entry.State);
}
else
{
Invoice customer = entry.Entity as Invoice;
if (customer != null)
{
Console.WriteLine("Invoice {0}, State: {1}",
customer.ID, entry.State);
}
}
}
}
// Bind the data
private void BindData()
{
entities = new TestDBEntities();
var query = from inv in entities.Invoice
select inv;
// Bind the List<Invoice> to the data source
bs.DataSource = query.ToList();
dataRepeater1.DataSource = bs;
}
// Button1 is used to bind the data
private void button1_Click(object sender, EventArgs e)
{
BindData();
}
// Button2 is used to update the database
private void button2_Click(object sender, EventArgs e)
{
// Update the database
try
{
DisplayObjectEntries();
entities.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
// Rebind the data if exceptions is thrown
BindData();
}
}
// Button3 is used to the data to the certain position
private void button3_Click(object sender, EventArgs e)
{
Invoice inv = new Invoice() { ID = 15, Name = "New Invoice" };
inv.Customer = entities.Customer.Where(c => c.ID == 1).FirstOrDefault();
// Insert the data into BindingSource
bs.Insert(4, inv);
// Add the data to the ObjectContext
entities.AddToInvoice(inv);
// Update the database
try
{
DisplayObjectEntries();
entities.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
// Rebind the data if exceptions is thrown
BindData();
}
}
// Button4 is used to add new entity in the last
private void button4_Click(object sender, EventArgs e)
{
Invoice inv = new Invoice() { ID = 15, Name = "New Invoice" };
inv.Customer = entities.Customer.Where(c => c.ID == 1).FirstOrDefault();
// Add the data into the BindingSource
bs.Add(inv);
// Add the data to the ObjectContext
entities.AddToInvoice(inv);
// Update the database
try
{
DisplayObjectEntries();
entities.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
// Rebind the data if exceptions is thrown
BindData();
}
}
// User delete the entity
private void dataRepeater1_UserDeletingItems(object sender, DataRepeaterAddRemoveItemsCancelEventArgs e)
{
try
{
e.Cancel = true;
// Get the entity
Invoice inv = dataRepeater1.BindingContext[dataRepeater1.DataSource].Current as Invoice;
if (inv != null)
{
// Mark the entity as Deleted in ObjectContext
entities.DeleteObject(inv);
// Remove the entity from the BindingSource
bs.Remove(inv);
DisplayObjectEntries();
int result = entities.SaveChanges();
Console.WriteLine("{0} modification performs...", result);
}
}
catch (Exception ex)
{
Console.WriteLine("User Deleting Items...");
Console.WriteLine(ex.ToString());
// Rebind the data if exceptions is thrown
BindData();
}
}
// DrawItem event
private void dataRepeater1_DrawItem(object sender, DataRepeaterItemEventArgs e)
{
DataRepeaterItem row = e.DataRepeaterItem;
int inx = row.ItemIndex;
Invoice inv = bs[inx] as Invoice;
if (inv != null)
{
int id = inv.ID;
row.BackColor = id % 2 == 0 ? Color.Red : Color.Black;
}
}
// User add entity by "Ctrl + N"
private void dataRepeater1_UserAddedItems(object sender, DataRepeaterAddRemoveItemsEventArgs e)
{
int newItemInx = e.ItemIndex;
Invoice inv = bs[newItemInx] as Invoice;
if (inv != null)
{
inv.ID = 15;
inv.Name = "New Invoice";
inv.Customer = entities.Customer.Where(c => c.ID == 1).FirstOrDefault();
}
// Mark the entity as Added in the ObjectContext
entities.AddToInvoice(inv);
DisplayObjectEntries();
// Update the database
try
{
entities.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
// Rebind the data if exceptions is thrown
BindData();
}
}
}
=======================================================================
You can modify these codes to fit your own design target. Important to note: The insert operation is only for the UI level, after load the data from the database, the order is determined by the data records order in the database.
If you have any questions, please feel free to let me know.
Have a great day!
Best Regards,
Lingzhi
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at
fbmsdn@microsoft.com.