Calculating one column from 2 in DataGrid through Converter
I'm writing app (my first using WPF) that shows data for products in datagrid. I succeded with connecting datagrid with MS SQL database. I stuck on creating calculated column.
I'm interested in multiplying values from two columns. I want to use Pcs_DC_col and Qty_multi_col to calcule values in Qty_DC_col.
From what I understand I have to use converter and because i use more than 1 columns I shoud use IMultiValueConverter. But don't know how to write code that would show me something diffient than blank cells or dependencyproperty.unsetvalue. What should I add or change to make it work?
XAML code:
<Window.Resources>
<local:ColumnConverter x:Key="ColumnConversion"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="matrix" SelectionUnit="FullRow" SelectionChanged="PLU_row_selected"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="PLU" Binding="{Binding PLU}"/>
<DataGridTextColumn Header="config" Binding="{Binding config}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Colour" Binding="{Binding Colour}"/>
<DataGridTextColumn Header="Promo" Binding="{Binding Promo}"/>
<DataGridTextColumn Header="Department" Binding="{Binding Department}"/>
<DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
<DataGridTextColumn x:Name="Pcs_DC_col" Header="Pc.s DC" Binding="{Binding Pcs Dc}"/>
<DataGridTextColumn Header="Allocated Pcs" Binding="{Binding Allocated Pcs}"/>
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding ElementName="Pcs_DC_col" Path="Text"/>
<Binding ElementName="Qty_multi_col" Path="Text"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Qty All" Binding="{Binding obliczenia}"/>
<DataGridTextColumn Header="Qty Tra" Binding="{Binding Qty Tra}"/>
<DataGridTextColumn Header="Qty Str" Binding="{Binding Qty Str}"/>
<DataGridTextColumn x:Name="Qty_multi_col" Header="Qty multiple" Binding="{Binding Qty Multiple}"/>
<DataGridTextColumn Header="% all" Binding="{Binding Percent_Allo}"/>
<DataGridTextColumn Header="Value all" Binding="{Binding Value_allocated}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
Converter code:
public class ColumnConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
try
{
return String.Format("{0}", values[0]);
}
catch
{
return 0;
}
} // Convert()
This version shows me blank column, and if I change code for:
return String.Format("{0}", values[0]);
That fill my column with dependencyproperty.unsetvalue.
Code how I load data from database to Grid:
private void load_products_to_grid()
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server = batorego426coreSQL; Database = CorpCore; Integrated Security=true;";
myConnection.Open();
SqlCommand query = new SqlCommand();
query.CommandText = "select * from [products]";
query.Connection = myConnection;
SqlDataAdapter adapter = new SqlDataAdapter(query);
DataTable product_table = new DataTable("products");
adapter.Fill(product_table);
matrix.ItemsSource = product_table.DefaultView;
}
EDIT:
I changed biding as Andy suggested to
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
And converter code:
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
return (int)values[0]*(int)values[1];
}
But still I get empty column, and I tried changing code in converter to check if can see any value to:
String.Format("{0}", values[0]);
But then get "dependencyproperty.unsetvalue" in that column. So I guess data is still not connected and is not even null but no tset. What else I can do?
wpf data-binding datagrid calculated-columns
add a comment |
I'm writing app (my first using WPF) that shows data for products in datagrid. I succeded with connecting datagrid with MS SQL database. I stuck on creating calculated column.
I'm interested in multiplying values from two columns. I want to use Pcs_DC_col and Qty_multi_col to calcule values in Qty_DC_col.
From what I understand I have to use converter and because i use more than 1 columns I shoud use IMultiValueConverter. But don't know how to write code that would show me something diffient than blank cells or dependencyproperty.unsetvalue. What should I add or change to make it work?
XAML code:
<Window.Resources>
<local:ColumnConverter x:Key="ColumnConversion"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="matrix" SelectionUnit="FullRow" SelectionChanged="PLU_row_selected"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="PLU" Binding="{Binding PLU}"/>
<DataGridTextColumn Header="config" Binding="{Binding config}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Colour" Binding="{Binding Colour}"/>
<DataGridTextColumn Header="Promo" Binding="{Binding Promo}"/>
<DataGridTextColumn Header="Department" Binding="{Binding Department}"/>
<DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
<DataGridTextColumn x:Name="Pcs_DC_col" Header="Pc.s DC" Binding="{Binding Pcs Dc}"/>
<DataGridTextColumn Header="Allocated Pcs" Binding="{Binding Allocated Pcs}"/>
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding ElementName="Pcs_DC_col" Path="Text"/>
<Binding ElementName="Qty_multi_col" Path="Text"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Qty All" Binding="{Binding obliczenia}"/>
<DataGridTextColumn Header="Qty Tra" Binding="{Binding Qty Tra}"/>
<DataGridTextColumn Header="Qty Str" Binding="{Binding Qty Str}"/>
<DataGridTextColumn x:Name="Qty_multi_col" Header="Qty multiple" Binding="{Binding Qty Multiple}"/>
<DataGridTextColumn Header="% all" Binding="{Binding Percent_Allo}"/>
<DataGridTextColumn Header="Value all" Binding="{Binding Value_allocated}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
Converter code:
public class ColumnConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
try
{
return String.Format("{0}", values[0]);
}
catch
{
return 0;
}
} // Convert()
This version shows me blank column, and if I change code for:
return String.Format("{0}", values[0]);
That fill my column with dependencyproperty.unsetvalue.
Code how I load data from database to Grid:
private void load_products_to_grid()
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server = batorego426coreSQL; Database = CorpCore; Integrated Security=true;";
myConnection.Open();
SqlCommand query = new SqlCommand();
query.CommandText = "select * from [products]";
query.Connection = myConnection;
SqlDataAdapter adapter = new SqlDataAdapter(query);
DataTable product_table = new DataTable("products");
adapter.Fill(product_table);
matrix.ItemsSource = product_table.DefaultView;
}
EDIT:
I changed biding as Andy suggested to
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
And converter code:
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
return (int)values[0]*(int)values[1];
}
But still I get empty column, and I tried changing code in converter to check if can see any value to:
String.Format("{0}", values[0]);
But then get "dependencyproperty.unsetvalue" in that column. So I guess data is still not connected and is not even null but no tset. What else I can do?
wpf data-binding datagrid calculated-columns
add a comment |
I'm writing app (my first using WPF) that shows data for products in datagrid. I succeded with connecting datagrid with MS SQL database. I stuck on creating calculated column.
I'm interested in multiplying values from two columns. I want to use Pcs_DC_col and Qty_multi_col to calcule values in Qty_DC_col.
From what I understand I have to use converter and because i use more than 1 columns I shoud use IMultiValueConverter. But don't know how to write code that would show me something diffient than blank cells or dependencyproperty.unsetvalue. What should I add or change to make it work?
XAML code:
<Window.Resources>
<local:ColumnConverter x:Key="ColumnConversion"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="matrix" SelectionUnit="FullRow" SelectionChanged="PLU_row_selected"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="PLU" Binding="{Binding PLU}"/>
<DataGridTextColumn Header="config" Binding="{Binding config}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Colour" Binding="{Binding Colour}"/>
<DataGridTextColumn Header="Promo" Binding="{Binding Promo}"/>
<DataGridTextColumn Header="Department" Binding="{Binding Department}"/>
<DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
<DataGridTextColumn x:Name="Pcs_DC_col" Header="Pc.s DC" Binding="{Binding Pcs Dc}"/>
<DataGridTextColumn Header="Allocated Pcs" Binding="{Binding Allocated Pcs}"/>
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding ElementName="Pcs_DC_col" Path="Text"/>
<Binding ElementName="Qty_multi_col" Path="Text"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Qty All" Binding="{Binding obliczenia}"/>
<DataGridTextColumn Header="Qty Tra" Binding="{Binding Qty Tra}"/>
<DataGridTextColumn Header="Qty Str" Binding="{Binding Qty Str}"/>
<DataGridTextColumn x:Name="Qty_multi_col" Header="Qty multiple" Binding="{Binding Qty Multiple}"/>
<DataGridTextColumn Header="% all" Binding="{Binding Percent_Allo}"/>
<DataGridTextColumn Header="Value all" Binding="{Binding Value_allocated}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
Converter code:
public class ColumnConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
try
{
return String.Format("{0}", values[0]);
}
catch
{
return 0;
}
} // Convert()
This version shows me blank column, and if I change code for:
return String.Format("{0}", values[0]);
That fill my column with dependencyproperty.unsetvalue.
Code how I load data from database to Grid:
private void load_products_to_grid()
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server = batorego426coreSQL; Database = CorpCore; Integrated Security=true;";
myConnection.Open();
SqlCommand query = new SqlCommand();
query.CommandText = "select * from [products]";
query.Connection = myConnection;
SqlDataAdapter adapter = new SqlDataAdapter(query);
DataTable product_table = new DataTable("products");
adapter.Fill(product_table);
matrix.ItemsSource = product_table.DefaultView;
}
EDIT:
I changed biding as Andy suggested to
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
And converter code:
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
return (int)values[0]*(int)values[1];
}
But still I get empty column, and I tried changing code in converter to check if can see any value to:
String.Format("{0}", values[0]);
But then get "dependencyproperty.unsetvalue" in that column. So I guess data is still not connected and is not even null but no tset. What else I can do?
wpf data-binding datagrid calculated-columns
I'm writing app (my first using WPF) that shows data for products in datagrid. I succeded with connecting datagrid with MS SQL database. I stuck on creating calculated column.
I'm interested in multiplying values from two columns. I want to use Pcs_DC_col and Qty_multi_col to calcule values in Qty_DC_col.
From what I understand I have to use converter and because i use more than 1 columns I shoud use IMultiValueConverter. But don't know how to write code that would show me something diffient than blank cells or dependencyproperty.unsetvalue. What should I add or change to make it work?
XAML code:
<Window.Resources>
<local:ColumnConverter x:Key="ColumnConversion"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="matrix" SelectionUnit="FullRow" SelectionChanged="PLU_row_selected"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="PLU" Binding="{Binding PLU}"/>
<DataGridTextColumn Header="config" Binding="{Binding config}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Colour" Binding="{Binding Colour}"/>
<DataGridTextColumn Header="Promo" Binding="{Binding Promo}"/>
<DataGridTextColumn Header="Department" Binding="{Binding Department}"/>
<DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
<DataGridTextColumn x:Name="Pcs_DC_col" Header="Pc.s DC" Binding="{Binding Pcs Dc}"/>
<DataGridTextColumn Header="Allocated Pcs" Binding="{Binding Allocated Pcs}"/>
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding ElementName="Pcs_DC_col" Path="Text"/>
<Binding ElementName="Qty_multi_col" Path="Text"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Qty All" Binding="{Binding obliczenia}"/>
<DataGridTextColumn Header="Qty Tra" Binding="{Binding Qty Tra}"/>
<DataGridTextColumn Header="Qty Str" Binding="{Binding Qty Str}"/>
<DataGridTextColumn x:Name="Qty_multi_col" Header="Qty multiple" Binding="{Binding Qty Multiple}"/>
<DataGridTextColumn Header="% all" Binding="{Binding Percent_Allo}"/>
<DataGridTextColumn Header="Value all" Binding="{Binding Value_allocated}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
Converter code:
public class ColumnConverter : IMultiValueConverter
{
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
try
{
return String.Format("{0}", values[0]);
}
catch
{
return 0;
}
} // Convert()
This version shows me blank column, and if I change code for:
return String.Format("{0}", values[0]);
That fill my column with dependencyproperty.unsetvalue.
Code how I load data from database to Grid:
private void load_products_to_grid()
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Server = batorego426coreSQL; Database = CorpCore; Integrated Security=true;";
myConnection.Open();
SqlCommand query = new SqlCommand();
query.CommandText = "select * from [products]";
query.Connection = myConnection;
SqlDataAdapter adapter = new SqlDataAdapter(query);
DataTable product_table = new DataTable("products");
adapter.Fill(product_table);
matrix.ItemsSource = product_table.DefaultView;
}
EDIT:
I changed biding as Andy suggested to
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
And converter code:
public object Convert(object values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null) return 0;
return (int)values[0]*(int)values[1];
}
But still I get empty column, and I tried changing code in converter to check if can see any value to:
String.Format("{0}", values[0]);
But then get "dependencyproperty.unsetvalue" in that column. So I guess data is still not connected and is not even null but no tset. What else I can do?
<Window.Resources>
<local:ColumnConverter x:Key="ColumnConversion"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="matrix" SelectionUnit="FullRow" SelectionChanged="PLU_row_selected"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="PLU" Binding="{Binding PLU}"/>
<DataGridTextColumn Header="config" Binding="{Binding config}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Colour" Binding="{Binding Colour}"/>
<DataGridTextColumn Header="Promo" Binding="{Binding Promo}"/>
<DataGridTextColumn Header="Department" Binding="{Binding Department}"/>
<DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
<DataGridTextColumn x:Name="Pcs_DC_col" Header="Pc.s DC" Binding="{Binding Pcs Dc}"/>
<DataGridTextColumn Header="Allocated Pcs" Binding="{Binding Allocated Pcs}"/>
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding ElementName="Pcs_DC_col" Path="Text"/>
<Binding ElementName="Qty_multi_col" Path="Text"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Qty All" Binding="{Binding obliczenia}"/>
<DataGridTextColumn Header="Qty Tra" Binding="{Binding Qty Tra}"/>
<DataGridTextColumn Header="Qty Str" Binding="{Binding Qty Str}"/>
<DataGridTextColumn x:Name="Qty_multi_col" Header="Qty multiple" Binding="{Binding Qty Multiple}"/>
<DataGridTextColumn Header="% all" Binding="{Binding Percent_Allo}"/>
<DataGridTextColumn Header="Value all" Binding="{Binding Value_allocated}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
<Window.Resources>
<local:ColumnConverter x:Key="ColumnConversion"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="matrix" SelectionUnit="FullRow" SelectionChanged="PLU_row_selected"
AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="PLU" Binding="{Binding PLU}"/>
<DataGridTextColumn Header="config" Binding="{Binding config}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Colour" Binding="{Binding Colour}"/>
<DataGridTextColumn Header="Promo" Binding="{Binding Promo}"/>
<DataGridTextColumn Header="Department" Binding="{Binding Department}"/>
<DataGridTextColumn Header="Category" Binding="{Binding Category}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}"/>
<DataGridTextColumn x:Name="Pcs_DC_col" Header="Pc.s DC" Binding="{Binding Pcs Dc}"/>
<DataGridTextColumn Header="Allocated Pcs" Binding="{Binding Allocated Pcs}"/>
<DataGridTextColumn x:Name="Qty_DC_col" Header="Qty DC">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding ElementName="Pcs_DC_col" Path="Text"/>
<Binding ElementName="Qty_multi_col" Path="Text"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Qty All" Binding="{Binding obliczenia}"/>
<DataGridTextColumn Header="Qty Tra" Binding="{Binding Qty Tra}"/>
<DataGridTextColumn Header="Qty Str" Binding="{Binding Qty Str}"/>
<DataGridTextColumn x:Name="Qty_multi_col" Header="Qty multiple" Binding="{Binding Qty Multiple}"/>
<DataGridTextColumn Header="% all" Binding="{Binding Percent_Allo}"/>
<DataGridTextColumn Header="Value all" Binding="{Binding Value_allocated}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
wpf data-binding datagrid calculated-columns
wpf data-binding datagrid calculated-columns
edited Nov 20 '18 at 21:11
Mlorism
asked Nov 19 '18 at 19:42
MlorismMlorism
104
104
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your bindings in the multibinding should be to the data and just the column names. If they are Pcs_DC_col and Qty_multi_col then
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
The object values array will have two values in it. These are presumably double but as far as the code knows these are just objects so cast and add them together. Return that as a number. Something like:
return (double)values[0] * (double)values[1];
You may have to null trap etc.
And that column better not be editable.
For most datagrids I would have a viewmodel to represent each row out the database and I could put calculations etc in there. So there'd be a property which summed the two columns.
And of course I'd have somewhere to put other logic such as validation.
Binding directly to a datatable is quite limiting.
I changed Binding Path as you suggested and changed converter toreturn (int)values[0]*(int)values[1];
but still I can see blank colum. I changed it for a moment toreturn String.Format("{0}", values[0]);
to check but column shows "dependencyproperty.unsetvalue". I understand that I corrected code with casting to int, but values are not send to the column. Data in column are not even null, but unset. Could I do something else with binding?
– Mlorism
Nov 20 '18 at 20:54
It expects an object from your binding which it'll convert to string itself. Do you have any binding errors in your output? Check you're binding to the correct column names. Then put a break point in your converter and see what you actually get in the array.
– Andy
Nov 21 '18 at 9:27
add a comment |
As an alternative you could modify your sql-query to:
"select *, (Pcs_DC_col * Qty_multi_col) as Qty_DC_col from [products]"
and then bind normally to that column. (or whatever the column is named)
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%2f53381567%2fcalculating-one-column-from-2-in-datagrid-through-converter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your bindings in the multibinding should be to the data and just the column names. If they are Pcs_DC_col and Qty_multi_col then
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
The object values array will have two values in it. These are presumably double but as far as the code knows these are just objects so cast and add them together. Return that as a number. Something like:
return (double)values[0] * (double)values[1];
You may have to null trap etc.
And that column better not be editable.
For most datagrids I would have a viewmodel to represent each row out the database and I could put calculations etc in there. So there'd be a property which summed the two columns.
And of course I'd have somewhere to put other logic such as validation.
Binding directly to a datatable is quite limiting.
I changed Binding Path as you suggested and changed converter toreturn (int)values[0]*(int)values[1];
but still I can see blank colum. I changed it for a moment toreturn String.Format("{0}", values[0]);
to check but column shows "dependencyproperty.unsetvalue". I understand that I corrected code with casting to int, but values are not send to the column. Data in column are not even null, but unset. Could I do something else with binding?
– Mlorism
Nov 20 '18 at 20:54
It expects an object from your binding which it'll convert to string itself. Do you have any binding errors in your output? Check you're binding to the correct column names. Then put a break point in your converter and see what you actually get in the array.
– Andy
Nov 21 '18 at 9:27
add a comment |
Your bindings in the multibinding should be to the data and just the column names. If they are Pcs_DC_col and Qty_multi_col then
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
The object values array will have two values in it. These are presumably double but as far as the code knows these are just objects so cast and add them together. Return that as a number. Something like:
return (double)values[0] * (double)values[1];
You may have to null trap etc.
And that column better not be editable.
For most datagrids I would have a viewmodel to represent each row out the database and I could put calculations etc in there. So there'd be a property which summed the two columns.
And of course I'd have somewhere to put other logic such as validation.
Binding directly to a datatable is quite limiting.
I changed Binding Path as you suggested and changed converter toreturn (int)values[0]*(int)values[1];
but still I can see blank colum. I changed it for a moment toreturn String.Format("{0}", values[0]);
to check but column shows "dependencyproperty.unsetvalue". I understand that I corrected code with casting to int, but values are not send to the column. Data in column are not even null, but unset. Could I do something else with binding?
– Mlorism
Nov 20 '18 at 20:54
It expects an object from your binding which it'll convert to string itself. Do you have any binding errors in your output? Check you're binding to the correct column names. Then put a break point in your converter and see what you actually get in the array.
– Andy
Nov 21 '18 at 9:27
add a comment |
Your bindings in the multibinding should be to the data and just the column names. If they are Pcs_DC_col and Qty_multi_col then
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
The object values array will have two values in it. These are presumably double but as far as the code knows these are just objects so cast and add them together. Return that as a number. Something like:
return (double)values[0] * (double)values[1];
You may have to null trap etc.
And that column better not be editable.
For most datagrids I would have a viewmodel to represent each row out the database and I could put calculations etc in there. So there'd be a property which summed the two columns.
And of course I'd have somewhere to put other logic such as validation.
Binding directly to a datatable is quite limiting.
Your bindings in the multibinding should be to the data and just the column names. If they are Pcs_DC_col and Qty_multi_col then
<MultiBinding Converter="{StaticResource ColumnConversion}">
<Binding Path="Pcs_DC_col"/>
<Binding Path="Qty_multi_col"/>
</MultiBinding>
The object values array will have two values in it. These are presumably double but as far as the code knows these are just objects so cast and add them together. Return that as a number. Something like:
return (double)values[0] * (double)values[1];
You may have to null trap etc.
And that column better not be editable.
For most datagrids I would have a viewmodel to represent each row out the database and I could put calculations etc in there. So there'd be a property which summed the two columns.
And of course I'd have somewhere to put other logic such as validation.
Binding directly to a datatable is quite limiting.
edited Nov 20 '18 at 9:01
answered Nov 19 '18 at 20:09
AndyAndy
3,0681106
3,0681106
I changed Binding Path as you suggested and changed converter toreturn (int)values[0]*(int)values[1];
but still I can see blank colum. I changed it for a moment toreturn String.Format("{0}", values[0]);
to check but column shows "dependencyproperty.unsetvalue". I understand that I corrected code with casting to int, but values are not send to the column. Data in column are not even null, but unset. Could I do something else with binding?
– Mlorism
Nov 20 '18 at 20:54
It expects an object from your binding which it'll convert to string itself. Do you have any binding errors in your output? Check you're binding to the correct column names. Then put a break point in your converter and see what you actually get in the array.
– Andy
Nov 21 '18 at 9:27
add a comment |
I changed Binding Path as you suggested and changed converter toreturn (int)values[0]*(int)values[1];
but still I can see blank colum. I changed it for a moment toreturn String.Format("{0}", values[0]);
to check but column shows "dependencyproperty.unsetvalue". I understand that I corrected code with casting to int, but values are not send to the column. Data in column are not even null, but unset. Could I do something else with binding?
– Mlorism
Nov 20 '18 at 20:54
It expects an object from your binding which it'll convert to string itself. Do you have any binding errors in your output? Check you're binding to the correct column names. Then put a break point in your converter and see what you actually get in the array.
– Andy
Nov 21 '18 at 9:27
I changed Binding Path as you suggested and changed converter to
return (int)values[0]*(int)values[1];
but still I can see blank colum. I changed it for a moment to return String.Format("{0}", values[0]);
to check but column shows "dependencyproperty.unsetvalue". I understand that I corrected code with casting to int, but values are not send to the column. Data in column are not even null, but unset. Could I do something else with binding?– Mlorism
Nov 20 '18 at 20:54
I changed Binding Path as you suggested and changed converter to
return (int)values[0]*(int)values[1];
but still I can see blank colum. I changed it for a moment to return String.Format("{0}", values[0]);
to check but column shows "dependencyproperty.unsetvalue". I understand that I corrected code with casting to int, but values are not send to the column. Data in column are not even null, but unset. Could I do something else with binding?– Mlorism
Nov 20 '18 at 20:54
It expects an object from your binding which it'll convert to string itself. Do you have any binding errors in your output? Check you're binding to the correct column names. Then put a break point in your converter and see what you actually get in the array.
– Andy
Nov 21 '18 at 9:27
It expects an object from your binding which it'll convert to string itself. Do you have any binding errors in your output? Check you're binding to the correct column names. Then put a break point in your converter and see what you actually get in the array.
– Andy
Nov 21 '18 at 9:27
add a comment |
As an alternative you could modify your sql-query to:
"select *, (Pcs_DC_col * Qty_multi_col) as Qty_DC_col from [products]"
and then bind normally to that column. (or whatever the column is named)
add a comment |
As an alternative you could modify your sql-query to:
"select *, (Pcs_DC_col * Qty_multi_col) as Qty_DC_col from [products]"
and then bind normally to that column. (or whatever the column is named)
add a comment |
As an alternative you could modify your sql-query to:
"select *, (Pcs_DC_col * Qty_multi_col) as Qty_DC_col from [products]"
and then bind normally to that column. (or whatever the column is named)
As an alternative you could modify your sql-query to:
"select *, (Pcs_DC_col * Qty_multi_col) as Qty_DC_col from [products]"
and then bind normally to that column. (or whatever the column is named)
edited Nov 19 '18 at 20:44
answered Nov 19 '18 at 20:36
Henrik HansenHenrik Hansen
1,132711
1,132711
add a comment |
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.
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%2f53381567%2fcalculating-one-column-from-2-in-datagrid-through-converter%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