Telerik WinForms RadGridView create and fill in runtime - c#

My question is pretty similar for this
Fill RadGridView dynamically
but i dont want to create datatable
This is grid view
GridView = new RadGridView
{
AutoSizeRows = true,
BeginEditMode = RadGridViewBeginEditMode.BeginEditProgrammatically,
Location = new Point(0, 130),
Name = "GridView",
ShowRowErrors = false,
Size = new Size(390, 100),
TabIndex = 0,
Visible = false
};
This method creates columns in runtime
private List<GridViewTextBoxColumn> InitializeColumns()
{
var result = new List<GridViewTextBoxColumn>();
{
var columnId = new GridViewTextBoxColumn
{
Name = "RecordID",
HeaderText = "ID",
FieldName = "RecordID",
MaxLength = 50,
Width = 50,
DataType = typeof(int)
};
result.Add(columnId);
}
{
var columnInformation = new GridViewTextBoxColumn
{
Name = "RecordInformation",
HeaderText = "Information",
FieldName = "RecordInformation",
MaxLength = 50,
Width = 250,
DataType = typeof(string)
};
result.Add(columnInformation);
}
{
var columnRowType = new GridViewTextBoxColumn
{
Name = "RecordType",
HeaderText = "Type",
FieldName = "RecordType",
MaxLength = 50,
Width = 250,
DataType = typeof(string)
};
result.Add(columnRowType);
}
return result;
}
This class is for grid row
internal class OrderTripChangedAlertItem
{
public int RecordID { get; set; }
public string RecordInformation { get; set; }
public string RecordType { get; set; }
public OrderTripChangedAlertItem(int recordID, string recordInformation, string recordType)
{
RecordID = recordID;
RecordInformation = recordInformation;
RecordType = recordType;
}
}
So i bind the List to grid view. Columns are displayed fine, but not data shown. There must be one record. What i missed ?

Here is the whole code I placed on a Form and this works just fine:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
RadGridView GridView = new RadGridView
{
AutoSizeRows = true,
BeginEditMode = RadGridViewBeginEditMode.BeginEditProgrammatically,
Location = new Point(0, 130),
Name = "GridView",
ShowRowErrors = false,
Size = new Size(390, 100),
TabIndex = 0,
//Visible = false,
Parent = this
};
GridView.Columns.AddRange(InitializeColumns().ToArray());
List<OrderTripChangedAlertItem> list = new List<OrderTripChangedAlertItem>();
OrderTripChangedAlertItem item = new OrderTripChangedAlertItem(0, "some info", "some type");
list.Add(item);
GridView.DataSource = list;
}
private List<GridViewTextBoxColumn> InitializeColumns()
{
var result = new List<GridViewTextBoxColumn>();
{
var columnId = new GridViewTextBoxColumn
{
Name = "RecordID",
HeaderText = "ID",
FieldName = "RecordID",
MaxLength = 50,
Width = 50,
DataType = typeof(int)
};
result.Add(columnId);
}
{
var columnInformation = new GridViewTextBoxColumn
{
Name = "RecordInformation",
HeaderText = "Information",
FieldName = "RecordInformation",
MaxLength = 50,
Width = 250,
DataType = typeof(string)
};
result.Add(columnInformation);
}
{
var columnRowType = new GridViewTextBoxColumn
{
Name = "RecordType",
HeaderText = "Type",
FieldName = "RecordType",
MaxLength = 50,
Width = 250,
DataType = typeof(string)
};
result.Add(columnRowType);
}
return result;
}
internal class OrderTripChangedAlertItem
{
public int RecordID { get; set; }
public string RecordInformation { get; set; }
public string RecordType { get; set; }
public OrderTripChangedAlertItem(int recordID, string recordInformation, string recordType)
{
RecordID = recordID;
RecordInformation = recordInformation;
RecordType = recordType;
}
}

Related

Custom control c# for mark-up aligning controls to others

