Add StackPanel to WPF DataGrid at runtime












0














I have a Datagrid that is being built dynamically from incoming data using the following:



TableData = JObject.Parse(File.ReadAllText(@"Datainfo.json"));
var listCols = new List<DataColumn>();
var rawData = new DataTable();
foreach (dynamic item in TableData.data)
{
string columnName = item.Column;
var column = new DataColumn(columnName);
string DataType = item.DataType;
if (DataType == "Int" )
{
column.DataType = Type.GetType("System.Int32");
}
else
{
column.DataType = Type.GetType("System.String");
}
column.Unique = false;
column.AllowDBNull = true;
column.AutoIncrement = false;
listCols.Add(column);
rawData.Columns.Add(column);
}


Then I am pushing it to the DataGrid like this:



    DataTable ETL = null;
ETL = rawData;
ETL.DefaultView.AllowEdit = true;
DataGridView.DataContext = ETL;


I would like to add a StackPanel to each column that includes a TextBox for the Column Name, and a dropdown that has various datatypes in it. It would then have a Apply / Cancel option.



I have been trying to follow similar solutions for adding Expanders to Grids, but I can't figure out how to apply it to a DataGrid since it doesn't have a .Children() to .Add() to.
https://www.codeproject.com/Questions/877973/How-Do-I-Add-A-Stackpanel-To-An-Expander-Header-Vi



Is there a way to do what I am asking? I also tried to hide the header row and control the first two rows, but then when I tried loading up the header into a Int column, I get an error, so obviously that isn't a good idea.










share|improve this question






















  • Define a DataGridTemplateColumn with a CellTemplate.
    – mm8
    Nov 13 '18 at 9:13
















0














I have a Datagrid that is being built dynamically from incoming data using the following:



TableData = JObject.Parse(File.ReadAllText(@"Datainfo.json"));
var listCols = new List<DataColumn>();
var rawData = new DataTable();
foreach (dynamic item in TableData.data)
{
string columnName = item.Column;
var column = new DataColumn(columnName);
string DataType = item.DataType;
if (DataType == "Int" )
{
column.DataType = Type.GetType("System.Int32");
}
else
{
column.DataType = Type.GetType("System.String");
}
column.Unique = false;
column.AllowDBNull = true;
column.AutoIncrement = false;
listCols.Add(column);
rawData.Columns.Add(column);
}


Then I am pushing it to the DataGrid like this:



    DataTable ETL = null;
ETL = rawData;
ETL.DefaultView.AllowEdit = true;
DataGridView.DataContext = ETL;


I would like to add a StackPanel to each column that includes a TextBox for the Column Name, and a dropdown that has various datatypes in it. It would then have a Apply / Cancel option.



I have been trying to follow similar solutions for adding Expanders to Grids, but I can't figure out how to apply it to a DataGrid since it doesn't have a .Children() to .Add() to.
https://www.codeproject.com/Questions/877973/How-Do-I-Add-A-Stackpanel-To-An-Expander-Header-Vi



Is there a way to do what I am asking? I also tried to hide the header row and control the first two rows, but then when I tried loading up the header into a Int column, I get an error, so obviously that isn't a good idea.










share|improve this question






















  • Define a DataGridTemplateColumn with a CellTemplate.
    – mm8
    Nov 13 '18 at 9:13














0












0








0







I have a Datagrid that is being built dynamically from incoming data using the following:



TableData = JObject.Parse(File.ReadAllText(@"Datainfo.json"));
var listCols = new List<DataColumn>();
var rawData = new DataTable();
foreach (dynamic item in TableData.data)
{
string columnName = item.Column;
var column = new DataColumn(columnName);
string DataType = item.DataType;
if (DataType == "Int" )
{
column.DataType = Type.GetType("System.Int32");
}
else
{
column.DataType = Type.GetType("System.String");
}
column.Unique = false;
column.AllowDBNull = true;
column.AutoIncrement = false;
listCols.Add(column);
rawData.Columns.Add(column);
}


Then I am pushing it to the DataGrid like this:



    DataTable ETL = null;
ETL = rawData;
ETL.DefaultView.AllowEdit = true;
DataGridView.DataContext = ETL;


