none
Linq, sql, object mapping, DataGridView and Comboboxes :D RRS feed

  • Question



  • Ok, I'll try to describe my issue in the shortest way possible.

    I use linq to do sql to object mapping and feed a BindingSource
    which feeds in turn a DataGridView, all this considering the
    DataBinding as follows: (db is a DataContext instance.)


    Table<Jewel> jewels = db.GetTable<Jewel>();
    IQueryable<Jewel> q = from j in jewels
                          select j;
    this.mainBindingSource.DataSource = q;


    Right, now this table, called Jewels has a field called "MaterialType"
    of type nvarchar 100. (SQL CE)

    I want to show only fields having the attribute self created
    attribute DataGridViewColumnAttribute and this field to be show
    in my DataGridView as a ComboBox, so as part of my code I
    have something like this:

    // In my object
    [DataGridViewColumn(4, DataGridViewColumnType.ComboBox, new string[] {"Gold", "Silver"} )]

    // In the method who generates the columns for the DataGridView

    foreach (PropertyDescriptor propertyDescriptor in propertiesToShow)
    {
        DataGridViewColumn dataGridViewColumn;
        DataGridViewColumnAttribute DataGridViewColumnAttribute =
            (propertyDescriptor.Attributes[typeof(DataGridViewColumnAttribute)]
            as DataGridViewColumnAttribute);

        switch (DataGridViewColumnAttribute.ColumnType)
        {
            case DataGridViewColumnType.TextBox:
                dataGridViewColumn = new DataGridViewTextBoxColumn();
                break;
            case DataGridViewColumnType.ComboBox:
                dataGridViewColumn = new DataGridViewComboBoxColumn();
                (dataGridViewColumn as DataGridViewComboBoxColumn)
                    .Items.AddRange(
                                     (propertyDescriptor.Attributes[typeof(DataGridViewColumnAttribute)]
                                     as DataGridViewColumnAttribute)
                                     .CBoxItems);
                break;

    Right, everything is fine, and it works great, I have my ComboBox shown with
    Gold and silver... Ok... But I have three issues:

    1) I have to click 2 or three times on the combobox for it to drop down
    2) The selected data doesn't get updated in the database when calling SubmitChanges()
    3) There is a blank choice where I only want the two values given
    To this, I'll add a question:
    Isn't it possible to have a match between what is shown and a predefined value ?
    For example, if I would want my database field (and so my object property) to be
    a boolean called IsGold, how would I have "Gold" to return true and silver to return false.
    ....

    There I am, many questions, If you need more informations / code, Please tell me

    Thank you.

    Saturday, June 7, 2008 2:50 AM

Answers



  • UpdateCheck = UpdateCheck.Never
    In the Column attribure of the comboboxed property fixed it...
    was an SQL issue.
    (Hey microsoft, what about CLEAR error/exception messages ?)
    Saturday, June 7, 2008 9:15 PM