Ajax Pie Chart C# modify properties - c#

Here is an image of my pie chart now
Here is the c# code for it..
string query = string.Format("select TestName,Count (TestName) AS Counts from VExecutionGlobalHistory where Tester <> 'dit2988' AND TestTypeID = 1 group by TestName", ddlTests.SelectedItem.Value);
DataTable dt = GetData(query);
foreach (DataRow row in dt.Rows)
{
SpecificTestsWeb6.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = row["TestName"].ToString(),
Data = Convert.ToDecimal(row["Counts"]),
}
);
}
SpecificTestsWeb6.ChartTitle = "Specific Tests Run";
I need to align the bottom values to the left so i can see them all. I would also like to be able to manually asign colors so there are no repeats. Thanks.

var random = new Random();
foreach (DataRow row in dt.Rows)
{
var color = String.Format("#{0:X6}", random.Next(0x1000000));
SpecificTestsRapp.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = row["TestName"].ToString(),
Data = Convert.ToDecimal(row["Counts"]),
PieChartValueColor = color
});
}
SpecificTestsRapp.ChartTitle = "Specific Tests Run";

Related

how to shuffle a datagridview?

I have a datagridview with values that I add manualy with a datasource, however I want to add a button where the user can shuffle the order of where the values are located in the datagridview. Does anyone have an idead on how to apply this?
if it comes from sql statement with
SELECT * FROM table
ORDER BY NEWID()
but if it is a different form of datasource
var data = //un randomed data
var randomData = data.OrderBy(x => Guid.NewGuid()).ToList();
//then bind randomData to your datasource
so on button click, update it by
getData(){
//use either sql statement or OrderBy() then bind
}
Add a hidden column that contains a random number and sort the rows using that.
If you set AutoGenerateColumns to false you can specify the order of the columns.
void ShuffleDataGridView(DataGridView dataGridView, DataTable dataTable)
{
// add or update random sort order column
const string randomSortConst = "RandomSort";
if (!dataTable.Columns.Contains(randomSortConst))
{
dataTable.Columns.Add(randomSortConst, typeof(int));
}
var rand = new Random();
foreach (DataRow drw in dataTable.Rows)
{
drw[randomSortConst] = rand.Next();
}
// randomize column display
dataGridView.AutoGenerateColumns = false;
dataGridView.AllowUserToOrderColumns = false;
dataGridView.Columns.Clear();
var columnsToAdd = new List<DataColumn>();
foreach (DataColumn dcl in dataTable.Columns)
{
if (!(dcl.ColumnName == randomSortConst))
{
columnsToAdd.Add(dcl);
}
}
while (columnsToAdd.Count > 0)
{
var j = rand.Next(0, columnsToAdd.Count - 1);
var dgvtbc = new DataGridViewTextBoxColumn
{
DataPropertyName = columnsToAdd[j].ColumnName,
HeaderText = columnsToAdd[j].ColumnName // remove this line to hide column headings
};
dataGridView.Columns.Add(dgvtbc);
columnsToAdd.RemoveAt(j);
}
// sort the rows using the hidden random column
dataGridView.DataSource = new DataView(dataTable, string.Empty, randomSortConst, DataViewRowState.CurrentRows);
}

I need helping adding two different rows(with same columns) to the same datatable

