Cdd data to column series in wpf C# - c#

I want to create a ColumnSeries Bar Chart in WPF using C#. I shall extract the data from the database and want to bind it to the bar chart.
The data extracted will contain two values. First is parameter name (string) and the other is its value (double). Which type of collection shall I use? and how to do the binding?

i finally used a simple KeyValuePair array and assigned it to the ItemsSource property of the ColumnSeries of barchart.

Just use the Dictionary as follows:
Dictionary<string,int> data = new Dictionary<string,int> ();
If you have a data in dataset then use foreach loop for item in the dataset
Example:
foreach (DataRow drv in DS.Tables[0].Rows)
{
string strvalue= Convert.ToString(drv["columnname string type"]);
string intvalue= Convert.ToString(drv["column name int type"]);
data.Add(Convert.ToString(strvalue), Convert.ToInt32(intvalue));
}
((ColumnSeries)msChart3.Series[0]).ItemsSource = data;
This way you can bind data to a column series chart type.

Related

Dynamically add data to columns in GridView

I have the following code that dynamically creates columns within a WPF GridView control, the header names come from a string[] which is stored in a List<string[]> named data_org
GridView gv = tabell.View as GridView;
foreach (string s in data_org.ElementAt(0))
{
gv.Columns.Add(new GridViewColumn { Header = s });
}
Is there a way to add data while I'm creating the columns? I've searched for ways of doing it in the add column statement but can't find a way.
gv.Columns.Add(new GridViewColumn{Header = s, **statement to add data to column**});
My data is stored in another List<float[]>, where each item float[] represents a column. Do I have to do something to handle that data type (float[]), too?
You do not add data items directly to the columns of a GridView. Instead, you set the ItemsSource of the associated ListView, which is tabell in your case.
tabell.ItemsSource = /* Set a binding or assign an items collection. */;
Then you would create bindings for each column to the corresponding properties on your data items that should be displayed in the columns using DisplayMemberBinding.
var gridViewColumn = new GridViewColumn
{
Header = s,
DisplayMemberBinding = new Binding(/* Binding property path / name of the property. */);
};
As you only have a list of floats for each column, you should create a suitable data item type first. The ItemsSource expects a list of items that contain properties for each column, it represents a row.
What you have to do now is:
Create a row data type that contains properties for each column
public class MyDataItem
{
public float Number { get; }
// ...properties for other columns..
}
Create a collection of these data items with data from you float lists.
var myDataItemList = new List<MyDataItem>();
// ...create data items, add your data and add the items to the list.
Assign the list as items source of the ListView.
tabell.ItemsSource = myDataItemList;
Add display member bindings for each column.
var gridViewColumn = new GridViewColumn
{
Header = s,
DisplayMemberBinding = new Binding(nameof(MyDataItem.Number));
};
Then it should work. However, I recommend you to have a look at the MVVM design pattern.

display rows from two GridViews in single GridView

I have two GridViews on my .aspx page and both have different selectMethod, different data but having same HeaderTemplate. I want my .aspx will have only one gridView but different datasources. Is There any Possible way to accomplish this task.
Hello If You Have Multiple Data source You may use condition that which data source list you want to bind to your gridview
In this case you can use dynamic list to bind your data source dynamically at run time as your header field is same if your data field name is same then dynamic list will be perfect for you.
I am not sure that if you used BoundField or templatefield or directly binding the datasource
As you didnt post your code i am posting a sample code for you hope you can find out something through this
//Suppose YOur Lists are List1 and List2 then
If(Condition1==true)
{
List1 // Your datasource list
List<dynamic> data = new List<dynamic>();
foreach(var data in List1)
{
data.Add(new {
Name =data.Name,
Address = data.Address
});
}
GridView1.DataSource = data
GridView1.DataBind();
}
///For Condition 2
If(Condition2==true)
{
List2 // Your second datasource list
List<dynamic> data = new List<dynamic>();
foreach(var data in List2){
data.Add(new {
Name =data.Name,
Address = data.Address
});
}
GridView1.DataSource = data
GridView1.DataBind();
}
Create an intermediate variable to hold your merged data, then fetch your retrieved data into it (from both select methods you have), then assign this variable value to showing grid *.DataSource and then call the *.DataBind(); method.
P.S. Make sure when merging the two source to have the same schema, otherwise you will have to loop on every set of results to extract it and merge it manually with the other set to have your new standard schema which should be rendered in GridView then.

Create Dynamic properties from Datatable

I have below class with one property.
public class MfrYearEqpType
{
public string EqpType { get; set; }
public MfrYearEqpType()
{
}
}
I wanted to create dynamic properties(Datatable column names) to my class "MfrYearEqpType" and set the values from below Datatable.
DataTable dt = getData();
This table contains 26 rows and 10 columns of data.
I have gone through the below link. But I am not sure how to handle my case.
Dynamically adding properties to an Object from an existing static object in C#
And also I have used ExpandoObject. I have done below sample.
DataTable dt = getData();
List<dynamic> expandoList = new List<dynamic>();
foreach (DataRow row in dt.Rows)
{
//create a new ExpandoObject() at each row
var expandoDict = new ExpandoObject() as IDictionary<String, Object>;
foreach (DataColumn col in dt.Columns)
{
//put every column of this row into the new dictionary
expandoDict.Add(col.ToString(), row[col.ColumnName].ToString());
}
//add this "row" to the list
expandoList.Add(expandoDict);
}
But my aim is to create the List of MfrYearEqpType. So that I can bind List of MfrYearEqpType to Gridview.
Please help me on this.
Assuming you are talking about ASP.NET here, you can directly bind your data table to a GridView's DataSource property.
If you are happy with these 10 columns displaying as default, you can set AutoGenerateColumns property to true. Otherwise, you may want to define <asp:BoundField /> or <asp:TemplateField /> columns as appropriate. Bound fields will let you at least format the value for a particular column while template fields will allow full customization where you can provide an defining how to render a particular field.

C# WPF get listview value from multiple selected row

im using WPF in c#.
How to get all the value of the row selected from column M.
Image of listview
From ListView.SelectedItems Property,
foreach (var item in myListView.SelectedItems)
{
//do something with the values, maybe add to a list?
DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();
}
Basically replace "NameAndScore" by your column name.
Reference: Why I get “System.Data.DataRowView” instead of real values in my Listbox?

How to make a datagridview display the contents of an array?

I have a 1d array listOfValues. It contains double values. I want to display its values into a datagridview named as dataGridVal.
I did
dataGridVal.Source = listOfValues but its not working.
Did I miss something???
DataGridView binds its columns to properties of items in data source. Thus double type do not have any properties, you can't bind directly to that type.
HINT: you can project double values into anonymous objects:
dataGridVal.DataSource = listOfValues.Select(d => new { Value = d }).ToArray();

Categories

Resources