I would like to add a StackPanel to each column that includes a TextBox for the Column Name, and a dropdown that has various datatypes in it. It would then have a Apply / Cancel option.



I have been trying to follow similar solutions for adding Expanders to Grids, but I can't figure out how to apply it to a DataGrid since it doesn't have a .Children() to .Add() to.
https://www.codeproject.com/Questions/877973/How-Do-I-Add-A-Stackpanel-To-An-Expander-Header-Vi



Is there a way to do what I am asking? I also tried to hide the header row and control the first two rows, but then when I tried loading up the header into a Int column, I get an error, so obviously that isn't a good idea.










share|improve this question













I have a Datagrid that is being built dynamically from incoming data using the following:



TableData = JObject.Parse(File.ReadAllText(@"Datainfo.json"));
var listCols = new List<DataColumn>();
var rawData = new DataTable();
foreach (dynamic item in TableData.data)
{
string columnName = item.Column;
var column = new DataColumn(columnName);
string DataType = item.DataType;
if (DataType == "Int" )
{
column.DataType = Type.GetType("System.Int32");
}
else
{
column.DataType = Type.GetType("System.String");
}
column.Unique = false;
column.AllowDBNull = true;
column.AutoIncrement = false;
listCols.Add(column);
rawData.Columns.Add(column);
}


Then I am pushing it to the DataGrid like this:



    DataTable ETL = null;
ETL = rawData;
ETL.DefaultView.AllowEdit = true;
DataGridView.DataContext = ETL;


I would like to add a StackPanel to each column that includes a TextBox for the Column Name, and a dropdown that has various datatypes in it. It would then have a Apply / Cancel option.



I have been trying to follow similar solutions for adding Expanders to Grids, but I can't figure out how to apply it to a DataGrid since it doesn't have a .Children() to .Add() to.
https://www.codeproject.com/Questions/877973/How-Do-I-Add-A-Stackpanel-To-An-Expander-Header-Vi



Is there a way to do what I am asking? I also tried to hide the header row and control the first two rows, but then when I tried loading up the header into a Int column, I get an error, so obviously that isn't a good idea.







c# wpf datagridview datagrid datagridviewcolumn






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 1:50









Alan

792718




792718












  • Define a DataGridTemplateColumn with a CellTemplate.
    – mm8
    Nov 13 '18 at 9:13


















  • Define a DataGridTemplateColumn with a CellTemplate.
    – mm8
    Nov 13 '18 at 9:13
















Define a DataGridTemplateColumn with a CellTemplate.
– mm8
Nov 13 '18 at 9:13




Define a DataGridTemplateColumn with a CellTemplate.
– mm8
Nov 13 '18 at 9:13












1 Answer
1






active

oldest

votes


















0














If you are adding this to the column header than you must add it as a HeaderTemplate.
Lets say this is the XAML...



<Window x:Class="testtestz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:local="clr-namespace:testtestz"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Grid>
<ListView>
<ListView.View>
<GridView x:Name="myGrid">
<GridViewColumn Header="Id"/>
<GridViewColumn Header="Name"/>
<GridViewColumn Header="Date"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>


Then you would do this in the code behind...



using System;
using System.Windows;
using System.Windows.Controls;

namespace testtestz
{

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);

var cBox = new FrameworkElementFactory(typeof(ComboBox));

myGrid.Columns[0].HeaderTemplate = new DataTemplate() { VisualTree = cBox };
}
}
}


This is just a showcase of how to do it. Of course you would have to play with some layout, sizes and simillar stuff to get it to a reasonable point but I hope it helps.






share|improve this answer

















  • 1




    Thanks Rob, this got me closer, but then I started taking a different direction: stackoverflow.com/questions/53288632/…
    – Alan
    Nov 13 '18 at 20:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272634%2fadd-stackpanel-to-wpf-datagrid-at-runtime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If you are adding this to the column header than you must add it as a HeaderTemplate.
Lets say this is the XAML...



<Window x:Class="testtestz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:local="clr-namespace:testtestz"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Grid>
<ListView>
<ListView.View>
<GridView x:Name="myGrid">
<GridViewColumn Header="Id"/>
<GridViewColumn Header="Name"/>
<GridViewColumn Header="Date"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>