How Would I center my labels and my text boxes I want them like the uwp date time picker.
public class DateTimePicker2: ContentView, INotifyPropertyChanged
{
public Entry _entry { get; private set; } = new Entry()
{
WidthRequest = 300
};
public Entry _dayEntry { get; private set; } = new Entry()
{
Placeholder = "00", WidthRequest = 30
};
public Entry _monthEntry { get; private set; } = new Entry()
{
Placeholder = "00", WidthRequest = 30
};
public Entry _yearEntry { get; private set; } = new Entry()
{
Placeholder = "00", WidthRequest = 30
};
public Entry _hourEntry { get; private set; } = new Entry()
{
Placeholder = "00", WidthRequest = 30
};
public Entry _minsEntry {get;private set; } = new Entry()
{
Placeholder = "00", WidthRequest = 30
};
private static DateTime ? _defaultDateTime = DateTime.Now;
public DatePicker _datePicker { get; private set; } = new DatePicker()
{
MinimumDate = DateTime.Today, IsVisible = false
};
public TimePicker _timePicker { get; private set; } = new TimePicker()
{
IsVisible = false
};
public Label _dayLabel { get; private set; } = new Label()
{
Text = "DAY"
};
public Label _monthLabel { get; private set; } = new Label()
{
Text = "MONTH"
};
public Label _yearLabel { get; private set; } = new Label()
{
Text = "Year"
};
public Label _hourLabel { get; private set; } = new Label()
{
Text = "Hour"
};
public Label _minLabel { get; private set; } = new Label()
{
Text = "Min"
};
public Picker _ampmPicker { get; private set; } = new Picker()
{
ItemsSource = new List < string > { "AM", "PM" }
};
public Picker _twntyFourHourPicker { get; private set; } = new Picker()
{
ItemsSource = new List < string > { "12", "24" }
};
public DateTimePicker2()
{
BindingContext = this;
Content = new Frame()
{
BorderColor = Color.Black,
CornerRadius = 30,
Padding = new Thickness(10, 10, 10, 10),
Margin = new Thickness(20),
Content = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Children =
{
_dayLabel,
_dayEntry,
_monthLabel,
_monthEntry,
_yearLabel,
_yearEntry,
_hourLabel,
_hourEntry,
_minLabel,
_minsEntry,
}
}
};
}
}
Basically, at present it its showing as such I want the rounded corners only to be the width of the controls and the Labels to be cantered above the text boxes is that possible if it means making text boxes bigger am ok with that, but as long as the text is centered.
By the way, am using c# for mark-up to render the controls.
Using Xamarin.Forms Grid can make the data more regular.
For more information, please refer to https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/grid
I wrote a small example for your referenceļ¼š
Here is background code:
Grid grid = new Grid
{
RowDefinitions =
{
new RowDefinition { Height = 25 },
new RowDefinition { Height = 50 },
},
ColumnDefinitions =
{
new ColumnDefinition { Width = 65 },
new ColumnDefinition { Width = 65 },
new ColumnDefinition { Width = 65 },
new ColumnDefinition { Width = 65 },
new ColumnDefinition { Width = 65 }
}
};
grid.Children.Add(new Label { Text = "Day" }, 0, 0);
grid.Children.Add(new Label { Text = "Month" }, 1, 0);
grid.Children.Add(new Label { Text = "Year" }, 2, 0);
grid.Children.Add(new Label { Text = "Hour" }, 3, 0);
grid.Children.Add(new Label { Text = "Min" }, 4, 0);
grid.Children.Add(new Entry { Placeholder = "00" }, 0, 1);
grid.Children.Add(new Entry { Placeholder = "00" }, 1, 1);
grid.Children.Add(new Entry { Placeholder = "00" }, 2, 1);
grid.Children.Add(new Entry { Placeholder = "00" }, 3, 1);
grid.Children.Add(new Entry { Placeholder = "00" }, 4, 1);
StackLayout stackLayout = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
grid
}
};
Frame defaultFrame = new Frame
{
Content = stackLayout,
CornerRadius = 30,
Margin = new Thickness(20,20,20,20)
};
Content = new StackLayout
{
Children =
{
defaultFrame
}
};

How to bind list objects inside Models in Xamarin

