The easiest way to insert DataGrid control is by dragging it directly from the Toolbox window in Visual Studio and then dropping it in our layout (it adds all the necessary references for us). After that, we only have to jump to the XAML of the application and define the row and/or column where we want it to appear.
If we execute the application, the result is not very appealing. We will only see a white rectangle that represents the DataGrid control. We will learn how to manage this control in subsequent chapters. For now, we will add some column definitions to make it look more similar to the following example screen.
To make it easier, we have to select the DataGrid object and use the Properties tab.
On the one hand, we disable auto-generation of columns.
On the other hand, we will define the columns manually, clicking on the property Columns.
We now move to the detailed area. The easiest thing to do here will be to design the page using Expression Blend or Visual Studio. We will use the knowledge acquired in this chapter to design the window manually using grid control. To do this, we must define the following area of rows and columns:
The generated XAML can be seen in the code that follows. A few things should be taken care of. They are:
The grid container which we insert is inside the parent layout grid
We use Margin properties to assign space between grid rows, as well as between labels and controls
To lay out labels and controls, we will use a StackPanel control and adjust the orientation property, setting it to vertical or horizontal, as applicable (another valid approach to position them is to play with each Margin property of each control, this margin being related to its container cell)
For the Comments textbox to occupy all of the grid width, we add a property called ColSpan
We will add the control panel in the lower part of the window to complete the layout. We will use a StackPanel container with horizontal orientation to do so.