Add StackPanel to WPF DataGrid at runtime
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
add a comment |
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
Define aDataGridTemplateColumn
with aCellTemplate
.
– mm8
Nov 13 '18 at 9:13
add a comment |
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
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
c# wpf datagridview datagrid datagridviewcolumn
asked Nov 13 '18 at 1:50
Alan
792718
792718
Define aDataGridTemplateColumn
with aCellTemplate
.
– mm8
Nov 13 '18 at 9:13
add a comment |
Define aDataGridTemplateColumn
with aCellTemplate
.
– 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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Define a
DataGridTemplateColumn
with aCellTemplate
.– mm8
Nov 13 '18 at 9:13