I have the following problem, in which I'm trying to Data Bind a List inside a Model to my xaml file. I want to bind the list of IOSensors to the xaml file in some text labels.
Model:
class IOModule
{
public IOModule(string Name, string Type, string Version, string Location, string Desc)
{
this.Name = Name;
this.Type = Type;
this.Version = Version;
this.Serial = Location;
this.Status = Desc;
this.list = new List<IOSensor>();
}
public void addSensorInput(IOSensor sensor)
{
list.Add(sensor);
}
[JsonProperty("Name")]
public String Name { get; set; }
[JsonProperty("Type")]
public String Type { get; set; }
[JsonProperty("Version")]
public String Version { get; set; }
[JsonProperty("Serial")]
public String Serial { get; set; }
[JsonProperty("Status")]
public String Status { get; set; }
public List<IOSensor> list { get; }
public List<String> getIDs
{
get
{
List<string> list1 = new List<string>();
foreach (IOSensor sens in list)
{
list1.Add(sens.ID);
}
return list1;
}
}
}
public class IOSensor
{
public IOSensor(String ID, String Type, String GasComp, String MeasureType, String value)
{
this.ID = ID;
this.IOType = Type;
this.GasComp = GasComp;
this.MeasurementType = MeasureType;
this.Value = value;
}
[JsonProperty("ID")]
public String ID { get; set; }
[JsonProperty("IOType")]
public String IOType { get; set; }
[JsonProperty("GasComp")]
public String GasComp { get; set; }
[JsonProperty("MeasurementType")]
public String MeasurementType { get; set; }
[JsonProperty("Value")]
public String Value { get; set; }
}
ViewModel:
class IoModulesViewModels : BaseViewModel
{
public Item item;
public ObservableCollection<IOModule> modlists;
public IoModulesViewModels(Item item)
{
this.item = item;
this.modlists= new ObservableCollection<IOModule>();
//sendReq();
IOModule mod1 = new IOModule("1", "2", "3", "4", "5");
mod1.addSensorInput(new IOSensor("44", "55", "66", "77", "88"));
mod1.addSensorInput(new IOSensor("444", "545", "646", "747", "848"));
modlists.Add(mod1);
modlists.Add(mod1);
}
}
Xaml:
public partial class IoModulesPage : ContentPage
{
IoModulesViewModels ioModModels;
public IoModulesPage(Item item)
{
InitializeComponent();
BindingContext = this.ioModModels = new IoModulesViewModels(item);
Label header = new Label
{
Text = "ListView",
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
};
ListView lw = new ListView
{
ItemsSource = ioModModels.modlists,
HasUnevenRows = true,
ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid();
Label modHeaderName = new Label();
modHeaderName.SetBinding(Label.TextProperty, "Name");
Label modHeaderType = new Label();
modHeaderType.SetBinding(Label.TextProperty, "Type");
Label modHeaderVersion = new Label();
modHeaderVersion.SetBinding(Label.TextProperty, "Version");
Label modHeaderSerial = new Label();
modHeaderSerial.SetBinding(Label.TextProperty, "Serial");
Label modHeaderStatus = new Label();
modHeaderStatus.SetBinding(Label.TextProperty, "Status");
grid.Children.Add(modHeaderName);
grid.Children.Add(modHeaderType, 1, 0);
grid.Children.Add(modHeaderVersion, 2, 0);
grid.Children.Add(modHeaderSerial, 3, 0);
grid.Children.Add(modHeaderStatus, 4, 0);
ListView lp = new ListView
{
ItemsSource = ioModModels.modlists,
HasUnevenRows = true,
ItemTemplate = new DataTemplate(() =>
{
Label header2 = new Label
{
Text = "ID,Type,GasComp,MeasurementType,Value",
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
HorizontalOptions = LayoutOptions.Fill
};
Grid gridIn = new Grid();
Label modID = new Label();
modID.SetBinding(Label.TextProperty, "getIDs");
Label modIOType = new Label();
modIOType.SetBinding(Label.TextProperty, "list.IOType");
Label modGasComp = new Label();
modGasComp.SetBinding(Label.TextProperty, "GasComp");
Label modMeasurementType = new Label();
modMeasurementType.SetBinding(Label.TextProperty, "list[0].MeasurementType");
Label modValue = new Label();
modValue.SetBinding(Label.TextProperty, "list[0].Value");
gridIn.Children.Add(modID);
gridIn.Children.Add(modIOType, 1, 0);
gridIn.Children.Add(modGasComp, 2, 0);
gridIn.Children.Add(modMeasurementType, 3, 0);
gridIn.Children.Add(modValue, 4, 0);
gridIn.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
gridIn.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
return new ViewCell
{
View = new StackLayout
{
Padding = new Thickness(0, 10),
Orientation = StackOrientation.Vertical,
Children =
{
header2,
gridIn
}
}
};
})
};
Frame frame = new Frame()
{
BorderColor=Color.Gray,
CornerRadius = 5,
Padding = 8,
Content = new StackLayout
{
Orientation=StackOrientation.Horizontal,
Children =
{
grid,
lp
}
}
};
return new ViewCell
{
View = new StackLayout
{
Padding = new Thickness(0, 10),
Spacing = 2,
Orientation = StackOrientation.Horizontal,
Children =
{
frame
}
}
};
}
)
};
// Accomodate iPhone status bar.
this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
// Build the page.
this.Content = new StackLayout
{
Children =
{
header,
lw
}
};
}
}
As you can see in the xaml file, I ve been trying many apporaches in which i can bind the list of IOSensors, but so far no luck. The Page is loading and showing some results (for example the Syntax with "list[0].property" works but i want it to work for every entry of the list.
Thanks ina dvance
As Jason said that you want to display a nested list of data, so I suggest you can use ListView Group to display data.
I modify your code, and creating one sample, that you can take a look:
Two models:
public class IOModule:List<IOSensor>
{
[JsonProperty("Name")]
public String Name { get; set; }
[JsonProperty("Type")]
public String Type { get; set; }
[JsonProperty("Version")]
public String Version { get; set; }
[JsonProperty("Serial")]
public String Serial { get; set; }
[JsonProperty("Status")]
public String Status { get; set; }
public List<IOSensor> list => this;
}
public class IOSensor
{
[JsonProperty("ID")]
public String ID { get; set; }
[JsonProperty("IOType")]
public String IOType { get; set; }
[JsonProperty("GasComp")]
public String GasComp { get; set; }
[JsonProperty("MeasurementType")]
public String MeasurementType { get; set; }
[JsonProperty("Value")]
public String Value { get; set; }
}
public partial class Page8 : ContentPage
{
public ObservableCollection<IOModule> modlists { get; set; }
public Page8()
{
InitializeComponent();
modlists = new ObservableCollection<IOModule>();
IOModule mod1 = new IOModule()
{
new IOSensor(){ID="IOSensor 1",IOType="",GasComp="",MeasurementType="",Value=""},
new IOSensor(){ID="IOSensor 2",IOType="",GasComp="",MeasurementType="",Value=""}
};
mod1.Name = "IOModule 1";
modlists.Add(mod1);
IOModule mod2 = new IOModule()
{
new IOSensor(){ID="IOSensor 3",IOType="",GasComp="",MeasurementType="",Value=""},
new IOSensor(){ID="IOSensor 4",IOType="",GasComp="",MeasurementType="",Value=""}
};
mod2.Name = "IOModule 2";
modlists.Add(mod2);
IOModule mod3 = new IOModule()
{
new IOSensor(){ID="IOSensor 5",IOType="",GasComp="",MeasurementType="",Value=""},
new IOSensor(){ID="IOSensor 6",IOType="",GasComp="",MeasurementType="",Value=""}
};
mod3.Name = "IOModule 3";
modlists.Add(mod3);
//this.BindingContext = this;
Label header = new Label
{
Text = "ListView",
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
};
ListView lw = new ListView();
lw.ItemsSource = modlists;
lw.IsGroupingEnabled = true;
lw.ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid();
Label modHeaderName = new Label();
modHeaderName.SetBinding(Label.TextProperty, "ID");
Label modHeaderType = new Label();
modHeaderType.SetBinding(Label.TextProperty, "IOType");
Label modHeaderVersion = new Label();
modHeaderVersion.SetBinding(Label.TextProperty, "GasComp");
Label modHeaderSerial = new Label();
modHeaderSerial.SetBinding(Label.TextProperty, "MeasurementType");
Label modHeaderStatus = new Label();
modHeaderStatus.SetBinding(Label.TextProperty, "Value");
grid.Children.Add(modHeaderName);
grid.Children.Add(modHeaderType, 1, 0);
grid.Children.Add(modHeaderVersion, 2, 0);
grid.Children.Add(modHeaderSerial, 3, 0);
grid.Children.Add(modHeaderStatus, 4, 0);
return new ViewCell { View = grid };
});
lw.GroupHeaderTemplate = new DataTemplate(() =>
{
var grid = new Grid();
Label modHeaderName = new Label() {
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
FontAttributes = FontAttributes.Bold,
};
modHeaderName.SetBinding(Label.TextProperty, "Name");
//Label modHeaderType = new Label();
//modHeaderType.SetBinding(Label.TextProperty, "Type");
//Label modHeaderVersion = new Label();
//modHeaderVersion.SetBinding(Label.TextProperty, "Version");
//Label modHeaderSerial = new Label();
//modHeaderSerial.SetBinding(Label.TextProperty, "Serial");
//Label modHeaderStatus = new Label();
//modHeaderStatus.SetBinding(Label.TextProperty, "Status");
grid.Children.Add(modHeaderName);
//grid.Children.Add(modHeaderType, 1, 0);
//grid.Children.Add(modHeaderVersion, 2, 0);
//grid.Children.Add(modHeaderSerial, 3, 0);
//grid.Children.Add(modHeaderStatus, 4, 0);
return new ViewCell { View = grid };
});
this.Content = new StackLayout
{
Children =
{
header,
lw
}
};
}
}
This is screenshot;

