Click here to Skip to main content
15,507,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My app uses EntityFrameWork 6.x in VS 2019 Ver 16.11.18

I bind a datagridview control to a bindingsource expecting all will be good. Problem is the grid allows user to enter more characters than the database table column allows. When this happens, an exception is thrown. OK - fair enough.

I have searched & searched for a way to limit the number of characters the user can enter but have failed to find a way. Obviously, MaxInputProperty is not available because of the binding.

Any help will be very much appreciated, TIA Joe

What I have tried:

C#
private void UpStation_Load(object sender, EventArgs e)
    {
    hrc = new HRContactsEntities();
                         
    var stx = from item in hrc.Stations
              where item.S_oper == MyParentForm.oper
              select item;

    bSrc.DataSource = stx.ToList();
    bNav.BindingSource = bSrc;
    bNav.Visible = true;
    dgvUp.DataSource = bSrc;           
    }  // end of method       

private void loadBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {            
    Validate();
    bSrc.EndEdit();
    try
    {
    hrc.SaveChanges();
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException ex)
    {
    foreach (var eve in ex.EntityValidationErrors)
       {
       Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation 
       errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);

    foreach (var ve in eve.ValidationErrors)
        {                        
        Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                    ve.PropertyName, ve.ErrorMessage);
        }
     }
      throw;
       }            
    }  // end of method
Posted
Updated 8-Sep-22 21:30pm
Comments
Gerry Schmitz 8-Sep-22 12:54pm    
What's "obvious" about "MaxInputProperty"? You didn't even say where you pulled it from; or whether it's Windows Forms, WPF or what.

1 solution

You can find some suggestions on CodeProject here:
How to set max length of datagridview column?[^]
 
Share this answer
 
Comments
jbm417 9-Sep-22 9:05am    
RickZ - Found the answer in the link. Thanks, Joe

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900