Working on a windows form application that reads in data from csv files and adds the data to a Datagridview. I ran into an issue with all of the rows being added to the datable and being displayed on the datagridview. The datagridview displays the datarows from the first two if conditions and OneRow if condition only. It will not add the rows from the twoRow if condition if the datable and datagridview rows are populated with the OneRow if condition rows. But i want the rows from both OneRow and TwoRow to be displyed. Also the rows from TwoRow do populate the datatable and datagridview when I comment(/**/) out the OneRow if condition. But I need both to populate the table. Thanks in advance!
Construct.MainDataTable.Columns.Add("Date", typeof(DateTime));
Construct.MainDataTable.Columns.Add("Time");
Construct.MainDataTable.Columns.Add("Serial");
Construct.MainDataTable.Columns.Add("Type");
Construct.MainDataTable.Columns.Add("level");
Construct.MainDataTable.Columns.Add("price");
Construct.MainDataTable.Columns.Add(" Limit");
Construct.MainDataTable.Columns.Add("last Limit");
Construct.MainDataTable.Columns.Add("Data");
..........................
...............................................
DataRow oneRow = Construct.MainDataTable.NewRow();
DataRow twoRow = Construct.MainDataTable.NewRow();
dataGridView2.AllowUserToAddRows = false;
if (line.Split(',')[2].Equals("Time"))
{
time = line.Split(',')[3];
date = line.Split(',')[1];
}
if (line.Split(',')[2].Equals("Level"))
{
level = line.Split(',')[3];
}
//OneROw(IF condition)
if ((Convert.ToDecimal(line.Split(',')[8])) < (Convert.ToDecimal (line.Split(',')[12])))
{
type = line.Split(',')[1];
serial = line.Split(',')[7];
price = line.Split(',')[3];
Limit = line.Split(',')[8];
lastLimit = line.Split(',')[10];
Data = line.Split(',')[12];
oneRow["Date"] = date;
oneRow["Time"] = time;
oneRow["Serial"] = serial;
oneRow["Type"] = type;
oneRow["level"] = level;
oneRow["price"] = price;
oneRow[" Limit"] = Limit;
oneRow["last Limit"] = lastlimit;
oneRow["Data"] = Data;
Construct.MainDataTable.Rows.Add(oneRow);
}
//TwoROw(IF condition)
if ((line.Contains('"')) && ((line.Contains("NG"))))
{
price = line.Split(',')[3];
type = line.Split(',')[1];
serial = line.Split(',')[7];
Limit = line.Split('"')[7];
var valLimit = Limit.Split(',').Select(a => Convert.ToInt32(a, 16));
var limitJoin = String.Join(",", valLimit);
lastlimit = line.Split('"')[1];
var vallastLimit = lastlimit.Split(',').Select(d => Convert.ToInt32(d, 16));
var lastJoin = String.Join(",", vallastLimit);
Data = line.Split('"')[5];
var valDatas = Data.Split(',').Select(s => Convert.ToInt32(s, 16));
var dataJoin = String.Join(",", valDatas);
twoRow["Date"] = date;
twoRow["Time"] = time;
twoRow["Serial"] = serial;
twoRow["Type"] = type;
twoRow["level"] = level;
twoRow["price"] = price;
twoRow["Limit"] = limitJoin;
twoRow["last Limit"] = lastJoin;
twoRow["Data"] = dataJoin;
Construct.MainDataTable.Rows.Add(twoRow);
}
dataGridView2.DataSource = Construct.MainDataTable;
Can't add a comment because I don't have enough karma so I ask my questions here: So, if I understood your problem you can't add data from one .csv file if it have more then one row? Why are you using 2 different if conditions for row in .csv file?
If you have empty data in row never mind you can still place them to your DataTable column, so you can use loop to add data from .csv to your DataTable. Try some thing like this:
public static DataTable CsvToDataTable(string csv)
{
DataTable dt = new DataTable();
string[] lines = csv.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
Regex onlyDeimiterComma = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
for (int i = 0; i < lines.Length; i++)
{
DataRow row = dt.NewRow();
string[] cells = onlyDeimiterComma.Split(lines[i]);
for (int j = 0; j < cells.Length; j++)
{
if (i == 0)
{
if (j == 0)
{
dt.Columns.Add(cells[j], typeof(DateTime));
}
else
{
dt.Columns.Add(cells[j]);
}
}
else
{
row[j] = cells[j];
}
}
dt.Rows.Add(row);
}
return dt;
}
Just call this method anywhere in your code and give it string read from your .csv file.
You can try to compile this code here and see how it works on .csv data with different data (empty columns, quoted text, quoted commas)
UPD: If you need to fill DataTable from two different .csv files you can still use code above. Just call it twice for both files and then Merge two DataTable, like this:
DataTable dt = CsvToDataTable(csvFileOne);
DataTable dtTwo = CsvToDataTable(csvFileTwo);
dt.Merge(dtTwo);

Merge two datatable to one based on column

