I have a ListView that is holding session dates for sessions someone may attend. I like to use a MonthCalendar control to allow the user to select several dates, allowing the control's DateSelected event to add an entry to the ListView control as a formatted date string for readability. Sorting is not nice though using the plain sort orders.
So I implemented an nice solution. I added a custom comparer to sort those string dates in the ListView control. Just add the following to your form's namespace, in my case it was the Sessions form called "frmSessions".
class SessionDatesListViewItemComparer : IComparer
private int col;
col = 0;
public int Compare(object x, object y)
int returnVal = -1;
DateTime dateX = Convert.ToDateTime(((ListViewItem)x).SubItems[col].Text);
DateTime dateY = Convert.ToDateTime(((ListViewItem)y).SubItems[col].Text);
returnVal = DateTime.Compare(dateX, dateY);
This allowed me to add a the following line in my form's Constructor (you might have more than one).
this.listView_SessionDates.ListViewItemSorter = new SessionDatesListViewItemComparer();
Now the ListView control sorts nicely each time a date is added.
If you have to display a two numbers and a percentage from older T-SQL you can do the following.
cast((((cast((query1) as decimal) / cast((query1) as decimal)) * 100)) as decimal(5,1)) as 'Percentage'
This will yield you a nice percentage like 67.9. All the casting is to preserve the numbers as decimals and not be boxed to integers. Note: Each query should return a count of something where query 2 is a quantity that is lesser than query1. Such as query2 being the total who like dessert and query 1 is the total who had meals.
I do not deal many
Crystal Reports but recently have had to dive into a few. One of the
things that drive me nuts is dealing with numbers such as in the running
totals. To get a number to display correctly you need to use the ToText
function. A good site I found is this
To get the variable
myNumber = 2.3798554 to display as 2.4 you would do the following:
know this is not very exciting stuff but if you do not deal with things like
this often, it can be a real pain.
It is a good practice to leave comments in your code. Knowing what the hell you were thinking or later intending can be salvation for yourself or the poor soul coming behind you. Comments can leave clues to why you chose one approach over the other. Perhaps staged re-engineering dictated that coding practices vary.
One thing that should not be left in code as comments is old code. There are many free tools that left you version your code. Subversion is a great tool when used with TortoiseSVN. Leaving commented code scattered all over will cause you to second guess yourself, all distraction to the real code, and is just bad practice.
If you have a versioning solution, take time to go back through your code and clean things up. You may find that you can remove lines and leave real comments that are far more knowledgeable than having to remember why you commented out the old code in the first place.
LastPass.com. Get it, use it, save your sanity. This is a great program to be able to remember one hard password and be able to generate several other hard passwords for each site, application, or sign in you have to use. Highly Recommended.
If you have to support some older Winforms you might like to remember this. When opening a MessageBox to display that the user entered incorrect information, if you are doing so from a dialog, catch the DialogResult of the MessageBox and then set
this.DialogResult = DialogResult.None;
to prevent the dialog from closing if you want the user to try again. Otherwise, it will close the dialog box and return to the section of code that called it.
Note: You do not have to catch the DialogResult from the MessageBox. You can still set this after the return from the call to the MessageBox. Just make sure to do either but exiting the body of the dialog itself.
Sometimes it the old habits and not the LINQ query. Look to the final object assignment if data is not what you though it should be and the query tested well in LINQPad. I had failed to look into the fact the foreach loop had a coding error that was causing the data to not be part of the custom class, which output the XML from the web service.
This is akin to first checking the availability of power and then the power cord if you computer has stop working.
I find myself on an assignment where I could not use System.Web.Security.Roles. That meant that I could not use Visual Studio's Website | ASP.NET Configuration. I had to go about things another way. The clues were in these two websites:
You can set in your web.config the restrictions on folders without having to set the restrictions in multiple folders through their own web.config file. In my main default.aspx file in my protected subfolder off my main site, I did the following code due to MultiFormAuthentication (MFA) providing the security to this point:
string role = string.Empty;
if (((Login)Session["Login"]).UserLevelID > 3)
role = "PowerUser";
role = "Newbie";
FormsAuthenticationTicket ticket =
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie =
new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
This all gave me the ability to change restrictions on folders without having to restart the website or having to do any hard coding.
I would highly suggest subscribing to PluralSight if you are wanting to learn different technologies and frameworks. I am a Visual Learner and find that it is very easy to throw on the headphones and run the app from the phone. The videos are well designed and the screens are also well designed to be comfortable viewing on smaller screens such as the phone.
I found many websites that showed either misinformation, incorrect code, or denial of the ability to use a class as a datasource for an asp.net listview control. It is actually very simple.
First you need to define your class. I created private fields and public property to get and set values for those fields. I then created a static function (since it is used often) that would return a list of the class type.
In my case, I was looking for documents that matched the page and page section so I can list them as hyerlinks. After finding the correct folder and obtaining a list of the files, I populated the instantiated object and added it to the list of those objects.
To set and bind the datasource of the listview control I did the following:
//Set datasource and bind
ListViewWebFiles.DataSource = WebFile.GetWebFiles(_section);
My listview is in a user control to aid in dragging it to different pages. The item template is what will be used to work with the bound datasource to create the hyperlinks to the files.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebFiles.ascx.cs"
<asp:ListView ID="ListViewWebFiles" runat="server">
<a href="<%# DataBinder.Eval(Container.DataItem, "FileURL") %>"
<%# DataBinder.Eval(Container.DataItem, "DisplayName") %>
FileURL and DisplayName are the names of the publicly exposed properties of the class. They must match. By the way, there is an <asp:Hyperlink> that could have been used. It would have given the same HTML output in the end.
I can now put the user control on my page and supply it with the section to do the folder lookup and output the files as hyperlinks.
<uc1:WebDocs ID="WebFiles1" runat="server" Section="SchoolPapers" />
This control was placed on Education.aspx page and my file folder virtual path is like "~/Documents/Education/SchoolPapers".