0

I am binding a DataGrid to a DataSet on my ViewModel.

I would like to switch to a more light weight control such as ListView.GridView, but it doesn't appear it supports dynamic column generation.

Since this is a MVVM model, i am trying to avoid having to loop through my dataset columns, and add each GridViewColumn from a codebehind.

is it possible to 1. Bind ad a Listview to a DataSet w/out setting columns explicitly, or 2. Bind the Columns collection to a property on the ViewModel.

If not, what are some other free grid controls that would allow this and outperform DataGrid?

2
  • so the DataSet is not strongly-typed? so it is dynamic? Commented Mar 24, 2011 at 23:55
  • that is correct. Not strongly typed Commented Mar 24, 2011 at 23:56

2 Answers 2

0

Dont believe you can do it easily, i would personally use the IValueConverter interface to convert your DataSet to an object that you can manage your columns inside.

public sealed class DataSetConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
           if((DataSet)value != null)
{

// Put logic in here to loop through the columns and create an object to bind to the ListView control.

}
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

XAML Code

 <conv:DataSetConverter x:key="datasetConverter"/>
                <ListBox x:Name="listbox1" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                        ItemsSource="{Binding datasetObject, Converter={StaticResource datasetConverter}}" >
Sign up to request clarification or add additional context in comments.

3 Comments

do you know why DataGrid does it transparently but not Gridview?
a Gridview does do it, just make sure that you switch on the AutoGenerateColumns=true
yes i tried that. Same property is necessary for DataGrid to generate columns . GridView does not do it
-2

Assume DataSet1:

Customer
  Name
  Address
  Phone

Model:

namespace WpfApplication1.Model
{
    class MyDataSetModel
    {
        private DataSet1 _myDataSet;
        private DataSet1TableAdapters.CustomerTableAdapter _myCustomerTableAdapter;
        public DataSet1.CustomerDataTable Customers
        {
            get { return _myDataSet.Customer; }   
        }

        public void FetchCustomers()
        {
            _myDataSet = new DataSet1();
            _myCustomerTableAdapter = new CustomerTableAdapter();
            _myCustomerTableAdapter.Fill(_myDataSet.Customer);
        }
    }
}

ViewModel: (combined with View here for clairity)

public partial class Window1 : Window
{
    private MyDataSetModel _myDataModel;
    public Window1()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        _myDataModel = new MyDataSetModel();
        _myDataModel.FetchCustomers();

        listView1.ItemsSource = _myDataModel.Customers;
    }
}

View:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="400" Width="600">
    <Grid>
        <ListView Name="listView1">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding= "{Binding Path=Name}">
                        <GridViewColumnHeader Width="100">Name</GridViewColumnHeader>
                    </GridViewColumn>

                    <GridViewColumn DisplayMemberBinding= "{Binding Path=Address}">
                        <GridViewColumnHeader Width="340">Address</GridViewColumnHeader>
                    </GridViewColumn>

                    <GridViewColumn DisplayMemberBinding= "{Binding Path=Phone}">
                        <GridViewColumnHeader Width="100">Phone</GridViewColumnHeader>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

1 Comment

He asks about dynamic column generation but not about knowingly column set. You just described a common use case. Next time please read the question carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.