Then you would do this in the code behind...



using System;
using System.Windows;
using System.Windows.Controls;

namespace testtestz
{

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);

var cBox = new FrameworkElementFactory(typeof(ComboBox));

myGrid.Columns[0].HeaderTemplate = new DataTemplate() { VisualTree = cBox };
}
}
}


This is just a showcase of how to do it. Of course you would have to play with some layout, sizes and simillar stuff to get it to a reasonable point but I hope it helps.






share|improve this answer

















  • 1




    Thanks Rob, this got me closer, but then I started taking a different direction: stackoverflow.com/questions/53288632/…
    – Alan
    Nov 13 '18 at 20:03
















0














If you are adding this to the column header than you must add it as a HeaderTemplate.
Lets say this is the XAML...



<Window x:Class="testtestz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:local="clr-namespace:testtestz"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Grid>
<ListView>
<ListView.View>
<GridView x:Name="myGrid">
<GridViewColumn Header="Id"/>
<GridViewColumn Header="Name"/>
<GridViewColumn Header="Date"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>


Then you would do this in the code behind...



using System;
using System.Windows;
using System.Windows.Controls;

namespace testtestz
{

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);

var cBox = new FrameworkElementFactory(typeof(ComboBox));

myGrid.Columns[0].HeaderTemplate = new DataTemplate() { VisualTree = cBox };
}
}
}


This is just a showcase of how to do it. Of course you would have to play with some layout, sizes and simillar stuff to get it to a reasonable point but I hope it helps.






share|improve this answer

















  • 1




    Thanks Rob, this got me closer, but then I started taking a different direction: stackoverflow.com/questions/53288632/…
    – Alan
    Nov 13 '18 at 20:03














0












0








0






If you are adding this to the column header than you must add it as a HeaderTemplate.
Lets say this is the XAML...



<Window x:Class="testtestz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:local="clr-namespace:testtestz"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Grid>
<ListView>
<ListView.View>
<GridView x:Name="myGrid">
<GridViewColumn Header="Id"/>
<GridViewColumn Header="Name"/>
<GridViewColumn Header="Date"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>


Then you would do this in the code behind...



using System;
using System.Windows;
using System.Windows.Controls;

namespace testtestz
{

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);

var cBox = new FrameworkElementFactory(typeof(ComboBox));

myGrid.Columns[0].HeaderTemplate = new DataTemplate() { VisualTree = cBox };
}
}
}


This is just a showcase of how to do it. Of course you would have to play with some layout, sizes and simillar stuff to get it to a reasonable point but I hope it helps.






share|improve this answer












If you are adding this to the column header than you must add it as a HeaderTemplate.
Lets say this is the XAML...



<Window x:Class="testtestz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:local="clr-namespace:testtestz"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Grid>
<ListView>
<ListView.View>
<GridView x:Name="myGrid">
<GridViewColumn Header="Id"/>
<GridViewColumn Header="Name"/>
<GridViewColumn Header="Date"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>


Then you would do this in the code behind...



using System;
using System.Windows;
using System.Windows.Controls;

namespace testtestz
{

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);

var cBox = new FrameworkElementFactory(typeof(ComboBox));

myGrid.Columns[0].HeaderTemplate = new DataTemplate() { VisualTree = cBox };
}
}
}


This is just a showcase of how to do it. Of course you would have to play with some layout, sizes and simillar stuff to get it to a reasonable point but I hope it helps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 7:27









Rob

1,0091022




1,0091022








  • 1




    Thanks Rob, this got me closer, but then I started taking a different direction: stackoverflow.com/questions/53288632/…
    – Alan
    Nov 13 '18 at 20:03














  • 1




    Thanks Rob, this got me closer, but then I started taking a different direction: stackoverflow.com/questions/53288632/…
    – Alan
    Nov 13 '18 at 20:03








1




1




Thanks Rob, this got me closer, but then I started taking a different direction: stackoverflow.com/questions/53288632/…
– Alan
Nov 13 '18 at 20:03




Thanks Rob, this got me closer, but then I started taking a different direction: stackoverflow.com/questions/53288632/…
– Alan
Nov 13 '18 at 20:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272634%2fadd-stackpanel-to-wpf-datagrid-at-runtime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python