Skip to content

Instantly share code, notes, and snippets.

@unseensenpai
Last active January 24, 2024 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unseensenpai/5be6061f0919c7f537cd9564d54a03f7 to your computer and use it in GitHub Desktop.
Save unseensenpai/5be6061f0919c7f537cd9564d54a03f7 to your computer and use it in GitHub Desktop.
DevExpress GridView GetSelectedSurrogates Extension
/// <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;
}
}
// 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