I have a query that reurns me a result of 3500 rows. Each row is a 15 minute time record period of a day:
DATE VALUE
--------------------|-------
1 |2016-10-21 10:00:00| 0
2 |2016-10-21 10:15:00| 0
3 |2016-10-21 10:30:00| 6
4 |2016-10-21 10:45:00| 9
5 |2016-10-21 11:00:00| 18
6 |2016-10-21 11:15:00| 15
.
.
.
3500|2016-11-30 00:15:00| 0
I want to insert this query in a c# datagridview but i dont want an unique column, i want a column for each day.For example, in this query i get 40 day result, and i want a datagridview with a column for each day.
¿Anyone knows which is the fastest way to do this?
Something like that :
var query = new[] { new { Date = new DateTime(2016,10,21,10,0,0), Value = 6 },
new { Date = new DateTime(2016,10,21,10,15,0), Value = 9 },
new { Date = new DateTime(2016,10,30,10,0,0), Value = 18 }};
var groupedQuery = query.GroupBy(p => p.Date.Date, p => p.Value, (key, g) => new { Date = key, Value = g.Sum() });
var grid = new DataGridView();
var bs = new BindingSource();
bs.DataSource = groupedQuery;
grid.AutoGenerateColumns = false;
grid.DataSource = bs;
foreach (var item in groupedQuery)
{
DataGridViewColumn column = new DataGridViewColumn { HeaderText = item.Date.Date.ToString() };
grid.Columns.Add(column);
}
Related
I am updating older code but part of it must stay the same. I have now picker that needs to be filled with list.
My list
public List<TimeoutBetweenSentences> FillTimoutOptions()
{
var newListTimeoutBetweenSentenceses = new List<TimeoutBetweenSentences>()
{
new TimeoutBetweenSentences()
{
Position = 0,
Text = "+ 0 sekund",
Value = 0
},
new TimeoutBetweenSentences()
{
Position = 1,
Text = "+ 1 sekunda",
Value = 1
},
new TimeoutBetweenSentences()
{
Position = 2,
Text = "+ 2 sekundy",
Value = 2
},
new TimeoutBetweenSentences()
{
Position = 3,
Text = "+ 3 sekundy",
Value = 3
},
new TimeoutBetweenSentences()
{
Position = 4,
Text = "+ 4 sekundy",
Value = 4
},
new TimeoutBetweenSentences()
{
Position = 5,
Text = "+ 5 sekund",
Value = 5
},
};
return newListTimeoutBetweenSentenceses;
}
List<TimeoutBetweenSentences> allOptions = FillTimoutOptions();
sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString());
however this displays just as "System collections" DO zou have any idea?
this is adding an entire list as ONE element
sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString());
to add elements of one list to another, use AddRange instead
sentencePausesStepper.Items.AddRange(allOptions.Select(m => m.Text).ToList().ToString());
or better, do this
sentencePausesStepper.ItemsSource = allOptions;
sentencePausesStepper.ItemDisplayBinding = new Binding("Text");
I have this scenario where I need to select the Bar Object with Val greater than 0 while keeping the Object with the current month whether Val is 0 or greater than 0.
var currentMonth = new DateTime(2015, 3, 1); // Just an example
var foo = new List<Bar>()
{
new Bar { Date = '01/01/2015', Val = 40 },
new Bar { Date = '02/01/2025', Val = 30 },
new Bar { Date = '03/01/2015', Val = 0 },
new Bar { Date = '04/01/2015', Val = 2 },
new Bar { Date = '05/01/2015', Val = 5 }
}
// I also need to select the current month whether it's Val is 0 or greater than 0
// Stuck here
var fooResult = foo.Where(f => f.Val > 0);
So the result should be something like this:
{
new Bar = { Date = '03/01/2015', Val = 0 },
new Bar = { Date = '04/01/2015', Val = 2 },
new Bar = { Date = '05/01/2015', Val = 5 }
}
Or if currentMonth is declared as this. var currentMonth = new DateTime(2015, 4, 1);
The result should be something like this:
{
new Bar { Date = '04/01/2015', Val = 2 },
new Bar { Date = '05/01/2015', Val = 5 }
}
Any help would be much appreciated. Thanks
Try this
var fooResult = foo.Where(f => f.Val > 0 || currentMonth.Month ==Convert.ToDateTime(f.Date).Month);
I have a datatable in which multiple data is populated. Need to check a column which contains same values in different rows and add up all the similar rows into one in the same datatable.
for example
id pid pname pAmountex vat vat
1 4 t1 123 2
2 3 t2 45 3
3 4 t3 56 7
4 3 t4 23 8
in the above table,pid column has similar values 4& 3 .i need to sum up the pamountex,vat column of 1st and 3 rd rows for pid 4 and sum up 2 and 3 rows for pid 3.
Here is an example with LINQ:
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("pid");
dt.Columns.Add("pname");
dt.Columns.Add("pamountex",typeof(int));
dt.Columns.Add("vat",typeof(int));
dt.Rows.Add( new object[] { 1, 4, "t1", 123, 2 } );
dt.Rows.Add( new object[] { 2, 3, "t2", 45, 3 } );
dt.Rows.Add( new object[] { 3, 4, "t3", 56, 7 } );
dt.Rows.Add( new object[] { 4, 3, "t4", 23, 8 } );
dt.AsEnumerable()
.GroupBy(r => r.Field<string>("pid") )
.Where (r => r.Count() > 1)
.Select (gr =>
new { id = 0,
pid = gr.Key,
pname = "???",
pamountex = gr.Sum (g => g.Field<int>("pamountex")),
vat = gr.Sum (g => g.Field<int>("vat"))
})
.ToList()
.ForEach( r => dt.Rows.Add(
new object[] {
r.id,
r.pid,
r.pname,
r.pamountex,
r.vat } ));
You did not specify what values are required in id and pname columns for rows that keep sum so I added some defaults(id = 0 and pname = "???") - change it according to your needs.
I assumed that rows with source data should stay in the table.
I have 2 Datatable with these fields:
DataTable1
Hour - Value1
0 - 34
1 - 22
2 - NULL
3 - 12
..
..
23 - 10
DataTable2
Hour - Value1
0 - NULL
1 - 22
2 - 35
3 - 11
..
..
23 - NULL
I need to populate an unique DataTable that has the same "Hour" fields (the hours of the day) and for the values has to get the values from DataTable1 if is not null. If the values in Datatable1 is null, I have to take the correspondet values from DataTable2.
If also the value from DataTable2 is null, I have to log the error.
For my example, I need to get:
DataTableResult
Hour - Value1
0 - 34
1 - 22
2 - 35
3 - 12
..
..
23 - 10
How can I get this?
This is question follows a simple conditional logic you would need to process each element in a foreach statement.
You want to process this so every time an element in a row has null.
You go to that row in datatable 2 and check if that has a value if so this becomes the new value in
datatable 1.
If this does not then throw an error.
I have provided this link how to compare but really you don't need this as all you are doing is testing for null in a field in a row.
Using Linq to objects and assuming dataTable1 and dataTable2 have the same columns:
var hoursMap1 = dataTable1.Rows.Cast<DataRow>().ToDictionary(row => row[0]);
var hoursMap2 = dataTable2.Rows.Cast<DataRow>().ToDictionary(row => row[0]);
var resultTable = new DataTable();
// Clone the column from table 1
for (int i = 0; i < dataTable1.Columns.Count; i++)
{
var column = dataTable1.Columns[i];
resultTable.Columns.Add(column.ColumnName, column.ColumnType);
}
foreach (var entry in hoursMap1)
{
int hours = entry.Key;
DataRow row1 = entry.Value;
DataRow row2 = null;
if (!hoursMap2.TryGetValue(hours, out row2))
{
// Hours in table 1 but not table 2, handle error
}
var fields = new object[resultTable.Columns.Count];
int fieldIndex = 0;
fields[fieldIndex++] = hours;
for (int i = 1; i < row1.ItemsArray.Length; i++)
{
var field1 = row1.ItemsArray[i];
var field2 = row2.ItemsArray[i];
var newField = field1 ?? field2;
if (newField == null)
{
// Field neither in table 1 or table 2, handle error
}
fields[fieldIndex++] = newField;
}
resultTable.Rows.Add(fields);
}
I'm digging in in my Microsoft Visual Studio Documentation and I found this article under C# Reference (ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_csref/html/df01e266-5781-4aaa-80c4-67cf28ea093f.htm), It's about Interface Interface. Here's the example code:
class SelectSample1
{
static void Main()
{
//Create the data source
List<int> Scores = new List<int>() { 97, 92, 81, 60 };
// Create the query.
IEnumerable<int> queryHighScores =
from score in Scores
where score > 80
select score;
// Execute the query.
foreach (int i in queryHighScores)
{
Console.Write(i + " ");
}
}
}
//Output: 97 92 81
Instead of a List, is it also possible to query a DataTable and set the result of the query as the DataSource of a DataGridView?
If yes, suppose I have this structure:
Fruit | CategoryID
---------------------------------------
Lemon | 1
Orange | 1
Apple | 2
Pear | 2
Can anyone please give me an example (if possible, for a beginner's approach.. :). What I want is to display the result in a DataGridView. Display all fruits where its CategoryID is equal to 1. Please help,
Thanks in advance guys.
You need to use AsEnumerable() extension of Databe to select the rows and bind to DataGridView like this:
DataTable table = new DataTable();
table.Columns.Add("Fruit");
table.Columns.Add("ID", typeof(int));
table.Rows.Add(new object[] { "Lemon", 1 });
table.Rows.Add(new object[] { "Orange", 1 });
table.Rows.Add(new object[] { "Apple", 2 });
table.Rows.Add(new object[] { "Pear", 2 });
BindingSource bs = new BindingSource();
bs.DataSource = from row in table.AsEnumerable()
where row.Field<int>("ID") == 1
select new {Fruit = row.Field<string>("Fruit"), ID = row.Field<int>("ID")};
dataGridView1.DataSource = bs;
Try this
var results = from row in dataTable.AsEnumerable()
where row.Field<int>("CategoryID") == 1
select row ;
and you can bind the result row easily to your control.