I am getting a problem related to the date format in ASPxGridView.
I have a application in which i am using AspxGridView. I have a column of type combo box which holds the date values.
The column is as
< dxwgv:GridViewDataComboBoxColumn Caption="SERVICE MONTH" Name="ServiceMonthComboBox" Visible=true VisibleIndex="1" FieldName="ServiceMonth">
< EditFormSettings VisibleIndex=1 Visible="false" />< CellStyle HorizontalAlign=Right />
< PropertiesComboBox Style-Font-Names="Verdana" Style-Font-Size="X-Small" TextField="ServiceMonth" ValueField="ServiceMonth">
< Style Font-Names="Verdana" Font-Size="X-Small">< /Style>
< /PropertiesComboBox>< EditFormCaptionStyle ForeColor="Maroon" />
< /dxwgv:GridViewDataComboBoxColumn>
Here, ServiceMonth is of DateTime Type.
At Page_Load event i m using the following code to bind the date data with filter.
GridViewDataComboBoxColumn serviceMonthComboBox = CarHireExchangeGroupSummaryGridView.Columns["ServiceMonthComboBox"] as GridViewDataComboBoxColumn;
serviceMonthComboBox.PropertiesComboBox.ValueType = typeof(DateTime);
serviceMonthComboBox.PropertiesComboBox.Items.Clear();
var serviceMonths = (from item in Presenter.CurrentModel.CarHireExchangeGroupSummaryRecords
select (item.ServiceMonth)).Distinct();
foreach (var serviceMonth in serviceMonths)
{
serviceMonthComboBox.PropertiesComboBox.Items.Add(serviceMonth.ToString("MM/yyyy").Trim(), serviceMonth.ToString("MM/yyyy"));
}
Here, i am binding that combo box with all the distinct ServiceMonth in my records.
Now, i want that, as user filter records using any ServiceMonth, then records should get filter. For that, I have used OnProcessColumnAutoFilter event as follows:
protected void CarHireExchangeGroupSummaryGridView_OnProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e)
{
if (e.Kind == GridViewAutoFilterEventKind.CreateCriteria)
{
switch (e.Column.FieldName)
{
case "ServiceMonth":
if (!string.IsNullOrEmpty(e.Value))
{
((OperandValue)((BinaryOperator)e.Criteria).RightOperand).Value = Convert.ToDateTime(e.Value.ToString());
}
break;
}
}
}
Now, my problem is that, the value i got is something like this: "Wed Dec 1 00:00:00 CST 2010", now, when i am trying to convert this into DateTime as above code, its giving me the error that "Input string in not in proper DateTime format"
Can you please tell me the reason for this and the way through which i can solve my problem.
Try to set the PropertiesComboBox.ValueType to System.DateTime to force the AutoFilterRow’s ASPxComboBox editor convert Value of its Items to the DateTime values:
<PropertiesComboBox ... ValueType="System.DateTime"></PropertiesComboBox>
Related
I am creating headerText for a datagrid (dgTop250). How do I get the variable headerStr to evaluate correctly in the last line of the function? With the code below I get the entire string as the column header in the datagrid, not the evaluated expression that I need. Variable colName is evaluating correctly. I tried creating an Object of headerStr and using Object.valueOf() in the last line, but got the same result as before.
public function get250(event:ResultEvent):void {
(var i:int = 0; i <= dgTop250.columnCount; i++) {
var colName:String=dgTop250.columns[i].dataField;
var headerStr:String="top250.lastResult.IMS001HQ2.SGM.getItemAt(i)."+colName+".label";
(dgTop250.columns[i] as DataGridColumn).headerText = headerStr;
}
}
As an example, this is what I'm getting as the header:
top250.lastResult.IMS001HQ2.SGM.getItemAt(i).STOCK.label
This is what I need:
Stock Number
"Stock Number" is the label for STOCK.
The correct answer is:
Change
var headerStr:String="top250.lastResult.IMS001HQ2.SGM.getItemAt(i)."+colName+".label";
to
var headerStr:String=top250.lastResult.IMS001HQ2.SGM.getItemAt(i)[colName].label;
Correct answer provided by Don Mitchinson.
I have two labels and two text boxes, a Compare validator and a button.
I need it to compare two dates (rental date , return date ) and when the rental date is less or equal to return date are the same. No validation message.
While when when the rental date is less than the return date, display an input error message.
The compare validator has been set with :
controltocompare : txtrental,
controltovalidate: txtreturndate,
operator :greater than equal,
type:date,
errormessage: return date must be greater or equal than rental date,
I am not sure how to get the btn to display it ?
You need to set the property "CausesValidation" of your button to "true" to trigger validation on its click.
Make sure the CompareValidator has runat="server"
Create a method to display message.
private void AlertBox(string Msg)
{
string s = "alert('" + Msg + "')";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ckey", s, true);
}
find the code to validate and throw alert message.
if (!String.IsNullOrEmpty(txtrental.Text) && !String.IsNullOrEmpty(txtreturndate.Text))
{
DateTime ssSD = Convert.ToDateTime(txtrental.Text);
DateTime qsED = Convert.ToDateTime(txtreturndate.Text);
int chktxtfd1_sd = ssSD.CompareTo(qsSD);
if ((chktxtfd1_sd == 0 || chktxtfd1_sd == -1) )
{
//do something bcoz condition is true
}
else
{
lvflag = false;
AlertBox("date must be greater or equal than rental date");
}
}
If you find it useful, please mark it as your answer else let me know...
protected void inderGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "DC_No_Decimal"));
//grdTotal = grdTotal + rowTotal;
grdTotal += rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
e.Row.Cells[3].Text = grdTotal.ToString("c");
//lbl.Text = grdTotal.ToString("c");
}
}
from the above code i m getting total for every page in the grid view. Instead of getting total to every page i need all page total at the end of the grid view footer. Help Immediatly.
Thanks in advance
If you want the total of ALL pages, and for the footer to only appear on the last page, then you can't calculate the totals how you are doing.
At the moment your looping through every row on the gridview. If you are using paging the gridview won't be showing all the rows, so the total won't be correct.
Are you paging with a PagedDataSource or are you limiting the records returned from SQL etc? If you are using a DataSet and a PagedDataSource you'll be able to find the total from the DataSet (as this will contain all the records). Otherwise, you'll have to create a second query to calculate the total.
Then in terms of displaying the values in the footer, you'll have to add an IF statement to your ItemDataBound event to only display this if its the final page.
If you trying to display the record summery, please look this one about Displaying Summary Information in the GridView's Footer
What I do is setup the following in the code behind (This is just an example column):
int totalCallsTaken = 0;
public int CallsTaken(int value)
{
totalCallsTaken += value;
return value;
}
public int CallsTakenTotal()
{
return totalCallsTaken;
}
Then in the ASPX page I put the following template field in:
<asp:TemplateField HeaderText="Calls Taken" FooterStyle-Font-Bold="true">
<ItemTemplate>
<%#CallsTaken(Convert.ToInt32(Eval("CallsTakenCount").ToString())).ToString("N0")%>
</ItemTemplate>
<FooterTemplate>
<%#CallsTakenTotal().ToString("N0")%>
</FooterTemplate>
</asp:TemplateField>
I hope that helps, Ian.
Noddy but you can try one more check in the if condition.
if(e.Row.RowType == DataControlRowType.Footer && inderGrid.PageCount == inderGrid.PageIndex + 1)
{
//code here.
}
// Try this
if(e.Row.RowType == DataControlRowType.Footer && inderGrid.PageCount == inderGrid.PageIndex + 1)
{
for (int i=0; i< inderGrid.Rows.Count; i++)
{
var currentRowCellVal = Convert.ToDecimal(inderGrid.Rows[i].Cells[0].Text);
grdTotal += currentRowCellVal;
}
e.Row.Cells[3].Text = grdTotal.ToString("c");
}
Just to be certain I'm understanding, you're saying you have a grid with say 100 items, but only 25 are shown at any given time. Then you want the footer to only display the sum of those 25 items that are displayed on the page.
There's a couple of options that you can do here for this:
1) Use JavaScript to calculate the total after the page has been rendered.
2) use intelligent SQL to only return those particular rows that you're wanting to display on the grid--and then keep your grid the same
3) calculate the visible rows in your code, and only add them when you need them. Remember, you know in the code behind which Grid.PageIndex you're on as well as how many items each page has. With this knowledge, you should be able to determine via the row index if any given datarow will be rendered to the screen.
You need to generate all records total using the actual data.
If you are doing paging at the data-sire (database) side then you have total at the data-sore side - you may use the same SP that returns a page-full of records to return the total of all records. If you are retrieving all records and doing paging at the web server side then you may use the retrieve data-source to do the totaling.
From optimization perspective, you can compute the total once and store it in the view-state.
If you wish to show the footer on the last row then you can use ShowFooter - set it to true only on the last page.
All you need to do is check whether your row is in the current page and do the calculation.
For example, something like this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "DC_No_Decimal"));
if (e.Row.DataItemIndex >= inderGrid.PageIndex * inderGrid.PageSize
&& e.Row.DataItemIndex < inderGrid.PageIndex * inderGrid.PageSize + inderGrid.PageSize)
grdTotal += rowTotal;
}
Try this,
using System.Linq;
dt.AsEnumerable().Select(x => x.Field<decimal>("DC_No_Decimal")).Sum().ToString();
you can use the code like this for get sum at the footer
gv.DataSource = dt;
gv.Columns[2].FooterText = dt.Rows.Count > 0 ? dt.AsEnumerable().Select(x => x.Field<decimal>("DC_No_Decimal")).Sum().ToString() : "";
gv.DataBind();
my question is:
my table consists of this values: 0, 1, 2 3
but when the gridview loads i want the text to be display instead of just those numbers.
0 = not set, 1 = low, 2 = medium, 3 = high
i could have done this like if/else condition but i just wanted to seek for a optimized sol.
here is my markup gridview:
<asp:TemplateField HeaderText="Priority" SortExpression="Priority" >
<ItemTemplate>
<asp:Label ID="lblPriority" Text='<%# DataBinder.Eval(Container.DataItem,"Priority")%>' runat="server" />
</ItemTemplate>
Assuming you don't have the display values stored in the DB anywhere, this is a way you can implement the rendering part. There may be a more maintainable way to store the lookup values, if anyone could contribute I'd appreciate it.
I wrote this in notepad since I don't have Visual Studio on my machine. Please excuse me if there are any syntax errors.
Markup:
<asp:Label ID="lblPriority" Text='<%# RenderPriority(DataBinder.Eval(Container.DataItem,"Priority")) %>' runat="server" />
Code:
Protected Function RenderPriority(ByVal dbValue As Object) As String
Dim strReturn as String = String.Empty
If Not IsDbNull(dbValue) Then
Dim intValue as Integer
If Integer.TryParse(dbValue, intValue) Then
Select Case intValue
Case 0
strReturn = "not set"
Case 1
strReturn = "low"
Case 2
strReturn = "medium"
Case 3
strReturn = "high"
End Select
Else
strReturn = dbValue.ToString()
End If
End If
Return strReturn
End Function
Edit:
After re-reading your question I get the impression you would prefer to avoid writing a specific function for this purpose in the code-behind page. If that is the case you should probably store the strings you want associated with the key values in the DB and pull them out through your SQL statement. Or, at the very least push the functionality down into a Data Access Layer. Either way ideally the GridView column will be presented with the required string by its datasource.
Why not using enumerations? Here:
Have an enumeration called Priority. Then put Description attribute on each of them, and write the display text inside the constructor of that attribute.
public enum Priority
{
[Description("not set")]
NotSet = 0,
[Description("low")]
Low = 1,
[Description("medium")]
Medium = 2,
[Description("high")]
High = 3
}
Then use Enum.ToObject method to convert the numbers (values) into their associated display value using these functions:
// An extension method for ease of use that converts an integer into enum
public static T ToEnum<T>(this int value)
{
if (typeof(T).BaseType.Name != typeof(Enum).Name)
{
throw new Exception("Input type of generic method ToEnum<T>() is not an Enum");
}
return (T)Enum.ToObject(typeof(T), value);
}
// Another extension method that gets the display text of the Description attribute of a given enum constant
public static string GetDescription(this Enum value)
{
return ((DescriptionAttribute)value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false)[0]).Description;
}
Then in your code, you can write:
databaseValue.ToEnum<Priority>().GetDescription();
You can use the RowDataBound event of the GridView and set the value on specific condition.
Here is the complete code....
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRow dr = ((System.Data.DataRowView)e.Row.DataItem).Row;
if (dr["Priority"].ToString() == "0")
{
((Label)e.Row.FindControl("lblPriority")).Text = "not set";
}
else if (dr["Priority"].ToString() == "1")
{
((Label)e.Row.FindControl("lblPriority")).Text = "low";
}
else if (dr["Priority"].ToString() == "2")
{
((Label)e.Row.FindControl("lblPriority")).Text = "medium";
}
else if (dr["Priority"].ToString() == "3")
{
((Label)e.Row.FindControl("lblPriority")).Text = "high";
}
}
}
The ASP.NET calendar always displays 6 weeks of dates in a 7x6 grid. My problem is that the first day of the target month does not necessarily appear in the first row... in some cases, the entire first row displays dates from the previous month. In other cases, the entire last row displays dates from the next row.
Is there a reliable way to query the calendar object to determine the 42-day range that would be rendered for a specific month/year?
For example, consider June 2008 and Feb 2009:
Notice that the first week contains ONLY dates from prior month http://img371.imageshack.us/img371/2290/datesmq5.png
I assume that the calendar tries to avoid bunching all of the "other month" dates at either the top or bottom of the grid, and therefore puts the first of the target month on the 2nd row. I am looking for an easy way to determine that the displayed range for June 2008 is May 25 - July 5, for instance.
Looking at the public members exposed by the ASP.NET Calendar control I do not believe that this information is something that you can just get from the calendar control.
You have a few options as "workarounds" to this though, although not nice....but they would work.
You could manually calculate the first week values
You can handle the "day render" event to handle the binding of the individual days, and record min/max values.
Granted neither is elegant, but AFAIK it is the only real option
Edit
After discussion in the comments, another option is a modified version of my second option above. Basically the first time Day Render is called, get the block of data for the next 42 days, then you can simply search the list for the proper day value to display on future calls to DayRender, avoiding a DB hit for each day. Doing this is another "non-elegant" solution, but it works, and reduces a bit of load on the DB, but introduces some overhead on the application side.
It will be important here to define well structured page level properties to hold the items during the binding events, but to ensure that if a month changed, etc that it wasn't loaded incorrectly etc.
I wrote a couple of methods to help with this. Just pass in Calendar.VisibleDate:
public static DateTime GetFirstDateOfMonth(DateTime date)
{
return new DateTime(date.Year, date.Month, 1);
}
public static DateTime GetFirstDisplayedDate(DateTime date)
{
date = GetFirstDateOfMonth(date);
return date.DayOfWeek == DayOfWeek.Sunday ? date.AddDays(-7) : date.AddDays((int)date.DayOfWeek * -1);
}
public static List<DateTime> GetDisplayedDates(DateTime date)
{
date = GetFirstDisplayedDate(date);
List<DateTime> dates = new List<DateTime>();
for (int i = 0; i < 42; i++)
{
dates.Add(date.AddDays(i));
}
return dates;
}
I've just been looking into this myself, and got directed to here. I'm personally tempted to go with option two, because the alternative is messy. Ronnie's version is nice, but unfortunately doesn't take into account cultures with different FirstDayOfWeeks.
Using Reflector, we can see how it's done internally:
...
DateTime visibleDate = this.EffectiveVisibleDate();
DateTime firstDay = this.FirstCalendarDay(visibleDate);
...
private System.Globalization.Calendar threadCalendar =
DateTimeFormatInfo.CurrentInfo.Calendar;
private DateTime EffectiveVisibleDate()
{
DateTime visibleDate = this.VisibleDate;
if (visibleDate.Equals(DateTime.MinValue))
{
visibleDate = this.TodaysDate;
}
if (this.IsMinSupportedYearMonth(visibleDate))
{
return this.minSupportedDate;
}
return this.threadCalendar.AddDays(visibleDate,
-(this.threadCalendar.GetDayOfMonth(visibleDate) - 1));
}
private DateTime FirstCalendarDay(DateTime visibleDate)
{
DateTime date = visibleDate;
if (this.IsMinSupportedYearMonth(date))
{
return date;
}
int num = ((int)
this.threadCalendar.GetDayOfWeek(date)) - this.NumericFirstDayOfWeek();
if (num <= 0)
{
num += 7;
}
return this.threadCalendar.AddDays(date, -num);
}
private int NumericFirstDayOfWeek()
{
if (this.FirstDayOfWeek != FirstDayOfWeek.Default)
{
return (int) this.FirstDayOfWeek;
}
return (int) DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
}
private bool IsMinSupportedYearMonth(DateTime date)
{
return this.IsTheSameYearMonth(this.minSupportedDate, date);
}
private bool IsTheSameYearMonth(DateTime date1, DateTime date2)
{
return (((this.threadCalendar.GetEra(date1) ==
this.threadCalendar.GetEra(date2)) &&
(this.threadCalendar.GetYear(date1) ==
this.threadCalendar.GetYear(date2))) &&
(this.threadCalendar.GetMonth(date1) ==
this.threadCalendar.GetMonth(date2)));
}
Sadly, the functionality is already there, we just can't get at it!
Mitchel,
Worked perfectly, thank you.
Started with a public variable
bool m_FirstDay = false
in the day_render function
if(m_FirstDay == false)
{
DateTime firstDate;
DateTime lastDate;
firstDate = e.Day.Date;
lastDate = firstDate.AddDays(41);
m_FirstDay = true;
}
I then had the visible date range of the asp.net calendar control. Thanks again.
see this one.
How to Remove the Last Week Of a Calendar