Last active
January 24, 2024 08:14
-
-
Save unseensenpai/5be6061f0919c7f537cd9564d54a03f7 to your computer and use it in GitHub Desktop.
DevExpress GridView GetSelectedSurrogates Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// It is the method that returns the selected data in generic type | |
/// using the repository view in all Devexpress components that can make selections. | |
/// </summary> | |
/// <typeparam name="T">Surrogate Type</typeparam> | |
/// <param name="gridView">Repository view on component</param> | |
/// <returns></returns> | |
public static IEnumerable<T> GetSelectedSurrogates<T>(this GridView gridView) where T : class | |
{ | |
if (gridView.SelectedRowsCount > 0) | |
{ | |
var selectedRowHandles = gridView.GetSelectedRows(); | |
IList<T> selectedSurrogates = []; | |
foreach (var rowHandle in selectedRowHandles) | |
{ | |
var surrogate = gridView.GetRow(rowHandle) as T; | |
selectedSurrogates.Add(surrogate); | |
} | |
return selectedSurrogates; | |
} | |
else | |
{ | |
return Enumerable.Empty<T>(); | |
} | |
} | |
/// <summary> | |
/// It is the method that returns the selected data in generic type | |
/// using the repository view in all Devexpress components that can make selection. | |
/// </summary> | |
/// <typeparam name="T">Surrogate Type</typeparam> | |
/// <param name="gridView">Repository view on component</param> | |
/// <returns></returns> | |
public static T GetSelectedFirstOrDefaultSurrogate<T>(this GridView gridView) where T : class | |
{ | |
var selectedRowHandle = gridView.GetSelectedRows().FirstOrDefault(); | |
if (selectedRowHandle >= 0) | |
{ | |
return gridView.GetRow(selectedRowHandle) as T; | |
} | |
else | |
{ | |
return default; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MULTI SELECT | |
private void SLUE_PrintersLocations_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e) | |
{ | |
IEnumerable<SaleLocationSurrogate> selectedSaleLocationSurrogates = SLUEV_PrintersLocations.GetSelectedSurrogates<SaleLocationSurrogate>(); | |
SelectedItems = selectedSaleLocationSurrogates; | |
} | |
public IEnumerable<SaleLocationSurrogate> SelectedItems { get; set; } | |
// SINGLE ITEM | |
private void SLUE_PrintersLocations_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e) | |
{ | |
SelectedItem = SLUEV_PrintersLocations.GetSelectedFirstOrDefaultSurrogate<SaleLocationSurrogate>(); | |
} | |
public SaleLocationSurrogate SelectedItem { get; set; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment