jquery_stack
jquery_stack

Reputation: 261

WPF Datagridview Equivalencies

I'm pretty new to WPF (I've worked with Windows Forms before) and I used DataGridView elements many times, but now I've switched to DataGrid in WPF and boy I'm in trouble...

What I want to do is to "create" the DataGrid with the dimensions entered in a textBox called sizeBox when I click on the button called size. Inside the event in the code I have the following lines:

try
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            int size;
            int i = 0;
            size = Convert.ToInt32(this.sizeBox.Text);
            int sizegridh = Grid.Size.Height;
            int sizegridw = Grid.Size.Width;
            int sizecellh = sizegridh / size;
            int sizecellw = sizegridw / size;
            for (i = 1; i <= size; i++)
            {
                dt.Columns.Add();
                dt.Rows.Add();
                Grid.DataSource = dt;

            }
            for (i = 1; i <= size; i++)
            {
                Grid.Rows[i - 1].Height = sizecellh;
                Grid.Columns[i - 1].Width = sizecellw;

            }
        }

What this code should do is, for example, if we enter the number 5, create a 5x5 Grid on the existing DataView which is empty until we enter the value.

The problems I have are:

Thanks in advance!

Upvotes: 0

Views: 219

Answers (1)

Dennis
Dennis

Reputation: 37770

Grid.Size.Height is not a valid command, and I can't find the equivalency in WPF's DataGrid, same with the Width

There are Width and Height properties.
But, before setting them, note, that WPF is layout-based, while WinForms is coordinate-based. In short, there are rather rare cases, when you want to set size explicitly. Before you will continue, I strongly recommend you to read about layout controls.

Grid.DataSource = dt; does not work

Because there is ItemsSource.

Grid.Rows[i] or Columns is not the right way to select a certain value, which is the equivalent one?

WPF is all about data binding. Do not try to work with WPF controls like in WinForms.

Upvotes: 1

Related Questions