Top n Record from infragistics xamdatagrid in wpf - infragistics

I have a wpf application in which I am using xamdatagrid of infragistics 14.2 to show data.
Sometimes grid may contains more than 1000 record.In same application I have given print option to print grid data.for printing I am using Report object of Infragistics.but before printing I am generating preview of print data using xamprintpreview control.
When grid contains large data (for 1000 record) it takes 35 seconds to generate preview for 1000 records.
I am binding Datatable to xamdatagrid(so you can say every record is a type of DataRow).
To speedup the process of preview I have an idea to show only top n record of grid in preview.but I am not able to fetch top n record of grid and store them in a datatable because if I take 10 record from datatable that I have binded to grid and user has applied some filter on grid so my preview mismatch with actual grid.
So please help me to fetch tp 10 record from grid that is currently displayed and store them in
datatable.
Thanks in advance...

You have a complete working example here
The keys are:
xamDataGrid.RecordManager.GetFilteredInDataRecords() gives your the filtered records. If you have not any filter it gives you all records.
Then Take(10) will take ten as much.
And I implemented ToDataTable as an extension method to convert the records taken to a datatable.
<Window x:Class="Stackoverflow1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:igDP="http://infragistics.com/DataPresenter"
xmlns:local="clr-namespace:Stackoverflow1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<igDP:XamDataGrid x:Name="xamDataGrid" Loaded="XamDataGrid_Loaded">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings FilterUIType="LabelIcons"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings AllowRecordFiltering="True" FilterLabelIconDropDownType="MultiSelectExcelStyle"/>
</igDP:XamDataGrid.FieldSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:Field Name="Date"/>
<igDP:Field Name="Order"/>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
<Button x:Name="btnPrintPreview" Content="Print Preview" Height="25" Width="100" Click="btnPrintPreview_Click"/>
</Grid>
</Window>
using Infragistics.Windows.DataPresenter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
namespace Stackoverflow1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void XamDataGrid_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<Item> list = new ObservableCollection<Item>();
for (int i = 0; i < 15; ++i)
{
list.Add(new Item { Date = DateTime.Now.AddDays(-i), Order = i});
}
xamDataGrid.DataSource = list;
}
private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
{
IEnumerable<DataRecord> data = xamDataGrid.RecordManager.GetFilteredInDataRecords();
DataTable dt = data.Take(10).ToDataTable();
// DO YOUR PRINT PREVIEW
}
}
class Item
{
public DateTime Date { get; set; }
public int Order { get; set; }
}
static class Utils
{
public static DataTable ToDataTable(this IEnumerable<DataRecord> items)
{
if (items.Count() > 0)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Item));
DataTable table = new DataTable();
List<Item> list = new List<Item>();
foreach (var item in items)
{
list.Add((Item)item.DataItem);
}
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (Item item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
return null;
}
}
}

In the code behind in an event handler:
private void Button_ClickEventHandler(object sender, MouseButtonEventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Data1");
table.Columns.Add("Data2"); //etc... Add all columns necessary
var records = XamDataGridName.Records.Take(10);
foreach (var record in records)
{
var item = ((DataRecord)record).DataItem as DataType;
if (item != null)
{
table.Rows.Add(item.Data1, item.Data2);
}
}
}
Or using MVVM you can send the XamDataGrid.Records as a command parameter to a command in your view model and then loop through the data records, adding them to your data table.

Related

Integrate Blazor with Chart.js: how to pass an object