this question has been asked but mine has a different approach link 1
l have a datatable with the following data
DataTable dtProduct = new DataTable();
dtProduct.Columns.Add("productId");
dtProduct.Columns.Add("name");
DataRow dataRow = dtProduct.NewRow();
dataRow["productId"] = 1;
dataRow["name"] = "Burger";
dtProduct.Rows.Add(dataRow);
DataRow dataRow2 = dtProduct.NewRow();
dataRow2["productId"] = 2;
dataRow2["name"] = "Chicken";
dtProduct.Rows.Add(dataRow2);
DataTable dtSales = new DataTable();
dtSales.Columns.Add("productId");
dtSales.Columns.Add("saleId");
dtSales.Columns.Add("month");
dtSales.Columns.Add("quantity");
DataRow dataSalesRow = dtSales.NewRow();
dataSalesRow["productId"] = 1;
dataSalesRow["saleId"] = "1";
dataSalesRow["month"] = "Jan";
dataSalesRow["quantity"] = 3433;
dtSales.Rows.Add(dataSalesRow);
DataRow drSales2 = dtSales.NewRow();
drSales2["productId"] = 1;
drSales2["saleId"] = "2";
drSales2["month"] = "Feb";
drSales2["quantity"] = 56;
dtSales.Rows.Add(drSales2);
DataRow drSales3 = dtSales.NewRow();
drSales3["productId"] = 1;
drSales3["saleId"] = "3";
drSales3["month"] = "Mar";
drSales3["quantity"] = 34522;
dtSales.Rows.Add(drSales3);
DataRow drSales4 = dtSales.NewRow();
drSales4["productId"] = 2;
drSales4["saleId"] = "4";
drSales4["month"] = "Feb";
drSales4["quantity"] = 345;
dtSales.Rows.Add(drSales4);
And another sample 2
DataTable dtStudents = new DataTable();
dtStudents.Columns.Add("studentId");
dtStudents.Columns.Add("fullname");
DataRow drStudentrow = dtStudents.NewRow();
drStudentrow["studentId"] = 1;
drStudentrow["fullname"] = "Bil";
dtStudents.Rows.Add(drStudentrow);
DataRow drStudentrow2 = dtStudents.NewRow();
drStudentrow2["studentId"] = 2;
drStudentrow2["fullname"] = "Paul";
dtStudents.Rows.Add(drStudentrow2);
DataTable dtStudentExam = new DataTable();
dtStudentExam.Columns.Add("studentId");
dtStudentExam.Columns.Add("subjectId");
dtStudentExam.Columns.Add("mark");
DataRow dataStudentExamRow = dtStudentExam.NewRow();
dataStudentExamRow["studentId"] = 1;
dataStudentExamRow["subjectId"] = "E123";
dataStudentExamRow["mark"] = 34;
dtStudentExam.Rows.Add(dataStudentExamRow);
DataRow dataStudentExamRow2 = dtStudentExam.NewRow();
dataStudentExamRow2["studentId"] = 2;
dataStudentExamRow2["subjectId"] = "E123";
dataStudentExamRow2["mark"] = 90;
dtStudentExam.Rows.Add(dataStudentExamRow2);
DataRow dataStudentExamRow3 = dtStudentExam.NewRow();
dataStudentExamRow3["studentId"] = 1;
dataStudentExamRow3["subjectId"] = "E155";
dataStudentExamRow3["mark"] = 78;
dtStudentExam.Rows.Add(dataStudentExamRow3);
DataRow dataStudentExamRow4 = dtStudentExam.NewRow();
dataStudentExamRow4["studentId"] = 1;
dataStudentExamRow4["subjectId"] = "E101";
dataStudentExamRow4["mark"] = 12;
dtStudentExam.Rows.Add(dataStudentExamRow4);
DataRow dataStudentExamRow5 = dtStudentExam.NewRow();
dataStudentExamRow5["studentId"] = 1;
dataStudentExamRow5["subjectId"] = "E234";
dataStudentExamRow5["mark"] = 42;
dtStudentExam.Rows.Add(dataStudentExamRow5);
DataTable products(dtProduct ) has a productId which is the key and another data table sales (dtSales) has sales for the product based on month . and has a column productId .
what l want to achieve is merge this two tables to this
Same Applies for students
l have a data table student (dtStudents) but l however l have student exam marks data tables (dtStudentExam) for each subject
and combine to something like this
So my question is can l do this dynamically . l tried the following below
public static System.Data.DataTable MergeRowsToColumns(DataTable rowDataTable, string rowDataTableIdColumnName, string friendlyName, DataTable columData)
{
List<string> columnsToAdd = new List<string>();
if (rowDataTable == null)
{
return null;
}
DataTable finalDataTable = new DataTable();
finalDataTable.Columns.Add(friendlyName.ToLower());
finalDataTable.Columns.Add(rowDataTableIdColumnName.ToLower());
foreach (DataColumn column in columData.Columns)
{
if (column.ColumnName.ToString() == rowDataTableIdColumnName.ToString())
{
continue;
}
else
{
finalDataTable.Columns.Add(column.ColumnName.ToString());
columnsToAdd.Add(column.ColumnName.ToString());
}
}
foreach (DataRow row in rowDataTable.Rows)
{
DataRow newRow = finalDataTable.NewRow();
newRow[rowDataTableIdColumnName] = row[rowDataTableIdColumnName];
newRow[friendlyName] = row[friendlyName];
foreach (DataRow columnRows in columData.Rows)
{
foreach (string column in columnsToAdd)
{
var value = columnRows[column].ToString();
newRow[column] = value;
}
}
finalDataTable.Rows.Add(newRow);
}
return finalDataTable;
}
Algorithm logic
Table 1 holds the key information
Table 2 holds the value data for table
using a column name from table one for id which will be the same on table 2
can l do it generically
in one function like
var dtMergedProducts = HelperDataTable.MergeRowsToColumns(dtProduct, "productId", "name", dtSales);
var dtStudents = HelperDataTable.MergeRowsToColumns(dtStudents, "studentId", "fullname", dtStudentExam);
Please am retrieving the data as is hence can not group by in table 2 and also table two does not have a friendly name that will be displayed.
Most of the data that is loaded is not more than 100 000
this is the link of the helper utility am making git hub
Edit One
DataTable 2 will have column value one for column and the other for value
For example
dtSales
1- month : shown vertically
2- quantity: will be cell value
Sample 2
dtStudentExam
1- subjectId : shown vertically
2- mark : will be the student mark
l got it working this morning
here if the method
public static System.Data.DataTable MergeRowsToColumns(DataTable rowDataTable, string rowDataTableIdColumnName, string friendlyName, DataTable dataAllValue , string dataColumnValue , string dataColumnKey)
{
List<string> columnsToAdd = new List<string>();
if (rowDataTable == null)
{
return null;
}
DataTable finalDataTable = new DataTable();
finalDataTable.Columns.Add(friendlyName.ToLower());
finalDataTable.Columns.Add(rowDataTableIdColumnName.ToLower());
foreach (DataRow row in dataAllValue.Rows)
{
if (row[rowDataTableIdColumnName].ToString() == rowDataTableIdColumnName.ToString())
{
continue;
}
else
{
var isExistColumnValue = columnsToAdd.Where(c => c == row[dataColumnKey].ToString()).FirstOrDefault();
if(isExistColumnValue == null)
{
columnsToAdd.Add(row[dataColumnKey].ToString());
finalDataTable.Columns.Add(row[dataColumnKey].ToString());
}
}
}
foreach (DataRow row in rowDataTable.Rows)
{
DataRow newRow = finalDataTable.NewRow();
newRow[rowDataTableIdColumnName] = row[rowDataTableIdColumnName];
newRow[friendlyName] = row[friendlyName];
foreach (DataRow columnRows in dataAllValue.Rows)
{
if(row[rowDataTableIdColumnName].ToString() != columnRows[rowDataTableIdColumnName].ToString())
{
continue;
}
var columnName = columnRows[dataColumnKey].ToString();
var columnValue = columnRows[dataColumnValue].ToString();
newRow[columnName] = columnValue;
}
finalDataTable.Rows.Add(newRow);
}
return finalDataTable;
}
it looked like l was failing on adding the column names before merging
you can just call like
var dt = HelperDataTable.MergeRowsToColumns(dtProduct, "productId", "name", dtSales , "quantity", "month");
var dt2 = HelperDataTable.MergeRowsToColumns(dtStudents, "studentId", "fullname", dtStudentExam , "mark", "subjectId");

