Reputation: 1158
I'm trying to find out how can I set up row
and columnspan
of programmatically created DataGrid
. My code adds it in the first row:
private DataGrid CreateTextBoxesDataGrid(string name)
{
DataGrid dataGrid = new DataGrid();
dataGrid.Name = name;
//row ???
dataGrid.Style = Resources["AppSettingsDataGridStyle"] as Style;
dataGrid.Columns.Add(CreateNameColumn());
dataGrid.Columns.Add(CreateTextValueColumn());
dataGrid.Columns.Add(CreateComentColumn());
return dataGrid;
}
...
mainGrid.Children.Add(CreateTextBoxesDataGrid("eeej"));
Upvotes: 0
Views: 166
Reputation: 61
To set a ColumnSpan on the DataGrid itself does not really make sense. The ColumnSpan defines how many columns a control on the grid will span across. So as you see, this is something you want to set per control that is INSIDE the grid, not on the grid itself.
Are you intending to set the width of the column of the DataGrid? If that is the case, you can do something like this:
column.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
Or like this if you want it based on the entire width of the DataGrid:
column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
If you want to add a new row programatically to the DataGrid, you can do this:
public class DataGridRow
{
public string Col1 { get; set; }
public string Col2 { get; set; }
}
...
var row = new DataGridRow{ Col1 = "Column1", Col2 = "Column2" };
dataGrid.Items.Add(row);
If you want to set how many columns the DataGrid will span inside it's parent grid, you can use the following:
Grid.SetColumnSpan(dataGrid, columnsToSpan);
I'm just guessing what you want here, if I did not understand you correctly, feel free to reply to my answer with more details.
Upvotes: 2
Reputation: 480
That's the code:
Grid.SetColumnSpan(dataGrid, colSpan);
Grid.SetRow(dataGrid, row);
Upvotes: 1