Radu Szasz
Radu Szasz

Reputation: 1021

Changing a DataGrid's column width

I'd like to change a DataGrid's column width, but I keep receiving an error.

This is how I fill in the DataGrid:

tabel.ItemsSource = Item.getItemByCategory(category).DefaultView;

getItemByCategory is a static method of the Item class which return a DataTable.

This is how I try to resize the column:

tabel.Columns[1].Width = 100;

And this is the error I receive:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

For some reason, tabel.Columns.Count == 0. Although immediately after it is being shown there are 4 column displayed.

Could I get some help in resizing the columns?

Upvotes: 2

Views: 8065

Answers (2)

kmatyaszek
kmatyaszek

Reputation: 19296

Try something like that:

XAML file:

<Window x:Class="DataGridView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="table" Loaded="table_Loaded" />
    </Grid>
</Window>

Code-behind file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

namespace DataGridView
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


            table.ItemsSource = getItemByCategory().DefaultView;
        }

        public static DataTable getItemByCategory(int category = 0)
        {
            //ServiceReference.ServiceSoapClient instance = Database.getClient();
            DataTable tabel = new DataTable();
            DataRow linie;
            tabel.Columns.Add(new DataColumn("id", typeof(int)));
            tabel.Columns.Add(new DataColumn("name", typeof(string)));

            //tabel.Columns.Add(new DataColumn("price", typeof(int)));
            //tabel.Columns.Add(new DataColumn("inCart", typeof(bool)));
            //ServiceReference.ArrayOfString[] values = instance.getItemsByCategory(category);

            List<Foo> values = new List<Foo>();
            for (int i = 0; i < 10; i++)
            {
                values.Add(new Foo { ID = i, Name = "name " + i });
            }

            foreach (var v in values)
            {
                linie = tabel.NewRow();
                linie["id"] = v.ID;
                linie["name"] = v.Name;
                //linie["price"] = Convert.ToInt32(v[3]);
                //linie["inCart"] = false;
                tabel.Rows.Add(linie);
            }
            return tabel;
        }

        private void table_Loaded(object sender, RoutedEventArgs e)
        {
            table.Columns[0].Width = 100;
            table.Columns[1].Width = 200;
        }
    }

    public class Foo
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }


}

Upvotes: 3

brunnerh
brunnerh

Reputation: 184506

If the columns are autogenerated you should defer acces until AutoGeneratedColumns fires.

Upvotes: 0

Related Questions