public class ItemDetails : INotifyPropertyChanged
{
private string itemCode;
public string ItemCode
{
get
{
return itemCode;
}
set
{
itemCode = value;
RaisePropertyChanged("ItemCode");
}
}
private string itemName;
public string ItemName
{
get
{
return itemName;
}
set
{
itemName = value;
RaisePropertyChanged("ItemName");
}
}
private int itemID;
public int ItemID
{
get
{
return itemID;
}
set
{
itemID = value;
RaisePropertyChanged("ItemID");
}
}
} |
public class OrderDetails:INotifyPropertyChanged
{
private string selName;
public string SelName
{
get
{
return selName;
}
set
{
selName = value;
RaisePropertyChanged("SelName");
}
}
} |
public void PopulateItemDetails()
{
itemDetails = new ObservableCollection<ItemDetails>();
itemDetails.Add(new ItemDetails() { ItemName = "SfDatagrid", ItemID = 01, ItemCode = "AAA" });
itemDetails.Add(new ItemDetails() { ItemName = "SfCellGrid", ItemID = 02, ItemCode = "AAB" });
itemDetails.Add(new ItemDetails() { ItemName = "SfComnboBox", ItemID = 03, ItemCode = "AAC"});
itemDetails.Add(new ItemDetails() { ItemName = "SfTreeGrid", ItemID = 04, ItemCode = "AAD" });
}
private ObservableCollection<ItemDetails> itemDetails;
public ObservableCollection<ItemDetails> ItemDetails
{
get
{
return itemDetails;
}
set
{
itemDetails = value;
}
} |
<Window.Resources>
<local:ViewModel x:Key="viewModel"/>
</Window.Resources>
<syncfusion:SfDataGrid x:Name="dataGrid" Grid.Column="0"
AutoGenerateColumns="False" AllowEditing="True"
AutoGenerateRelations="False"
AllowResizingColumns="False"
HideEmptyGridViewDefinition="True"
ItemsSource="{Binding Path=OrdersDetails}"
NavigationMode="Cell"
ShowGroupDropArea="True">
<syncfusion:SfDataGrid.DetailsViewDefinition>
<syncfusion:GridViewDefinition RelationalColumn="OrderDetails">
<syncfusion:GridViewDefinition.DataGrid>
<syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid" AllowEditing="True" >
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridComboBoxColumn ItemsSource="{Binding ItemDetails,Source={StaticResource viewModel}}"
MappingName="SelName"
DisplayMemberPath="ItemName"
SelectedValuePath="ItemName" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
</syncfusion:GridViewDefinition.DataGrid>
<syncfusion:GridViewDefinition >
</syncfusion:SfDataGrid.DetailsViewDefinition>
</syncfusion:SfDataGrid> |
public class SfDataGridBehavior : Behavior<SfDataGrid>
{
SfDataGrid dataGrid = null;
GridComboboxColumnRendererExt comboBoxRenderer = new GridComboboxColumnRendererExt();
protected override void OnAttached()
{
dataGrid = this.AssociatedObject as SfDataGrid;
dataGrid.DetailsViewLoading += DataGrid_DetailsViewLoading;
base.OnAttached();
}
private void DataGrid_DetailsViewLoading(object sender, DetailsViewLoadingAndUnloadingEventArgs e)
{
if (e.DetailsViewDataGrid.CellRenderers["ComboBox"] != comboBoxRenderer)
e.DetailsViewDataGrid.CellRenderers["ComboBox"] = comboBoxRenderer;
}
} |
public class GridComboboxColumnRendererExt:GridCellComboBoxRenderer
{
int selectedIndex = 0;
public GridComboboxColumnRendererExt():base()
{
}
protected override void OnUnwireEditUIElement(ComboBox uiElement)
{
if(uiElement.SelectedIndex == -1 && uiElement.SelectedValue== null)
{
uiElement.SelectedIndex = selectedIndex;
}
base.OnUnwireEditUIElement(uiElement);
}
protected override void OnEditElementLoaded(object sender, RoutedEventArgs e)
{
var combobox = sender as ComboBox;
if (combobox != null && combobox.SelectedIndex > -1)
selectedIndex = combobox.SelectedIndex;
base.OnEditElementLoaded(sender, e);
}
} |
public class SfDataGridBehavior : Behavior<SfDataGrid>
{
SfDataGrid dataGrid = null;
protected override void OnAttached()
{
dataGrid = this.AssociatedObject as SfDataGrid;
dataGrid.DetailsViewLoading += DataGrid_DetailsViewLoading;
base.OnAttached();
}
private void DataGrid_DetailsViewLoading(object sender, DetailsViewLoadingAndUnloadingEventArgs e)
{
(dataGrid.DetailsViewDefinition[0] asGridViewDefinition).DataGrid.CurrentCellValidating += DetailsViewDataGrid_CurrentCellValidating;
}
private void DetailsViewDataGrid_CurrentCellValidating(object sender, CurrentCellValidatingEventArgs e)
{
var detailsViewDataGrid = e.OriginalSender as DetailsViewDataGrid;
var currentColumn = detailsViewDataGrid.CurrentColumn;
var currentCell = detailsViewDataGrid.SelectionController.CurrentCellManager.CurrentCell;
if (currentColumn is GridComboBoxColumn)
{
var combobox = ((currentCell as DataColumnBase).ColumnElement as GridCell).Content asComboBox;
var itemName = combobox.Text;
var dataContext = (dataGrid.DataContext as ViewModel).ItemDetails;
var itemDetails = dataContext.FirstOrDefault(item => item.ItemName == itemName);
if (itemDetails == null)
{
dataContext.Add(new ItemDetails() { ItemName = itemName, ItemID = new Random().Next(6, 100), ItemCode = "itemName" });
}
e.NewValue = combobox.Text;
combobox.ItemsSource = dataContext;
}
} |