Adding legend to winforms point chart

I am building a winform point chart using C# where the data comes from a datatable. I've created the points like this:
chart1.Series.Add("series1");
chart1.Series["series1"].ChartType = SeriesChartType.Point;
chart1.Series["series1"].YValueMembers = "VALUE";
chart1.Series["series1"].XValueMember = "DATE";
chart1.DataSource = dt;
I'd like to be able make a legend of the points on the chart using different colors/symbols based on a third column in the datatable dt called product. I've tried a number of things to get this done but nothing is working. How can I accomplish this?
You can use this code I got from msdn page
// Create a new legend called "Legend2".
chart1.Legends.Add(new Legend("Legend2"));
// Set Docking of the Legend chart to the Default Chart Area.
chart1.Legends["Legend2"].DockToChartArea = "Default";
// Assign the legend to Series1.
chart1.Series["Series1"].Legend = "Legend2";
chart1.Series["Series1"].IsVisibleInLegend = true;
Part of my problem was first understanding how to construct charts in the first place. I realized I needed to create a series for each unique product in my datatable and add those to the chart. I first made a list of unique products in the datatable:
List<string> products = new List<string>();
foreach (DataRow row in dt.Rows)
{
if (!products.Contains(row["product"].ToString()))
{
products.Add(row["product"].ToString());
}
}
Then
foreach (string product in products)
{
string seriesName = product;
chart1.Series.Add(seriesName);
DataTable dtprod = new DataTable();
dtprod = dt.Select("product= '" + product + "'").CopyToDataTable();
chart1.Series[seriesName].ChartType = SeriesChartType.Point;
chart1.Series[seriesName].YValueMembers = "VALUE";
chart1.Series[seriesName].XValueMember = "DATE";
chart1.Series[seriesName].Points.DataBindXY(dtprod.Rows,"DATE", dtprod.Rows, "VALUE");
chart1.Series[seriesName].XValueType = ChartValueType.DateTime;
chart1.Series[seriesName].MarkerSize = 10;
}
This produced a point chart with a unique marker for each product. Thanks for your answers!

