Devexpress mvc gridview group datetime column by date? - c#

I have datetime column in gridview:
settings.Columns.Add(clmn =>
{
clmn.FieldName = "InsertDate";
clmn.Caption = TsmDxWeb.Resources.InsertDate;
clmn.CellStyle.Wrap = DefaultBoolean.False;
clmn.Width = 150;
clmn.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
});
output:
and group output:
grid group data by datetime, but I want to group them by only date.
If I add clmn.Settings.GroupInterval = DevExpress.XtraGrid.ColumnGroupInterval.Date;, no data shown in grouping. Because gridview add time '00.00.000' to dates.
Also, there is a gridviewSetting that named setting.CustomColumnGroup, but there is no example. I researched a lot for an example but, I could not find anything.
How can I group records only by date without time?

settings.Columns.Add(clmn =>
{
clmn.FieldName = "InsertDate";
clmn.Caption = TsmDxWeb.Resources.InsertDate;
clmn.CellStyle.Wrap = DefaultBoolean.False;
clmn.Width = 150;
clmn.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
clmn.Settings.GroupInterval = DevExpress.XtraGrid.ColumnGroupInterval.Date;
});
"Rows are grouped by the date part of their values, the time portion is ignored"

Related

Date displaying in weird way when adding milliseconds

So in my application I extract data from a database and then extract it to a csv file and I say what I want under each columns like so:
public List<ExtractDocument> GetExtractDocuments(List<Guid>ItemDetailIds)
{
var items = GetExtractData(ItemDetailIds);
return items.Select().Select(item => new ExtractDocument
{
ItemDetailId = item.Field<Guid>("ItemDetailID"),
ItemNumber = item.Field<string>("Number"),
ItemTitle = item.Field<string>("Title"),
ItemRevision = item.Field<string>("RevisionNumber"),
ActionType = item.Field<string>("Action"),
ActionedBy = item.Field<string>("ActionedBy"),
ActionedDate = item.Field<DateTime?>("ActionedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Comment = item.Field<string>("Comment"),
TaskType = item.Field<string>("TaskType"),
StartDate = item.Field<DateTime?>("StartDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
CompletedDate = item.Field<DateTime?>("CompletedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Status = item.Field<string>("Status"),
Outcome = item.Field<string>("Outcome"),
ActionerName = item.Field<string>("TaskActionedBy"),
DateActioned = item.Field<DateTime?>("ActionDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
ActionTaken = item.Field<string>("TaskAction"),
TaskComment = item.Field<string>("TaskComment"),
Link = item.Field<string>("URL")
}).ToList();
}
This is working but when I open up the csv file and look under the date columns such as "ActionedDate" the date is showing up in a weird way, for example one of the values should be showing as: 03/03/2022 09:07:46 but it is showing in the field as:07:46.3, why is it doing this and how I can prevent that from happening? it doesn't do this for all the fields but it does for the majority
Yeah this was a face palm moment, just realised I had the format written as: "dd/MM/yyyy HH:mm:ss.fff" when it should be "dd/MM/yyyy HH:mm:ss:fff", changing it from ss.fff to ss:fff, fixed the issue

The property 'ID' is part of the object's key information and cannot be modified in asp.net mvc

I have used entity framework with code first approach.
when I am trying to pass record one by one Fromdate to Todate, 1st time its work, after it gives error like: "The property 'ID' is part of the object's key information and cannot be modified."
var fd = todaycooked.CookDate; // 2016-07-01
var td = todaycooked.ToCookDate; //2016-11-01
for (var date = fd; date <= td; date = date.AddDays(1))
{
var product = db.Products.Find(todaycooked.ProductID);
product.Qty = product.Qty + todaycooked.QTY;
todaycooked.Product = product;
todaycooked.CookDate = date;
db.TodayCookeds.Add(todaycooked);
db.SaveChanges();
}
Thanks in Advance.
You are setting the Product and CookDate once per day, so I assume you want one record per day - which means you mean one object per day. I suspect you actually want something like:
var fd = todaycooked.CookDate; // 2016-07-01
var td = todaycooked.ToCookDate; //2016-11-01
// this doesn't change per day, so only fetch it once
var product = db.Products.Find(todaycooked.ProductID);
for (var date = fd; date <= td; date = date.AddDays(1))
{
var toAdd = todaycooked.Clone(); // TODO: add a suitable clone method
toAdd.Product = product;
toAdd.CookDate = date;
db.TodayCookeds.Add(toAdd);
product.Qty = product.Qty + todaycooked.QTY;
db.SaveChanges();
}
However, you can probably also get away with moving the db.SaveChanges() to outside of the loop, which would make the whole thing atomic (rather than risking getting the first 4 of 8 days saved, then an error):
...
for (var date = fd; date <= td; date = date.AddDays(1))
{
...
}
db.SaveChanges();

Update a List of values with empty string and keep value in other fields

I have a list in the form-
FieldName- DataType-
Date DateTime
DateString String
Unit double
Price double
I want to perform an operation such that if weakday on Date is not Monday then update DateString with empty string otherwise keep the value of DateString as it is.
Data is present in List.
UPDATE
I have applied aggregate function on DataTable dtGas as follows-
var qGas = from x in dtGas.AsEnumerable()
group x by new
{
Date = x.Field<DateTime>("Date"),
DateString = x.Field<string>("DateString")
} into egroup
select new
{
Date = egroup.Key.Date,
DateString = egroup.Key.DateString,
Unit = egroup.Sum(r => r.Field<double>("Unit")),
Price = egroup.Sum(r => r.Field<double>("Price"))
};
Now I need to show this result into Chart. Due to large amount of data values are overlapping on X-axis.
That is why I need to remove some values from DateString and show only few of them.
You can simply use conditional operator like this:-
group x by new
{
Date = x.Field<DateTime>("Date"),
DateString = x.Field<string>("DateString")
} into egroup
let isMonday = egroup.Key.Date.DayOfWeek.ToString() == "Monday"
select new
{
Date = egroup.Key.Date,
DateString = isMonday ? egroup.Key.DateString : "",
..other properties

Date format in LINQ

i got problem with my LINQ to format date. Here is code
var resultCustomer = from row in formDg.dtCustomer.AsEnumerable()
where row.Field<int>("customerID") == customerID2
select new
{
name = row["customerName"].ToString(),
ic = row["customerIC"].ToString(),
add1 = row["customerAdd1"].ToString(),
add2 = row["customerAdd2"].ToString(),
tel = row["customerTel"].ToString(),
tel2 = row["customerTel2"].ToString(),
tel3 = row["customerTel3"].ToString(),
email = row["customerEmail"].ToString(),
dateRegister = row["customerDateRegister"].ToString(),
customerRef = row["customerRef"].ToString(),
customerGroup = row["groupCustName"].ToString(),
panelName = row["panelName"].ToString(),
};
var firstRecord = resultCustomer.First();// My record return single row only
My question is how i can format custom date like "dd/MM/yyyy" in customerDateRegister?, Because I got problem when my textbox show 16-Nov-12 12:00:00 AM. I don`t want to display time but just date only.
I have try like this tbDateRegOv.Text = firstRecord.dateRegister.ToString("dd/MM/yyyy");. not work. Then i try dateRegister = row["customerDateRegister"].ToString("dd/MM/yyyy"), in LINQ. Same problem.
So how can I solved this problem? Thanks for help. :)
You need to cast your row as a DateTime first. This will allow you to use the custom format strings for dates.
dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy")
If customerDateRegister is of type DateTime you should make it a DateTime in the first place. Therefor use the DataRow extension method Field<T>:
dateRegister = row.Field<DateTime>("customerDateRegister")
You could try this , replace your dateRegister line with this.
dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy"),
Or, you can use MaskedEditBox with the desired datetime mask

Ordering the row_number using LinqToSql in asp.net/C#

I have set of records in a database table lets call it Components Table which is defined as follows.
The administrator can disable some of the components using disableflag which is the last column of the table. If a particular component is disabled it should not appear in the gridview of the user.
I'm getting the data from the database and presenting through the gridview as shown here, if you observe the SNo values are not in order.
The linq query that i'm using to retrieve the data is:
var gridViewResults = from results in db.Components where results.DisableFlag == false
select new { SNo = results.SNo, ComponentNames = results.Component_Name, Size = results.Size__in_MB_, Price = results.Price__in_SEK_, TotalDownloads = results.Total_Downloads, Description = results.Description };
But I want the data to be shown in order meaning with SNo to be 1, 2, 3, 4 with out dependency on the database table SNO values: for reference look at this.
I'm not able to figure out how to use the linq query to achieve this:
I have tried this query:
(db.Components.AsEnumerable().Select((iterator)=> new{iterator.SNo + 1})
But i think it is absurd. Can some one help me out on this.
Thanks in anticipation.
If you're absoutely certain you want to ignore the database numbers (why output the numbers if they don't actually correspond to anything?) you may be able to try the following:
var gridViewData = from results in db.Components
where results.DisableFlag == false
select new
{
ComponentNames = results.Component_Name,
Size = results.Size__in_MB_,
Price = results.Price__in_SEK_,
TotalDownloads = results.Total_Downloads,
Description = results.Description
};
var gridViewResults = gridViewData.AsEnumerable().Select((item, index) => new
{
SNo = index + 1,
ComponentNames = item.ComponentNames,
Size = item.Size,
Price = item.Price,
TotalDownloads = item.TotalDownloads,
Description = item.Description
});
EDIT: Alternate solution from How To Project a Line Number Into Linq Query Results
EDIT2: Fix for unsupported select by SQL: Linq error - "NotSupportedException: Unsupported overload used for query operator 'Select'"
Hi everyone here is the final answer. Joshua did all of the work. A big thanks to him. Just want to highlight the answer to anyone with the same problem for the future. If any one want to vote up please vote for Joshua
var gridViewData = from results in db.Components
where results.DisableFlag == false
select new
{
ComponentNames = results.Component_Name,
Size = results.Size__in_MB_,
Price = results.Price__in_SEK_,
TotalDownloads = results.Total_Downloads,
Description = results.Description
};
var gridViewResults = gridViewData.AsEnumerable().Select((item, index) => new
{
SNo = index + 1,
ComponentNames = item.ComponentNames,
Size = item.Size,
Price = item.Price,
TotalDownloads = item.TotalDownloads,
Description = item.Description
}).ToList();
This should work.

Categories

Resources