I want to display in my Blazor WebAssembly application, some graphs with Chart.js. I tried to use Chartjs.Blazor.Fork but I have few errors, for example I have opened another post about here.
So, after a day without results, I decided to start my own component. I follow the instruction I found in a blog. Basically, I have my Razor component called Chart.razor with the following code
#inject IJSRuntime JSRuntime
<canvas id="#Id"></canvas>
#code {
public enum ChartType
{
Pie,
Bar
}
[Parameter]
public string Id { get; set; }
[Parameter]
public ChartType Type { get; set; }
[Parameter]
public string[] Data { get; set; }
[Parameter]
public string[] BackgroundColor { get; set; }
[Parameter]
public string[] Labels { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Here we create an anonymous type with all the options
// that need to be sent to Chart.js
var config = new
{
Type = Type.ToString().ToLower(),
Options = new
{
Responsive = true,
Scales = new
{
YAxes = new[]
{
new { Ticks = new {
BeginAtZero=true
} }
}
}
},
Data = new
{
Datasets = new[]
{
new { Data = Data, BackgroundColor = BackgroundColor}
},
Labels = Labels
}
};
await JSRuntime.InvokeVoidAsync("setup", Id, config);
}
}
then I have my own mychart.js script to update the chart
window.setup = (id,config) => {
var ctx = document.getElementById(id).getContext('2d');
new Chart(ctx, config);
}
So, I use this code
<Chart Id="bar1" Type="#Chart.ChartType.Bar"
Data="#(new[] { " 10", "9" } )"
BackgroundColor="#(new[] { " yellow","red"} )"
Labels="#(new[] { " Fail","Ok" } )">
</Chart>
Ugly code but it is working. Now, I can display a graph in my page. Cool! What I want to display is something more complex, because I have to show a stacked bar graph with groups and the configuration is quite complicated.
I want to replace the config you can see in the page with for example a class. In this class I want to collect all configuration, like Type, Options, Data, Labels and so on, and pass them in the await JSRuntime.InvokeVoidAsync("setup", Id, config);`
For starting I created my base class like
public abstract class ConfigBase
{
protected ConfigBase(ChartType chartType)
{
Type = chartType;
}
public ChartType Type { get; }
public string CanvasId { get; } = Guid.NewGuid().ToString();
}
My problem is how to transform this class to obtain a valid object for the JavaScript to execute correctly new Chart(ctx, config);.
OK, it was quite easy. I saw a lot of different technics for that but there is a very basic simple way
await JSRuntime.InvokeVoidAsync("setup", Config.CanvasId, Config);

insert whole list of data into sql database table in xamarin.android

i'm trying to create an application where data in a list must be inserted into a database table at once. I made some research and found out that this is possible using user-defined table types where in c# a datatable is used and passed to a stored procedure that is executed. now my problem is that there are no data tables in Xamarin.Android. so I thought to use a list instead. my idea was to create a list in the application and pass it to the webservice method, and in my webservice method I receive the list and convert it to a datatable then pass it as a parameter to the stored procedure. I wrote the following codes:
in my webservice:
[WebMethod]
public bool insrt_dt(List<Class1> lst)
{
SqlParameter param;
SqlConnection conn = new SqlConnection(new DBConnection().ConnectionString);
DataTable dt = list_to_dt(lst);
SqlCommand cmd = new SqlCommand("Insert_Customers", conn);
cmd.CommandType = CommandType.StoredProcedure;
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
param = new SqlParameter("#tblcustomers", dt);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
cmd.Parameters.Add(param);
cmd.CommandTimeout = 300;
int a=cmd.ExecuteNonQuery();
if (a > 0)
{
return true;
}
else return false;
}
}
Class1:
public class Class1
{
public int id { get; set; }
public string name { get; set; }
public string country { get; set; }
}
in my Xamarin.Android app
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Button btn = FindViewById<Button>(Resource.Id.button1);
btn.Click += delegate
{
wr.WebService1 ws = new wr.WebService1();
wr.Class1 class1 = new wr.Class1();
List<wr.Class1> lst = new List<wr.Class1>(){
new wr.Class1() { id = 1, name = "hgf", country = "khg" },
new wr.Class1() { id = 2, name = "hgf", country = "khg"} };
ws.insrt_dt(lst);
ws.insrt_dtCompleted += Ws_insrt_dtCompleted;
};
}
private void Ws_insrt_dtCompleted(object sender, wr.insrt_dtCompletedEventArgs e)
{
bool l = e.Result;
if (l == true)
Toast.MakeText(this, "khh", ToastLength.Long).Show();
else
Toast.MakeText(this, "ijo'pioo", ToastLength.Long).Show();
}
}
but I keep getting this error:
Argument 1: cannot convert from 'System.Collections.Generic.List<app_dt.wr.Class1>' to 'app_dt.wr.Class1[]
so I used these lines instead
new wr.Class1() { id = 1, name = "hgf", country = "khg" },
new wr.Class1() { id = 2, name = "hgf", country = "khg"} };
wr.Class1[] class1s = lst.ToArray();
ws.insrt_dt(class1s);
now I don't get an error, but it doesn't work, I mean why does it say that the webservice method input must be an array and I've created it as a list. any suggestions for this?
As i know, Xamarin do not support System.Data.SqlClient. If you want to use the database for the Xamarin android project, you could use the SQLite.NET.
Install the package from the NuGet.
NuGet: sqlite-net-pcl https://www.nuget.org/packages/sqlite-net-pcl/
For the code sample about how to use the database in Xamarin, you could check the link below. https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows
For Xamarin.Android, you could check the sample code in the link below. https://learn.microsoft.com/en-us/xamarin/android/data-cloud/data-access/using-sqlite-orm

How to add a custom field into the ARInvoice Customer selector?

There is a custom field, that I declared for the Customer DAC:
public class CustomerExt : PXCacheExtension<Customer>
{
#region UsrDemoField
[PXDBString(255)]
[PXUIField(DisplayName = "Demo Field")]
public virtual string UsrDemoField { get; set; }
public abstract class usrDemoField : IBqlField { }
#endregion
}
Attempts to modify the ARInvoice Customer selector with the Customize Selector Columns popup didn't seem to work. How can I add my custom field into the ARInvoice customer selector?
Be aware, since Acumatica ERP build #17.201.0043, it's possible to customize the list of columns defined for AR Invoices' Customer lookup via the Customize Selector Columns dialog (available in the Data Class section of the Customization Manager). For step-by-step instructions please check the screenshot below:
To modify AR Invoices' Customer lookup on Acumatica ERP ver. 6.1 and earlier, please follow the steps below:
The definition of PXCustomizeSelectorColumns generated by the Customize Selector Columns popup brilliantly works with the majority of selectors inside Acumatica ERP. Basically, PXCustomizeSelectorColumns simply replaces originally defined columns for a selector with the custom set of columns during an initialization of PXCache:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false)]
public class PXCustomizeSelectorColumns: PXEventSubscriberAttribute
{
private readonly Type[] _columns;
public PXCustomizeSelectorColumns(params Type[] columns)
{
_columns = columns;
}
public override void CacheAttached(PXCache cache)
{
cache.SetAltered(this.FieldName, true);
foreach (PXEventSubscriberAttribute attr in cache.GetAttributes(null, this.FieldName))
{
PXSelectorAttribute sel = attr as PXSelectorAttribute;
if (sel == null)
continue;
sel.SetFieldList(_columns);
sel.Headers = null;
}
}
}
So what can cause the PXCustomizeSelectorColumns attribute to fail and not replace selector's originally defined columns? Any time the SetColumns method is executed on an instance of PXDimensionSelectorAttribute or PXSelectorAttribute after PXCache was initialized, there is no chance for PXCustomizeSelectorColumns to do its job.
[PXDBInt()]
[PXUIField(DisplayName = "Customer", Visibility = PXUIVisibility.Visible)]
[Serializable]
public class CustomerAttribute : AcctSubAttribute
{
...
public virtual void FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
{
if (this.AttributeLevel == PXAttributeLevel.Item || e.IsAltered)
{
PopulateFields(sender);
}
PXFieldSelecting handler = GetAttribute<PXDimensionSelectorAttribute>().FieldSelecting;
handler(sender, e);
}
protected virtual void PopulateFields(PXCache sender)
{
if (_FieldList == null)
{
_FieldList = new string[this._fields.Length];
_HeaderList = new string[this._fields.Length];
for (int i = 0; i < this._fields.Length; i++)
{
Type cacheType = BqlCommand.GetItemType(_fields[i]);
PXCache cache = sender.Graph.Caches[cacheType];
if (cacheType.IsAssignableFrom(typeof(BAccountR)) ||
_fields[i].Name == typeof(BAccountR.acctCD).Name ||
_fields[i].Name == typeof(BAccountR.acctName).Name)
{
_FieldList[i] = _fields[i].Name;
}
else
{
_FieldList[i] = cacheType.Name + "__" + _fields[i].Name;
}
_HeaderList[i] = PXUIFieldAttribute.GetDisplayName(cache, _fields[i].Name);
}
}
var attr = GetAttribute<PXDimensionSelectorAttribute>().GetAttribute<PXSelectorAttribute>();
attr.SetColumns(_FieldList, _HeaderList);
}
...
}
With that said, to add a custom field into the ARInvoice Customer selector, one should replace all attributes declared for the ARInvoice.CustomerID field and redefine columns for the Customer selector within the CustomerActive attribute:
[PXDefault()]
[CustomerActive(typeof(Search<BAccountR.bAccountID>),
new Type[]
{
typeof(BAccountR.acctCD),
typeof(BAccountR.acctName),
typeof(CustomerExt.usrDemoField),
typeof(Address.addressLine1),
typeof(Address.addressLine2),
typeof(Address.postalCode),
typeof(CustomerAttribute.Contact.phone1),
typeof(Address.city),
typeof(Address.countryID),
typeof(CustomerAttribute.Location.taxRegistrationID),
typeof(Customer.curyID),
typeof(CustomerAttribute.Contact.salutation),
typeof(Customer.customerClassID),
typeof(Customer.status)
},
Visibility = PXUIVisibility.SelectorVisible, DescriptionField = typeof(Customer.acctName), Filterable = true, TabOrder = 2)]
After publishing the customization, custom Demo Field should finally appear in the ARInvoice Customer selector:
To enable searching against a custom field inside the ARInvoice Customer selector, open Invoices and Memos screen in the Layout Editor and type UsrDemoField as the GridProperties.FastFilterFields property of the Customer selector:

using a viewmodel to assign values from a soap web service to a dropdownlist

I need some advice on getting values from a soap web service to display in a dropdownlist using a viewmodel, I currently receive the data for the various dropdownlists from a service class found in a service class library project (n-tier application).
The code for the dropdownlist service follows a similar format to the code below:
public IEnumerable<SelectListItem> getValuesAsSelectItems(string selected)
{
var items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Please Select", Value = string.Empty, Selected = (selected == string.Empty) });
foreach (var value in this.getValues())
{
items.Add(new SelectListItem { Text = value.Value, Value = value.Value, Selected = (selected == value.Value) });
}
return new SelectList(items, "Value", "Text");
}
I need a way of passing the values from this service to the viewmodel I have then created 7 controllers for each of the dropdownlist which will all link to partial views that I can reuse throughout the application, dropdownlists include titles, countries, states and others.
An approach you could take is to extract the drop down list values into a viewmodel of their own. So:
Step 1: Create a view model (ItemsViewModel) that encapsulates the drop down list items:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Models
{
public class DropDownListItem
{
public string Text { get; set; }
public string Value { get; set; }
}
public class ItemsViewModel
{
private readonly List<DropDownListItem> _items;
// The selected item:
public string SelectedItem { get; set; }
// The items:
public IEnumerable<SelectListItem> Items
{
get
{
var allItems = _items.Select(i => new SelectListItem
{
Value = i.Value,
Text = i.Text
});
return DefaultItem.Concat(allItems);
}
}
// Default item (i.e. the "select" text) if none selected
public IEnumerable<SelectListItem> DefaultItem
{
get
{
return Enumerable.Repeat(new SelectListItem
{
Value = "-1",
Text = "Select an item"
}, count: 1);
}
}
public ItemsViewModel()
{
}
// Constructor taking in items from service and selected item string:
public ItemsViewModel(List<DropDownListItem> items, string selected)
{
_items = items;
SelectedItem = selected;
}
}
}
Step 2: Create a partial view in the Views folder that binds to the ItemsViewModel:
#model Models.ItemsViewModel
#Html.DropDownListFor(m => m.SelectedItem, Model.Items)
Step 3: In the appropriate controller (e.g. HomeController), place the child action that pulls the data from the service, the view model and the partial view together:
[ChildActionOnly]
public ActionResult DropDownList(string type, string selected)
{
// If you need to call different services based on the type (e.g. Country), then pass through "type" and base the call on that
var items = new ItemsViewModel(
(from g in _service.getTitles() select new DropDownListItem { Text = g.Text, Value = g.Value }).ToList(),
selected);
return PartialView("DropDownPartial", items);
}
Step 4: Drop this line of code into the view where you need the drop down list:
#Html.Action("DropDownList", "Home", new { selected = "2", type = "country" })
Note that selected and type are to be determined whichever way you see fit and are optional.
Hopefully this gives you some inspiration.

How to create a Sitecore ribbon button with dropdown menu?

The Sitecore PageEditor and Preview interfaces feature a language selector button that triggers a "dropdown"/overlay menu where the user can select a language. How do I replicate this behavior?
(I set out to answer this question, and came up with a solution. Posting to SOF for comment/enhancement.)
You can see how Sitecore does it in the Sitecore.Client assembly, Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguage and Sitecore.Shell.Applications.WebEdit.Commands.SetLanguage.
You'll need to create two commands of your own for this. One command is associated with the button, one is executed when a subitem is selected. The example is based on a scenario of changing a country cookie.
ChangeCountry Command
First, the command to display the menu. You can see that it displays a Menu with dynamic options. Overriding GetHeader and GetIcon allows the button itself to be dynamic based on the user's current selection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Shell.Applications.WebEdit.Commands;
using Sitecore.Diagnostics;
using Sitecore.Data.Items;
using Sitecore.Web.UI.Sheer;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.StringExtensions;
using System.Web;
namespace Prototype.Commands
{
public class ChangeCountry : WebEditCommand
{
protected Dictionary<string, CountryOption> _countries = new Dictionary<string, CountryOption>
{
{"US", new CountryOption {
ID = "US",
Name = "United States",
Icon = "Flags/32x32/flag_usa.png"
}},
{"CA", new CountryOption {
ID = "CA",
Name = "Canada",
Icon = "Flags/32x32/flag_canada.png"
}},
{"MX", new CountryOption {
ID = "MX",
Name = "Mexico",
Icon = "Flags/32x32/flag_mexico.png"
}},
{"DE", new CountryOption {
ID = "DE",
Name = "Germany",
Icon = "Flags/32x32/flag_germany.png"
}}
};
public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
if (context.Items.Length == 1)
{
Item item = context.Items[0];
SheerResponse.DisableOutput();
Menu control = new Menu();
//replace with lookup and loop of available values
foreach (var key in _countries.Keys)
{
var country = _countries[key];
string id = country.ID;
string header = country.Name;
string icon = country.Icon;
string click = "prototype:setcountry(country={0})".FormatWith(key);
control.Add(id, header, icon, string.Empty, click, false, string.Empty, MenuItemType.Normal);
}
SheerResponse.EnableOutput();
SheerResponse.ShowPopup("ChangeCountryButton", "below", control);
}
}
public override string GetHeader(Sitecore.Shell.Framework.Commands.CommandContext context, string header)
{
HttpCookie country = HttpContext.Current.Request.Cookies["country"];
if (country != null && _countries.ContainsKey(country.Value))
{
return _countries[country.Value].Name;
}
return base.GetHeader(context, header);
}
public override string GetIcon(Sitecore.Shell.Framework.Commands.CommandContext context, string icon)
{
HttpCookie country = HttpContext.Current.Request.Cookies["country"];
if (country != null && _countries.ContainsKey(country.Value))
{
return _countries[country.Value].Icon;
}
return base.GetIcon(context, icon);
}
protected class CountryOption
{
public string ID { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
}
}
}
In either Commands.config or an include file, register the new command.
<command name="prototype:changecountry" type="Prototype.Commands.ChangeCountry,Prototype" />
Change Country Button
Create a new Chunk and Button under /sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Experience. This ribbon strip is referenced/replicated in Preview mode as well. The button will use the following properties:
The Click field must match the name of your command, and the ID field must match the element ID provided in the SheerResponse.ShowPopup call above.
SetCountry Command
Next is the command that will be called when an item in your menu/dropdown is selected.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Shell.Applications.WebEdit.Commands;
using System.Net;
using Sitecore.Diagnostics;
using Sitecore.Web.UI.Sheer;
using System.Web;
namespace Prototype.Commands
{
public class SetCountry : WebEditCommand
{
public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
var country = context.Parameters["country"];
Assert.IsNotNullOrEmpty(country, "Country not found");
HttpCookie cookie = new HttpCookie("country", country);
HttpContext.Current.Response.Cookies.Add(cookie);
WebEditCommand.Reload(WebEditCommand.GetUrl());
}
}
}
In our example, we're setting a cookie based on the selected value and reloading the page. The value passed in is based on the click event associated with the menu item in ChangeCountry. Likewise, the name of the command when configured needs to match what was used in the ChangeCountry click event.
<command name="prototype:setcountry" type="Prototype.Commands.SetCountry,Prototype" />