Home Forums WPF controls Xceed DataGrid for WPF Bind DataTypes to a Column

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26713 |

    I have now just got Default Cell Editor to work. My problem is now, that I don’t know how to bind a specific Type to a column. Is there a tag I can set to e.g. make it accept only boolean values?

    Imported from legacy forums. Posted by Johny (had 3073 views)

    Xceed Support
    Member
    Post count: 5658

    I am not sure I understand your question.

    If your grid is bound to a data source that has some boolean fields, then those columns should automatically only accept boolean values. Otherwise, you will need to create the appropriate DataGridItemProperties through the DataGridCollection[Source] to which the grid is bound and set their DataType properties to the desired type.

    Now, if you are using a CellEditor that is not “made” for boolean values (e.g., TextBox), then you can create and use a converter that will check the inputted value and convert it to the approiate value.

    For example:

    Text=”{xcdg:CellEditorBinding Converter={StaticResource textToBooleanConverter}}”/>

    Imported from legacy forums. Posted by Jenny [Xceed] (had 253 views)

    User (Old forums)
    Member
    Post count: 23064

    Well, my problem is, that I haven’t bound it to a Data Source, as I’d like to use it in the same way that the old ListView functioned in Windows Forms. This is because I’m making a Client/Server program, and either I have to make quite a thick Client to just display the data and make them editable, or I’ll have to make too much linkage between the client and the server, which I’d prefere to avoid as well.

    So what I really just want it to do, is being able to define the columns, by defining the header and Type the values in this column has. Then I’d just add the rows trough the “Items.Add(Row r)”.

    This way, I’m not using the list to do anything particular complex, but I’m using it to display text fields, checkboxes, comboboxes etc. and they are editable. That’s really all I need my system to accomplish.

    Ok, that was a long explanation for why I’d like to do so, but if you see anything wrong in my reasoning, please say so, and I’ll consider doing it some other way.

    Thanks for your help.

    Best regards,
    Johny Iversen

    Imported from legacy forums. Posted by Johny (had 542 views)

    Xceed Support
    Member
    Post count: 5658

    Since you need to define DataGridItemProperties to give your columns a data type, you will in turn need to bind the grid to a DataGridCollectionView[Source]. By doing so, you lose the ability to add items directly to the ItemsCollection. That said, this is what I suggest:

    1- Create a property of type ObservableCollection<object[]> (if you know the data type of your items, then use that rather than object[]).

    2- Create a DataGridCollectionViewSource that is bound to that property and whose ItemProperties are defined to have the correct DataType and ValuePath.

    3- Bind the DataGridControl to the DataGridCollectionViewSource.

    At this point, any items that are added to the ObservableCollection will be reflected in the grid.

    <code>

    private static ObservableCollection<object[]> m_dataItems = new ObservableCollection<object[]>();
    public static ObservableCollection<object[]> DataItems
    {
    get
    {
    return m_dataItems;
    }
    }

    void Window1_Loaded( object sender, RoutedEventArgs e )
    {
    App.DataItems.Add( new object[ 3 ] { “hello”, 4, true } );
    App.DataItems.Add( new object[ 3 ] { “world”, 2, false} );
    App.DataItems.Add( new object[ 3 ] { “another”, 4, true } );
    }

    <Grid xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”&gt;
    <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key=”cvs_orders”
    Source=”{Binding Source={x:Static Application.Current},
    Path=DataItems}”>
    <xcdg:DataGridCollectionViewSource.ItemProperties>
    <xcdg:DataGridItemProperty Name=”StringColumn”
    DataType=”{x:Type s:String}”
    Title=”First”
    ValuePath=”[0]”/>
    <xcdg:DataGridItemProperty Name=”IntColumn”
    DataType=”{x:Type s:Int32}”
    Title=”Second”
    ValuePath=”[1]” />
    <xcdg:DataGridItemProperty Name=”BooleanColumn”
    DataType=”{x:Type s:Boolean}”
    Title=”Third”
    ValuePath=”[2]” />
    </xcdg:DataGridCollectionViewSource.ItemProperties>
    </xcdg:DataGridCollectionViewSource>

    </Grid.Resources>
    <xcdg:DataGridControl x:Name=”OrdersGrid”
    ItemsSource=”{Binding Source={StaticResource cvs_orders}}” />
    </Grid>

    </code>

    Hope this helps!

    Imported from legacy forums. Posted by Jenny [Xceed] (had 4439 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.