Can I AutoGenerateColumns inside a DataGrid, based on a list inside ObservableCollection?

I'm attempting to create a dynamic log viewer. I currently have three classes:
public class SystemLogEntry
{
public ObservableCollection<SystemLogRowEntity> LogEntries { get; set; }
public SystemLogEntry(){LogEntries = new ObservableCollection<SystemLogRowEntity>();}
}
public class SystemLogRowEntity
{
public int Id { get; set; }
public List<LogDetailsEntity> Name { get; set; }
}
public class LogDetailsEntity
{
public string Header { get; set; }
public string Details { get; set; }
}
My DataGrid Code:
<DataGrid x:Name="MainDataGrid" AutoGenerateColumns="True"/>
I also have some initialization code:
private SystemLogEntry systemLog = new SystemLogEntry();
systemLog.LogEntries.Add(new SystemLogRowEntity
{
Id = 1,
Name = new List<LogDetailsEntity>
{
new LogDetailsEntity {Header = "Name", Details = "SQL Report"},
new LogDetailsEntity {Header = "Type", Details = "Message"},
new LogDetailsEntity {Header = "Time", Details = "15:00"},
new LogDetailsEntity {Header = "Date", Details = "01/01/2019"},
new LogDetailsEntity {Header = "Details", Details = "The SQL Report completed successfully."}
}
});
systemLog.LogEntries.Add(new SystemLogRowEntity
{
Id = 2,
Name = new List<LogDetailsEntity>
{
new LogDetailsEntity {Header = "Name", Details = "SQL Report"},
new LogDetailsEntity {Header = "Type", Details = "Error"},
new LogDetailsEntity {Header = "Time", Details = "15:01"},
new LogDetailsEntity {Header = "Date", Details = "01/01/2019"},
new LogDetailsEntity {Header = "Details", Details = "The SQL Report failed catastrophically."},
new LogDetailsEntity {Header = "Thread", Details = "10250"},
new LogDetailsEntity {Header = "User", Details = "System"}
}
});
MainDataGrid.AutoGenerateColumns = true;
MainDataGrid.DataContext = systemLog;
MainDataGrid.SetBinding(ItemsControl.ItemsSourceProperty,
new Binding
{
Source = systemLog.LogEntries,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
I would like each item inside LogDetailsEntity to appear as a column header (with associated "details" as the content). However I always just receive "collection" on my DataGrid output.
Thanks to the comment from #EdPlunkett, I solved this using a DataTable as the ItemsSource.
DataTable logDataTable = new DataTable();
for (var index = 0; index < systemLog.LogEntries.Count; index++)
{
var entry = systemLog.LogEntries[index];
foreach (var logDetailsEntity in entry.Name)
{
if (logDataTable.Columns.Contains(logDetailsEntity.Header)) continue;
logDataTable.Columns.Add(logDetailsEntity.Header);
}
logDataTable.Rows.Add();
foreach (var logDetailsEntity in entry.Name)
{
logDataTable.Rows[index][logDetailsEntity.Header] = logDetailsEntity.Details;
}
}
MainDataGrid.ItemsSource = logDataTable.DefaultView;

How to covert this to for loop array c#.net

I am trying to create a dynamic control based in MVC,
I got a solution to implement.. it working fine..
with this code
public class DynamicControlViewModel
{
public ControlViewModel[] Controls { get; set; }
}
public abstract class ControlViewModel
{
public abstract string Type { get; }
public bool Visible { get; set; }
public string Label { get; set; }
public string Name { get; set; }
}
public class TextBoxViewModel : ControlViewModel
{
public override string Type
{
get { return "textbox"; }
}
public string Value { get; set; }
}
public class CheckBoxViewModel : ControlViewModel
{
public override string Type
{
get { return "checkbox"; }
}
public bool Value { get; set; }
}
public class DropDownListViewModel : TextBoxViewModel
{
public override string Type
{
get { return "ddl"; }
}
public SelectList Values { get; set; }
}
This is the way i am calling the object
public ActionResult Index()
{
return View(GetControls1());
}
public DynamicControlViewModel GetControls1()
{
var model1 = new DynamicControlViewModel
{
Controls = new ControlViewModel[] {
new DropDownListViewModel{Visible = true,Label = "drop label",Name = "DropDown1",Values = new SelectList(new[]{new { Value = "1", Text = "text 1" },new { Value = "2", Text = "text 2" },new { Value = "3", Text = "text 3" },}, "Value", "Text", "2")},
new TextBoxViewModel { Visible = true, Label = "label 1", Name = "TextBox1", Value = "value of textbox" },
new CheckBoxViewModel { Visible = true, Label = "CheckBox label 1", Name = "CheckBox1", Value = true},
new TextBoxViewModel { Visible = true, Label = "label 2", Name = "TextBox2", Value = "value of textbox" }}
};
return model1;
}
Here,
I need to use for loop to create the dynamic control..
here is my code, getting error
public DynamicControlViewModel GetControls()
{
var model = new DynamicControlViewModel { };
var Controls1 = new ControlViewModel[] { };
var s = model.Controls;
//int i=0;
//for loop start
Controls1[0] = new TextBoxViewModel { Visible = true, Label = "label 2", Name = "TextBox2", Value = "value of textbox" };
Controls1[1] = new TextBoxViewModel { Visible = true, Label = "label 3", Name = "TextBox3", Value = "value of textbox" };
Controls1[2] = new CheckBoxViewModel { Visible = true, Label = "CheckBox label 1", Name = "CheckBox1", Value = true },
Controls1[3] = new DropDownListViewModel { Visible = true, Label = "drop label", Name = "DropDown1", Values = new SelectList(new[] { new { Value = "1", Text = "text 1" }, new { Value = "2", Text = "text 2" }, new { Value = "3", Text = "text 3" }, }, "Value", "Text", "2") },
//loop end
var model1 = new DynamicControlViewModel
{
Controls = Controls1
};
return model1;
}
The problem is that you first define an array of size 0 (i.e. with 0 cells to hold values):
var Controls1 = new ControlViewModel[] { }; // array with length 0
Afterwards you try to assign to the first, second, third and fourth cell of the array, which do not exist. Therefore you get the error.
So you should either use a List<ControlViewModel> or initialize an array of the correct length:
var Controls1 = new ControlViewModel[4];

Pie Charts issue use in controller

I need to draw a pie chart with HighCharts. I did this in my controller:
class SimpleClass
{
public string x { set; get; }
public int y { set; get; }
}
public ActionResult Index()
{
var multas = from multa in db.Multa select multa;
int MultaSinPago = multas.Where(s => s.Estado == "sin pago").Count();
int MultaPagado = multas.Where(s => s.Estado == "Pagado").Count();
var results = new List<SimpleClass>();
results.Add(new SimpleClass { x = "Sin Pago", y = MultaSinPago });
results.Add(new SimpleClass { x = "Pagado", y = MultaPagado });
//Create chart Model
var chart1 = new Highcharts("Chart1");
chart1
.InitChart(new Chart() { DefaultSeriesType = ChartTypes.Pie })
.SetTitle(new Title() { Text = "Multas" })
.SetSeries(new[] { new Series { Data = new List<SimpleClass>(results) } });
//pass Chart1Model using ViewBag
ViewBag.Chart1Model = chart1;
return View();
}
But where Data = new List<SimpleClass>(results) is bad, how can i do for it?
I found solution!..
var multas = from multa in db.Multa select multa;
int MultaSinPago = multas.Where(s => s.Estado == "sin pago").Count();
int MultaPagado = multas.Where(s => s.Estado == "Pagado").Count();
TempData["CountMulta"] = (MultaPagado + MultaSinPago).ToString();
Highcharts chart = new Highcharts("chart")
.SetTitle(new Title() { Text = "Multas"})
.InitChart(new Chart())
.SetPlotOptions(new PlotOptions
{
Pie = new PlotOptionsPie
{
AllowPointSelect = true,
Cursor = Cursors.Pointer,
ShowInLegend = true,
Events = new PlotOptionsPieEvents { Click = "function(event) { alert('The slice was clicked!'); }" },
Point = new PlotOptionsPiePoint { Events = new PlotOptionsPiePointEvents { LegendItemClick = "function(event) { if (!confirm('Do you want to toggle the visibility of this slice?')) { return false; } }" } }
}
})
.SetSeries(new Series
{
Type = ChartTypes.Pie,
Name = "Cantidad",
Data = new Data(new object[]
{
new object[] { "Pagado", MultaPagado},
new object[] { "Sin pago", MultaSinPago}
})
});
ViewBag.Chart1Model = chart;
return View();
Hope this is helpful for somebody! :)

Categories

Resources