none
How to display 'Lookup value' in DataGridView that uses databinding with EF 6 RRS feed

  • Question

  • Background: I'm familiar with ASP.NET & EF, but new to WinForms. 
    I'm building a Windows Forms app using Entity-Framework.  To do this, I followed the instructions at https://msdn.microsoft.com/en-us/data/jj682076.aspx 
    I've got the basic functionality working (used databinding with EF to display data in a DataGridView)
    I have the following EF classes for Address and State tables:

    public partial class Address { ... public int Id { get; set; } public string AddressMain { get; set; } public string City { get; set; } public int StateId { get; set; } ... public virtual State State { get; set; } ... }

    public partial class State
    {
          public State()
          {
          Addresses = new ObservableListSource<Address>();
          }

          public int Id { get; set; }
          public string Name { get; set; }

          public virtual ObservableListSource<Address> Addresses { get; set; }
    }

    Right now, the State column in the DataGridView displays values of "system.data.entity.dynamicproxies.State_<GUID-Like-Value>"
    How do I display the child's value of State.Name in that column?



    • Edited by WHOIS JG Wednesday, December 9, 2015 7:38 PM
    Wednesday, December 9, 2015 7:26 PM

Answers

  • Hi WHOIS JG,

    According to your description, you want to set the column to displays a custom data.

    I suggest you use custom DataGridView columns and reflection to achieve.

    Here is the code:

         //set the costum columns in DataGrid
    
                dataGridView1.Columns.Add("Id", "Id");
    
                dataGridView1.Columns.Add("AddressMain", "AddressMain");
    
                dataGridView1.Columns.Add("City", "City");
    
                dataGridView1.Columns.Add("StateId", "StateId");
    
                dataGridView1.Columns.Add("State", "State");
    
    
                //set the value Correspond to the primary DataSource which DataGridView Control bind
    
                dataGridView1.Columns["Id"].DataPropertyName = "Id";
    
                dataGridView1.Columns["AddressMain"].DataPropertyName = "AddressMain";
    
                dataGridView1.Columns["City"].DataPropertyName = "City";
    
                dataGridView1.Columns["StateId"].DataPropertyName = "StateId";
    
                dataGridView1.Columns["State"].DataPropertyName = "State.Name";
    
    
                
    
                List<Address> lstAddress = new List<Address>();
    
                Address addr = new Address();
    
                State stat = new State();
    
                addr.Id = 1;
    
                addr.AddressMain = "abcd";
    
                addr.City = "ac";
    
                stat.Id = 0;
    
                stat.Name = "checked";
    
                addr.State = stat;
    
                addr.StateId = 0;
    
    
                lstAddress.Add(addr);
    
    
                addr = new Address();
    
                addr.Id = 2;
    
                addr.AddressMain = "DC";
    
                addr.City = "abc";
    
                stat = new State();
    
                stat.Id = 1;
    
                stat.Name = "uncheck";
    
                addr.State = stat;
    
                addr.StateId = 1;
    
                lstAddress.Add(addr);
    
                //disable AutoGenerateColumns
    
                dataGridView1.AutoGenerateColumns = false;
    
                dataGridView1.DataSource = lstAddress;
    
    
            /// <summary>
    
            /// use reflection get state.Name
    
            /// </summary>
    
            /// <param name="sender"></param>
    
            /// <param name="e"></param>
    
            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    
            {
    
                if ((dataGridView1.Rows[e.RowIndex].DataBoundItem != null) &&
    
                    (dataGridView1.Columns[e.ColumnIndex].DataPropertyName.Contains(".")))
    
                {
    
                    //spilt the State.Name to string array
    
                    string[] names = dataGridView1.Columns[e.ColumnIndex].DataPropertyName.Split('.');
    
                    //save the class State
    
                    object obj = dataGridView1.Rows[e.RowIndex].DataBoundItem;
    
                    for (int i = 0; i < names.Count(); ++i)
    
                    {
    
                        try
    
                        {
    
                            //Use reflection Get State.Name
    
                            var result = obj.GetType().GetProperty(names[i]).GetValue(obj, null);
    
                            obj = result;
    
                            e.Value = result.ToString();
    
                        }
    
                        catch (Exception)
    
                        {
    
                            return;
    
                            throw;
    
                        }
    
                    }
    
                }
    
            }
    

    Hope above is helpful to you.

    Best regards

    Xiaoxin Sheng

    • Marked as answer by WHOIS JG Friday, December 11, 2015 4:28 PM
    Thursday, December 10, 2015 8:58 AM