How can I set column values to GridView Columns? - asp.net

I have csv file like this:
I need to show this csv file with gridview. But I must change format like this:
I must select distinct just date and mount columns and use date values on gridview columns.
How can I use values of csv file for Gridview columns?

Assuming that reading the CSV file is not an issue and you have already something like a List<ClassName>, DataTable or List<string[]>. I'm presuming that it's a List<String[]> where the first "column" is Date, the second Mount and the last % in my following approach.
You need real DateTimes and ints to be able to sum percents by date:
var formatProvider = new CultureInfo("de-DE"); // seems to be the correct format
var mountGroups = listOfStringArray
.Select(arr => new
{
Date = DateTime.Parse(arr[0].Trim(), formatProvider).Date,
Mount = arr[1].Trim(),
Percent = int.Parse(arr[2].Trim())
})
.GroupBy(x => x.Mount);
Now you have grouped by Mount, you just need to sum the percents for every day. You can use a DataTable as datasource for the GridView. Here's code that creates the table with the dynamic columns for every day:
var dataSource = new DataTable();
dataSource.Columns.Add("Mount");
var lastWeekColumns = Enumerable.Range(0, 7)
.Select(d => new DataColumn(DateTime.Today.AddDays(-6 + d).ToShortDateString(), typeof(int)))
.ToArray();
dataSource.Columns.AddRange(lastWeekColumns);
Following loop executes the LINQ query and fills the table:
foreach(var grp in mountGroups)
{
DataRow row = dataSource.Rows.Add();
row.SetField("Mount", grp.Key); // because: GroupBy(x => x.Mount);
foreach(DataColumn dayCol in lastWeekColumns)
{
DateTime day = DateTime.Parse(dayCol.ColumnName, formatProvider);
int sumPercent = grp.Where(x => x.Date == day)
.Select(x => x.Percent)
.DefaultIfEmpty(0) // fallback value for missing days
.Sum();
row.SetField(dayCol, sumPercent);
}
}
Now you just need to use it as datasource (AuthoGenerateColumns set to true)
grid.DataSource = dataSource;
grid.DataBind();

Related

Set Different Value on WebGrid Data if One Specific Row and Column is Null

I am working on MVC using WebGrid and WebGridColumns.
I have a list of data on my Model, but some of the columns on my data are null so I want to specify a different value for null columns.
Here is a sample of my code on View:
var objData = Model.myListofUsers;
var webMyGrid= new WebGrid();
webMyGrid.Bind(objData);
//Build a list of columns
List<WebGridColumn> webColumns= new List<WebGridColumn>();
webColumns.Add(new WebGridColumn { ColumnName = "strName", Header = "Name"});
webColumns.Add(new WebGridColumn { ColumnName = "strPhone", Header = "Phone"});
Not all Names have Phone numbers so I want to specify if there is no Phone number, I want to declare some words like "User have no phone" instead on those fields.
Format property of WeGridColumn could help you.
webColumns.Add(new WebGridColumn { ColumnName = "strName", Header = "Name", Format= (item) => String.IsNullOrEmpty(item.strName)? "Your Message": item.strName});

How to Group By on Columns in datatable in Linq

var res3 = dtPriorityMatrix.AsEnumerable()
.GroupBy(hour => hour.Field<int>("Hour"))
.OrderBy(item => item.Key)
.Select(item => new { Hour = item.Key });
I have a data table with 5 columns and around 200+ rows of data. One has hours (0-23) and others are Priority 1, Priority 2 and so on till Priority 4. I need to extract number of items of each priority for every hour. I need help with the Linq query as it is not my strong field. Thanks
You have to group by both columns. something like this
var query = source.GroupBy(x => new { x.Column1, x.Column2 });
so your code will be
var res3 = dtPriorityMatrix.AsEnumerable()
.GroupBy(hour =>new { hour= hour.Field<int>("Hour"), priority=hour.Field<int>("Priority")} )
.Select(item => new {count=item.count(), Hour = item.Key.hour, priority = item.Key.priority});\
edit
Reply to your comment:
when you have your results in a list, you can write it in anyway that you want.
the out put of the query is a list that contains hour, priority and count.
if you want it in 3 lines of string
string hLine="";
string pLine="";
string countLine="";
foreach(var item in res3)
{
hLine+= item.hour+" ";
pLine+= item.prioriy+" ";
countLine+= item.count+" ";
}
if you want to have them in a table or anything, use the same approach for filling your tables items

I need to select a particular row in a table with selenium webriver