Distinct Enumerable DataTable based on column?

This is what i'm trying to accomplish. I want to select only distinct values from all Rows in Column[0].
Then I want to get all the distinct values from column[2] and group them on column[0].
so basically i got a DataTable like so:
Fruit|Apples
Fruit|Pears
Vegetables|Peas
Vegetables|Carrots
so I want to do a foreach on the distinct values, so I would Enumerate Fruit once and then pick up Apples and Pears, and VegeTables once and pick up Peas and Carrots.
I'm doing this to create Accordion Panes, where I want to group my results under one header, the below code does such, however, it creates two panes of Fruit because it does not realize it already went though fruit.
foreach (DataRow dtrow in dtTable.Rows)
{
string idRow = dtrow[0].ToString();
AccordionPane currentPane = new AccordionPane();
currentPane.ID = "AccordionPane" + Guid.NewGuid().ToString();
currentPane.HeaderContainer.Controls.Add(new LiteralControl(dtrow[0].ToString()));
foreach(DataRow dtRow2 in dtTable.Rows)
{
if(dtRow2[0].ToString() == idRow)
{
currentPane.ContentContainer.Controls.Add(new LiteralControl(dtRow2[1].ToString()));
}
}
NavigateAccordion.Panes.Add(currentPane);
}
You can accomplish this easily when using linq, see for yourself:
var groupedRows = from row in dtTable.Rows.AsEnumerable()
group row by row[0] into grouped
select grouped;
foreach (var group in groupedRows)
{
currentPane = new AccordionPane();
currentPane.HeaderContainer.Controls.Add(group.Key.ToString());
foreach (var row in group)
{
currentPane.ContentContainer.Controls.Add(row[1].ToString());
}
}
Or if you want to stick with your current non-linq approach:
foreach (DataRow dtrow in dtTable.Rows)
{
bool skip = false;
foreach (var pane in NavigateAccordion.Panes)
{
if (pane.HeaderContainer.Controls[0].Text == dtRow[0].ToString())
{
skip = true;
break;
}
}
if (!skip)
{
string idRow = dtrow[0].ToString();
AccordionPane currentPane = new AccordionPane();
currentPane.ID = "AccordionPane" + Guid.NewGuid().ToString();
currentPane.HeaderContainer.Controls.Add(new LiteralControl(dtrow[0].ToString()));
foreach(DataRow dtRow2 in dtTable.Rows)
{
if(dtRow2[0].ToString() == idRow)
{
currentPane.ContentContainer.Controls.Add(new LiteralControl(dtRow2[1].ToString()));
}
}
NavigateAccordion.Panes.Add(currentPane);
}
}

Categories

Resources