how to Sort Gridview At A Pageload itself? - sorting

I have this code of mine on pageload, please have a look:
var d = from p in db.Questions
where p.CatId == Convert.ToInt32(s)
select p;
DataTable datatable =d as DataTable;
DataView dataview = new DataView(datatable);
dataview.Sort ="id DESC" ;
GridView1.DataSource =dataview;
GridView1.DataBind();
I have a column named "id" in the table "Questions" on which I want to sort the gridview on pageload itself.
The following Error shows up during compilation:
DataTable must be set prior to using DataView.
Please Help.

well, I already Figured out the answer. Here is the code which I inserted
var d = from p in db.Questions
orderby p.Dtime descending
where p.CatId == Convert.ToInt32(s)
select p;
DataTable datatable =d as DataTable;
DataView dataview = new DataView(datatable);
dataview.Sort ="id DESC" ;
GridView1.DataSource =dataview;
GridView1.DataBind();

Related

Summing and grouping in datatable

1.Requirement:
the above is the result of my datatable value
1. Need to group the states by summing the premium value
2. need to mention the address, city for the states which is having highesht premium values
3. Linq or for loop anything is fine for me, please help me out
thanks,
Srikanth Anantharaman
You could use this LINQ query to fill a second table:
DataTable newTable = tbl.Clone(); // empty table, same schema
var groupedByState = tbl.AsEnumerable()
.GroupBy(r => r.Field<string>("State"));
foreach(var group in groupedByState)
{
DataRow maxPremRow = group.OrderByDescending(r => r.Field<int>("Premium")).First();
DataRow newRow = newTable.Rows.Add();
newRow.SetField("State", group.Key);
newRow.SetField("Address", maxPremRow.Field<string>("Address"));
newRow.SetField("City", maxPremRow.Field<string>("City"));
newRow.SetField("Premium", group.Sum(r => r.Field<int>("Premium")));
}

LINQ Where condition against datatable

I have a DataTable. I would like to filter DataRows where City = "Hongkong".
How to apply LINQ against DataRow?
You could use the following Query
var filter = testTable.AsEnumerable().
Where(x => x.Field<string>("City") == "HongKong");
var result = dr.Where(r => r.Field<string>("City") == "Hongkong");
Using LINQ to DataSets, you can do the following:
DataTable table;
var rows =
from row in table.AsEnumerable()
where row.Field<string>("City") == "Hongkong"
select row;

c# LINQ DataTable substraction

i have two DataTables which has different columns but one column is same.
i want to get new DataTable which contains row from dt1 which are not exist in dt2.
how can i do it using LINQ?
that's is my current code, but it work only for Datatables with same columns:
private void Delete_HPM_Deleted_Points(int YellowCard_Request_ID)
{
DataTable dt_hpm_points_DB = DataAccessLayer.Get_YellowCard_Request_HPM_Points(YellowCard_Request_ID);
//dt_hpm_points_DB.Columns.Remove("HPM_Tool_ID");
//dt_hpm_points_DB.Columns.Remove("HPM_Point_Message");
//dt_hpm_points_DB.Columns.Remove("HPM_Point_isDisabled");
//dt_hpm_points_DB.Columns.Remove("HPM_Point_Comments");
DataTable dt_hpm_points_Local = UserSession.Get_User_YC_HPM_Added_Points_DATATABLE();
//dt_hpm_points_Local.Columns.Remove("HPM_Tool_ID");
//dt_hpm_points_Local.Columns.Remove("HPM_Point_Message");
//var rowsInFirstNotInSecond = dt_hpm_points_DB.Rows.Where(r1 => !dt_hpm_points_Local.Rows.Any(r2 => r1.ID == r2.ID));
DataTable dt_points_to_remove = dt_hpm_points_DB.AsEnumerable().Except(dt_hpm_points_Local.AsEnumerable(), DataRowComparer.Default).CopyToDataTable();
foreach (DataRow dr in dt_points_to_remove.Rows)
{
DataAccessLayer.Delete_YellowCard_Request_HPM_Point(YellowCard_Request_ID, dr["HPM_Point_ID"].ToString());
}
}
You can use Linq-To-DataSet with a "Left Outer Join":
var justInDT1 = from r1 in dt1.AsEnumerable()
join r2 in dt2.AsEnumerable()
on r1.Field<int>("PK_ID") equals r2.Field<int>("FK_ID") into GJ
from subRow in GJ.DefaultIfEmpty()
where subRow == null
select r1;
How to: Perform Left Outer Joins (C# Programming Guide)

Linq with DbNull values

I have a DataTable that contains columns that would contain DbNull values. I want to use Linq and output a new DataTable but I want the DbNull values to be perserved. Here is an example:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("C1",System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("C2",System.Type.GetType("System.String")));
DataRow row;
row = dt.NewRow();
row["C1"] = "Hello";
row["C2"] = "World";
dt.Rows.Add(row);
row = dt.NewRow();
row["C1"] = DBNull.Value;
row["C2"] = "World";
dt.Rows.Add(row);
var cars1 = from car in dt.AsEnumerable()
where car.Field<String>("C2") == "World"
select car;
DataTable cars2 = cars1.CopyToDataTable();
How can the C1 column of the second row have the DbNull value be perserved in cars2 DataTable?
Thomas is correct - CopyToDataTable() does nothing special to DBNull field values. However, the Field<T> extension method on DataRow does return null for DBNull - were you using that to test cars2?

Iterating Linq result set using indexers

Let's ay I have this query:
var results = from row in db.Table select row;
How can I access this:
string name = results[0]["columnName"];
if you really want a particular index you can use the Skip() method with First().
var rowOffset = 0;
var results = (from row in db.Table
select row).Skip(rowOffset).First()["columnName"];
But unless you are using a Where clause I would really recommend using the indexer. The indexer is pretty much a direct reference while the LINQ statement would be using the objects iterator.
Also don't forget you can do much more advanced stuff with LINQ.
var rowOffset = 0;
var pageLength = 10;
var results = (from row in db.Table
let colValue = row["columnname"]
where colValue != null
select colValue.ToString()
).Skip(rowOffset)
.Take(pageLength)
.ToArray();
var commaString = string.Join(", ", results);
If you specifically just want the zeroth element, you can use results.First()
results is a IEnumerable list of Rows. So you can get it with a simple foreach.
foreach(var row in results)
{
string name = row["columnName"];
}
(from row in db.Table select row).First().columnName

Resources