I need to select a particular row in a table with selenium web driver, I have close to 5000 rows, Navigating through each row would be difficult. Any better approach if we have?
You need to click on a checkbox to select the row right?
I'll assume thats what you meant:
The only way I know how is to check each row, select the filter (i.e. a customer number) and once you find it, click on the selection checkbox.
Mostly of the tables are similar so here is an example:
This is your table:
= = = = = = =
=================================================================================================
= = = = = = =
=================================================================================================
= = = = = = =
=================================================================================================
Lets say that in the first row, is where the checkbox is, and that in the second row is the location of the filter.
meaning the data you will use in order to know that this is the row you need....
you will need to inspect the table element in order to find its ID, once you have that info you can use a similar method as the below:
Where you will use your webdriver, data filter, tableID and the number of the column where your data filter is located.
The method will return the row you need to click, then you can simply use the row[columnNumberLocation].Click() in order to select it.
public IWebElement returnRow(IWebDriver webDriver, string filter ,string tableName, int i)
{
IWebElement tableElement = webDriver.FindElement(By.Id(tableName));
IWebElement tbodyElement = tableElement.FindElement(By.TagName("TBODY"));
List<IWebElement> rows = new List<IWebElement>(tbodyElement.FindElements(By.TagName("tr")));
foreach (var row in rows)
{
List<IWebElement> cells = new List<IWebElement>(row.FindElements(By.TagName("td")));
if (cells.Count > 0)
{
string s = cells[i].Text;
if (s.Equals(filter))
{
return row;
}
}
}
return null;
}
It depends on what is your requirement of selecting that particular row.
I assume you need to select that according to the row index.
So it's all about the your x-path to locate the element.
ex: I need to get row number 2000
WebElement element = driver.findElement(By.xpath("//tr[2000]"));

How to convert datatable to list with out List of object or List of class

I have a DataTable dtStudent which have columns like stuid,stuname,stuclass and so on like that so many columns are there and assume i have 10 rows.we dont know how many columns will be present in datatable it may vary depends on functionality.
Now how do i convert datatable to List of string,string,string,string, like that without using List of class becuase unknown columns in the datatable.
Please help me.
You need to iterate through DataTable rows and collect data.
You can use DataTable.Columns for getting column names and using them to get row cells.
List<string[]> result = new List<string[]>();
foreach (DataRow row in rightsTable.Rows)
{
string[] cells = new string[rightsTable.Columns.Count];
for (int i = 0; i < rightsTable.Columns.Count; i++)
{
cells[i] = row[rightsTable.Columns[i].ColumnName].ToString();
}
result.Add(cells);
}
Update:
OK now. I have forgotten about ItemArray property. It simply returns the array of object stored in a row. You just need to execute ToString() on it.
You can use this one instead as it is more convenient and fast.
List<string[]> result = new List<string[]>();
foreach (DataRow row in dt.Rows)
{
list.Add(row.ItemArray.Select(x => x.ToString()).ToArray());
}
Or even shorter using LINQ:
List<string[]> result = dt.Rows
.Cast<DataRow>()
.Select(row => row.ItemArray
.Select(x => x.ToString())
.ToArray())
.ToList();

Can Sorting be performed on a Flex arrayCollection based on date/time values instead of normal text string / alpha?

I have a flex Array Collection created from a live XML data source and am trying to use my date/time string in the array to SORT the array prior to having the UI display the info / listing... currently the array is created and displays fine but the sorting by date / time is NOT working properly...
The routine works if I change the sort field (dataSortField.name) to 'name' (just alphanumeric text string based on filenames generated by my xml source), but if I use 'datemodified' as the sort field ( i.e. 7/24/2013 12:53:02 PM ) it doesn't sort it by date, just tries to sort alphabetically so the date order is not proper at all and for example it shows 1/10/2013 10:41:57 PM then instead of 2/1/2013 11:00:00 PM next it shows 10/10/2013 5:37:18 PM. So its using the date/time as a regular text string
// SORTING THE ARRAY BY DATE DESCENDING...
var dataSortField:SortField = new SortField();
dataSortField.name = "datemodified";
dataSortField.descending = false;
var arrayDataSort:Sort = new Sort();
arrayDataSort.fields = [dataSortField];
arr.sort = arrayDataSort;
arr.refresh();
Now if I CHANGE the dataSortField.name to "name" (which are alphanumeric filenames) it sorts a-z just fine... so How do I get it to sort by DATE where my array data looks like 7/24/2013 12:00:00 PM
Now the TIME part of the date isnt necessary for my sorting needs at all, so Im just looking to sort by date and beyond that the time doesnt matter for my needs but is hard coded in my xml data source.
I tried specifying
dataSortField.numeric = true;
but that didnt work either and while I can use it to specify string or numeric theres not a DATE option as I was expecting.
so my question, to clarify, is how do I make the SORT function acknowledge that I want to sort based on a series of date / time stamps in my array? Im using apache flex 4.9.1 / fb 4.6 premium).
I use this as a date compare function:
public static function genericSortCompareFunction_Date(obj1:Object, obj2:Object):int{
// * -1 if obj1 should appear before obj2 in ascending order.
// * 0 if obj1 = obj2.
// * 1 if obj1 should appear after obj2 in ascending order.
// if you have an XML Datasource; you'll have to do something here to get the
// date objects out of your XML and into value1 and value2
var value1:Date = obj1.dateField;
var value2:Date = obj2.dateField;
if(value1 == value2){
return 0;
}
if(value1 < value2){
return -1;
}
return 1;
}
To apply this to your code; you would do something like this:
var arrayDataSort:Sort = new Sort();
arrayDataSort.compareFunction = genericSortCompareFunction_Date;
arr.sort = arrayDataSort;
arr.refresh();

Resources