Lostfocus event in a datagrid
I want lost focus event to be implemented for a column in a datagrid in wpf. I have used CellEditEnding event but this is getting raised on the start of editing not at the end. Please someone help me.
wpf
add a comment |
I want lost focus event to be implemented for a column in a datagrid in wpf. I have used CellEditEnding event but this is getting raised on the start of editing not at the end. Please someone help me.
wpf
add a comment |
I want lost focus event to be implemented for a column in a datagrid in wpf. I have used CellEditEnding event but this is getting raised on the start of editing not at the end. Please someone help me.
wpf
I want lost focus event to be implemented for a column in a datagrid in wpf. I have used CellEditEnding event but this is getting raised on the start of editing not at the end. Please someone help me.
wpf
wpf
asked Oct 15 '12 at 8:55
LayaLaya
3419
3419
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
i have used that event and it's working perfect.
my xaml Code Look like
<DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490" ItemsSource="{Binding DeleteDate,Mode=TwoWay}" Margin="6,7,0,0" CanUserDeleteRows="True" RowEditEnding="dgEmp_RowEditEnding" CellEditEnding="dgEmp_CellEditEnding" Grid.RowSpan="3">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Update" x:Name="btnUpdate"
Click="btnUpdate_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete"
Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
in my .cs file Code look like
private void dgEmp_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var update = from p in Context.Expense_Submits where p.ID == objEmpToEdit.ID select p;
foreach (var item in update)
{
item.ID = objEmpToEdit.ID;
item.Description = objEmpToEdit.Description;
item.Date = objEmpToEdit.Date;
item.Amount = objEmpToEdit.Amount;
item.Remark = objEmpToEdit.Remark;
}
Context.SubmitChanges();
MessageBox.Show("The Current row updation is complete..");
}
private void dgEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (isUpdateMode) //The Row is edited
{
Expense_Submit exp_sub = (from exp in submit where exp.ID == objEmpToEdit.ID select exp).FirstOrDefault();
FrameworkElement element_1 = dgEmp.Columns[1].GetCellContent(e.Row);
if (element_1.GetType() == typeof(TextBox))
{
var Description = ((TextBox)element_1).Text;
objEmpToEdit.Description = Description.ToString();
}
FrameworkElement element_2 = dgEmp.Columns[2].GetCellContent(e.Row);
if (element_2.GetType() == typeof(TextBox))
{
var Amount = ((TextBox)element_2).Text;
objEmpToEdit.Amount = Amount.ToString();
}
FrameworkElement element_3 = dgEmp.Columns[3].GetCellContent(e.Row);
if (element_3.GetType() == typeof(TextBox))
{
var Date = ((TextBox)element_3).Text;
objEmpToEdit.Date = Convert.ToDateTime(Date);
}
FrameworkElement element_4 = dgEmp.Columns[4].GetCellContent(e.Row);
if (element_4.GetType() == typeof(TextBox))
{
var Remark = ((TextBox)element_4).Text;
objEmpToEdit.Remark = Remark.ToString();
}
}
}
private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
objEmpToEdit = dgEmp.SelectedItem as Expense_Submit;
}
add a comment |
Why not use LostFocus event on cells, then check if the cell belongs to the column you want.
For example:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Then in the handler:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
if (((DataGridCell)sender).Column.Header == "My Column")
//do stuff
}
You gotta add "ToString()" to make this work : if (((DataGridCell)sender).Column.Header.ToString() == "My Column")
– George
Nov 21 '18 at 16:24
add a comment |
The solution provided by Esam works needs a little edit:
XAML remains the same:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Code behind to be adapted:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
var myCell = sender as DataGridCell;
if (myCell.Column.Header.ToString() == "Name of the column")
{
MessageBox.Show("tralala. I got it");
//do whatever needed here
}
}
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%2f12892210%2flostfocus-event-in-a-datagrid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
i have used that event and it's working perfect.
my xaml Code Look like
<DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490" ItemsSource="{Binding DeleteDate,Mode=TwoWay}" Margin="6,7,0,0" CanUserDeleteRows="True" RowEditEnding="dgEmp_RowEditEnding" CellEditEnding="dgEmp_CellEditEnding" Grid.RowSpan="3">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Update" x:Name="btnUpdate"
Click="btnUpdate_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete"
Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
in my .cs file Code look like
private void dgEmp_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var update = from p in Context.Expense_Submits where p.ID == objEmpToEdit.ID select p;
foreach (var item in update)
{
item.ID = objEmpToEdit.ID;
item.Description = objEmpToEdit.Description;
item.Date = objEmpToEdit.Date;
item.Amount = objEmpToEdit.Amount;
item.Remark = objEmpToEdit.Remark;
}
Context.SubmitChanges();
MessageBox.Show("The Current row updation is complete..");
}
private void dgEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (isUpdateMode) //The Row is edited
{
Expense_Submit exp_sub = (from exp in submit where exp.ID == objEmpToEdit.ID select exp).FirstOrDefault();
FrameworkElement element_1 = dgEmp.Columns[1].GetCellContent(e.Row);
if (element_1.GetType() == typeof(TextBox))
{
var Description = ((TextBox)element_1).Text;
objEmpToEdit.Description = Description.ToString();
}
FrameworkElement element_2 = dgEmp.Columns[2].GetCellContent(e.Row);
if (element_2.GetType() == typeof(TextBox))
{
var Amount = ((TextBox)element_2).Text;
objEmpToEdit.Amount = Amount.ToString();
}
FrameworkElement element_3 = dgEmp.Columns[3].GetCellContent(e.Row);
if (element_3.GetType() == typeof(TextBox))
{
var Date = ((TextBox)element_3).Text;
objEmpToEdit.Date = Convert.ToDateTime(Date);
}
FrameworkElement element_4 = dgEmp.Columns[4].GetCellContent(e.Row);
if (element_4.GetType() == typeof(TextBox))
{
var Remark = ((TextBox)element_4).Text;
objEmpToEdit.Remark = Remark.ToString();
}
}
}
private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
objEmpToEdit = dgEmp.SelectedItem as Expense_Submit;
}
add a comment |
i have used that event and it's working perfect.
my xaml Code Look like
<DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490" ItemsSource="{Binding DeleteDate,Mode=TwoWay}" Margin="6,7,0,0" CanUserDeleteRows="True" RowEditEnding="dgEmp_RowEditEnding" CellEditEnding="dgEmp_CellEditEnding" Grid.RowSpan="3">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Update" x:Name="btnUpdate"
Click="btnUpdate_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete"
Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
in my .cs file Code look like
private void dgEmp_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var update = from p in Context.Expense_Submits where p.ID == objEmpToEdit.ID select p;
foreach (var item in update)
{
item.ID = objEmpToEdit.ID;
item.Description = objEmpToEdit.Description;
item.Date = objEmpToEdit.Date;
item.Amount = objEmpToEdit.Amount;
item.Remark = objEmpToEdit.Remark;
}
Context.SubmitChanges();
MessageBox.Show("The Current row updation is complete..");
}
private void dgEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (isUpdateMode) //The Row is edited
{
Expense_Submit exp_sub = (from exp in submit where exp.ID == objEmpToEdit.ID select exp).FirstOrDefault();
FrameworkElement element_1 = dgEmp.Columns[1].GetCellContent(e.Row);
if (element_1.GetType() == typeof(TextBox))
{
var Description = ((TextBox)element_1).Text;
objEmpToEdit.Description = Description.ToString();
}
FrameworkElement element_2 = dgEmp.Columns[2].GetCellContent(e.Row);
if (element_2.GetType() == typeof(TextBox))
{
var Amount = ((TextBox)element_2).Text;
objEmpToEdit.Amount = Amount.ToString();
}
FrameworkElement element_3 = dgEmp.Columns[3].GetCellContent(e.Row);
if (element_3.GetType() == typeof(TextBox))
{
var Date = ((TextBox)element_3).Text;
objEmpToEdit.Date = Convert.ToDateTime(Date);
}
FrameworkElement element_4 = dgEmp.Columns[4].GetCellContent(e.Row);
if (element_4.GetType() == typeof(TextBox))
{
var Remark = ((TextBox)element_4).Text;
objEmpToEdit.Remark = Remark.ToString();
}
}
}
private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
objEmpToEdit = dgEmp.SelectedItem as Expense_Submit;
}
add a comment |
i have used that event and it's working perfect.
my xaml Code Look like
<DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490" ItemsSource="{Binding DeleteDate,Mode=TwoWay}" Margin="6,7,0,0" CanUserDeleteRows="True" RowEditEnding="dgEmp_RowEditEnding" CellEditEnding="dgEmp_CellEditEnding" Grid.RowSpan="3">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Update" x:Name="btnUpdate"
Click="btnUpdate_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete"
Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
in my .cs file Code look like
private void dgEmp_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var update = from p in Context.Expense_Submits where p.ID == objEmpToEdit.ID select p;
foreach (var item in update)
{
item.ID = objEmpToEdit.ID;
item.Description = objEmpToEdit.Description;
item.Date = objEmpToEdit.Date;
item.Amount = objEmpToEdit.Amount;
item.Remark = objEmpToEdit.Remark;
}
Context.SubmitChanges();
MessageBox.Show("The Current row updation is complete..");
}
private void dgEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (isUpdateMode) //The Row is edited
{
Expense_Submit exp_sub = (from exp in submit where exp.ID == objEmpToEdit.ID select exp).FirstOrDefault();
FrameworkElement element_1 = dgEmp.Columns[1].GetCellContent(e.Row);
if (element_1.GetType() == typeof(TextBox))
{
var Description = ((TextBox)element_1).Text;
objEmpToEdit.Description = Description.ToString();
}
FrameworkElement element_2 = dgEmp.Columns[2].GetCellContent(e.Row);
if (element_2.GetType() == typeof(TextBox))
{
var Amount = ((TextBox)element_2).Text;
objEmpToEdit.Amount = Amount.ToString();
}
FrameworkElement element_3 = dgEmp.Columns[3].GetCellContent(e.Row);
if (element_3.GetType() == typeof(TextBox))
{
var Date = ((TextBox)element_3).Text;
objEmpToEdit.Date = Convert.ToDateTime(Date);
}
FrameworkElement element_4 = dgEmp.Columns[4].GetCellContent(e.Row);
if (element_4.GetType() == typeof(TextBox))
{
var Remark = ((TextBox)element_4).Text;
objEmpToEdit.Remark = Remark.ToString();
}
}
}
private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
objEmpToEdit = dgEmp.SelectedItem as Expense_Submit;
}
i have used that event and it's working perfect.
my xaml Code Look like
<DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490" ItemsSource="{Binding DeleteDate,Mode=TwoWay}" Margin="6,7,0,0" CanUserDeleteRows="True" RowEditEnding="dgEmp_RowEditEnding" CellEditEnding="dgEmp_CellEditEnding" Grid.RowSpan="3">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Update" x:Name="btnUpdate"
Click="btnUpdate_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" x:Name="btnDelete"
Click="btnDelete_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
in my .cs file Code look like
private void dgEmp_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var update = from p in Context.Expense_Submits where p.ID == objEmpToEdit.ID select p;
foreach (var item in update)
{
item.ID = objEmpToEdit.ID;
item.Description = objEmpToEdit.Description;
item.Date = objEmpToEdit.Date;
item.Amount = objEmpToEdit.Amount;
item.Remark = objEmpToEdit.Remark;
}
Context.SubmitChanges();
MessageBox.Show("The Current row updation is complete..");
}
private void dgEmp_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (isUpdateMode) //The Row is edited
{
Expense_Submit exp_sub = (from exp in submit where exp.ID == objEmpToEdit.ID select exp).FirstOrDefault();
FrameworkElement element_1 = dgEmp.Columns[1].GetCellContent(e.Row);
if (element_1.GetType() == typeof(TextBox))
{
var Description = ((TextBox)element_1).Text;
objEmpToEdit.Description = Description.ToString();
}
FrameworkElement element_2 = dgEmp.Columns[2].GetCellContent(e.Row);
if (element_2.GetType() == typeof(TextBox))
{
var Amount = ((TextBox)element_2).Text;
objEmpToEdit.Amount = Amount.ToString();
}
FrameworkElement element_3 = dgEmp.Columns[3].GetCellContent(e.Row);
if (element_3.GetType() == typeof(TextBox))
{
var Date = ((TextBox)element_3).Text;
objEmpToEdit.Date = Convert.ToDateTime(Date);
}
FrameworkElement element_4 = dgEmp.Columns[4].GetCellContent(e.Row);
if (element_4.GetType() == typeof(TextBox))
{
var Remark = ((TextBox)element_4).Text;
objEmpToEdit.Remark = Remark.ToString();
}
}
}
private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
objEmpToEdit = dgEmp.SelectedItem as Expense_Submit;
}
answered Oct 15 '12 at 9:23
Dhaval PatelDhaval Patel
4,77132755
4,77132755
add a comment |
add a comment |
Why not use LostFocus event on cells, then check if the cell belongs to the column you want.
For example:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Then in the handler:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
if (((DataGridCell)sender).Column.Header == "My Column")
//do stuff
}
You gotta add "ToString()" to make this work : if (((DataGridCell)sender).Column.Header.ToString() == "My Column")
– George
Nov 21 '18 at 16:24
add a comment |
Why not use LostFocus event on cells, then check if the cell belongs to the column you want.
For example:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Then in the handler:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
if (((DataGridCell)sender).Column.Header == "My Column")
//do stuff
}
You gotta add "ToString()" to make this work : if (((DataGridCell)sender).Column.Header.ToString() == "My Column")
– George
Nov 21 '18 at 16:24
add a comment |
Why not use LostFocus event on cells, then check if the cell belongs to the column you want.
For example:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Then in the handler:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
if (((DataGridCell)sender).Column.Header == "My Column")
//do stuff
}
Why not use LostFocus event on cells, then check if the cell belongs to the column you want.
For example:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Then in the handler:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
if (((DataGridCell)sender).Column.Header == "My Column")
//do stuff
}
answered Jul 3 '14 at 15:44
Esam SherifEsam Sherif
10219
10219
You gotta add "ToString()" to make this work : if (((DataGridCell)sender).Column.Header.ToString() == "My Column")
– George
Nov 21 '18 at 16:24
add a comment |
You gotta add "ToString()" to make this work : if (((DataGridCell)sender).Column.Header.ToString() == "My Column")
– George
Nov 21 '18 at 16:24
You gotta add "ToString()" to make this work : if (((DataGridCell)sender).Column.Header.ToString() == "My Column")
– George
Nov 21 '18 at 16:24
You gotta add "ToString()" to make this work : if (((DataGridCell)sender).Column.Header.ToString() == "My Column")
– George
Nov 21 '18 at 16:24
add a comment |
The solution provided by Esam works needs a little edit:
XAML remains the same:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Code behind to be adapted:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
var myCell = sender as DataGridCell;
if (myCell.Column.Header.ToString() == "Name of the column")
{
MessageBox.Show("tralala. I got it");
//do whatever needed here
}
}
add a comment |
The solution provided by Esam works needs a little edit:
XAML remains the same:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Code behind to be adapted:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
var myCell = sender as DataGridCell;
if (myCell.Column.Header.ToString() == "Name of the column")
{
MessageBox.Show("tralala. I got it");
//do whatever needed here
}
}
add a comment |
The solution provided by Esam works needs a little edit:
XAML remains the same:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Code behind to be adapted:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
var myCell = sender as DataGridCell;
if (myCell.Column.Header.ToString() == "Name of the column")
{
MessageBox.Show("tralala. I got it");
//do whatever needed here
}
}
The solution provided by Esam works needs a little edit:
XAML remains the same:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="LostFocus" Handler="OnCellLostFocus"/>
</Style>
</DataGrid.CellStyle>
Code behind to be adapted:
private void OnCellLostFocus(object sender, RoutedEventArgs e)
{
var myCell = sender as DataGridCell;
if (myCell.Column.Header.ToString() == "Name of the column")
{
MessageBox.Show("tralala. I got it");
//do whatever needed here
}
}
edited Dec 6 '18 at 17:54
answered Nov 21 '18 at 16:26
GeorgeGeorge
94110
94110
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%2f12892210%2flostfocus-event-in-a-datagrid%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