I am using WPF datagrid. I am providing context menu for row-header and column-header. The code is as mentioned below:
MainWindow.xaml
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cmd="clr-namespace:WpfApplication10"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Insert Column Before" Command="cmd:ContextMenuCustomCommands.InsertColumnBeforeCommand" />
<MenuItem Header="Insert Column After" Command="cmd:ContextMenuCustomCommands.InsertColumnAfterCommand"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Delete" Command="Delete"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Grid.CommandBindings>
<CommandBinding Command="cmd:ContextMenuCustomCommands.InsertColumnBeforeCommand" CanExecute="InserColumnBefore_CanExecute" Executed="InserColumnBefore_Executed"/>
<CommandBinding Command="cmd:ContextMenuCustomCommands.InsertColumnAfterCommand" CanExecute="InsertColumnAfter_CanExecute" Executed="InsertColumnAfter_Executed"/>
</Grid.CommandBindings>
<DataGrid x:Name="grdEmployee" ItemsSource="{Binding}" AutoGeneratingColumn="grdEmployee_AutoGeneratingColumn" LoadingRow="grdEmployee_LoadingRow" SelectionUnit="CellOrRowHeader">
</DataGrid>
</Grid>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace WpfApplication10
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private SqlConnection empCon = null;
private SqlCommand empCmd = null;
private DataSet empDS = null;
private SqlDataAdapter empAdap = null;
private string query = "SELECT * FROM Employees";
public MainWindow()
{
InitializeComponent();
this.grdEmployee.ItemsSource = this.GetEmployeeData().DefaultView;
}
private DataTable GetEmployeeData()
{
try
{
empCon = new SqlConnection(Application.Current.Properties["connectionStr"].ToString());
empCmd = new SqlCommand(query, empCon);
empAdap = new SqlDataAdapter(empCmd);
empDS = new DataSet();
empAdap.Fill(empDS);
}
catch (SqlException sqlEx)
{
}
finally
{
if (empCon != null)
empCon.Dispose();
if (empCmd != null)
empCmd.Dispose();
}
if (empDS != null)
{
for (int count = 1; count <= empDS.Tables[0].Columns.Count; count++)
{
empDS.Tables[0].Columns[count - 1].ColumnName = Utility.ConvertNumber(count).ToString();
}
return empDS.Tables[0];
}
else
{
return null;
}
}
private void grdEmployee_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
}
private void grdEmployee_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString();
}
private void InserColumnBefore_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void InserColumnBefore_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
private void InsertColumnAfter_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void InsertColumnAfter_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
}
public class Utility
{
private static string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static string ConvertNumber(int number)
{
string result;
number -= 1;
int rest = number % 26;
int q = number / 26;
if (q == 0)
{
result = chars[rest].ToString();
}
else
{
result = ConvertNumber(q) + chars[rest];
}
return result;
}
}
public static class ContextMenuCustomCommands
{
public static readonly RoutedUICommand InsertColumnBeforeCommand = new RoutedUICommand("Insert Column Before", "Insert Column Before", typeof(MainWindow));
public static readonly RoutedUICommand InsertColumnAfterCommand = new RoutedUICommand("Insert Column After", "Insert Column After", typeof(MainWindow));
}
}
App.xaml.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
namespace WpfApplication10
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
string connectionStr = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind;Data Source=(local);";
Application.Current.Properties["connectionStr"] = connectionStr;
}
}
}
The problem I am facing is, The context menu on column header remain disabled until and unless I access the datagrid. I am not able to figure out the reason.
Please help me. Do let me know if you need any other info.
As noted above the problem seemed to be stemming from the fact that the DataGrid was not focused when the click was made.
Calling
gid.Focus();in the loaded event fixed the problem by bringing focus on the GridView.As for detecting the column clicked, you can likely get that through an event handler similar to getting Cell and Row information detailed in this article