Monday, 17 October 2011

String Format for DateTime [C#]

This example shows how to format DateTime using String.Format method. All formatting can be done also using DateTime.ToString method.

Custom DateTime Formatting

There are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
Following examples demonstrate how are the format specifiers rewritten to the output.
[C#]
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone

Friday, 14 October 2011

DevExpress, GridControl, how the Best Fit Column feature works

The property GridView.BestFitMaxRowCount indicates how many rows will be processed in order to adjust the best column width.
  • -1 means that all row will be processed
  • 0 nothing
  • Any other value indicates how many will be processed
For performance issues apply a small number or apply 0.
Important note: If auto width is enabled, a column's GridColumn.Width property doesn't contain its visible width, but the value used when auto width is false, so that a column's layout can be restored. A column's visible width is available via its GridColumn.VisibleWidth property.

Monday, 10 October 2011

Method code, to convert a deleted DataRow to non deleted DataRow

        /// <summary>
        /// Get the deleted record you provide as non deleted row; NOTE: the State of the row is ADDED.
        /// </summary>
        /// <param name="sourceDataTable"></param>
        /// <param name="deletedDataRow"></param>
        /// <returns></returns>
        public static DataRow readDeletedDataRow(DataTable sourceDataTable,DataRow deletedDataRow) {
            int deletedDataRowIndex = sourceDataTable.Rows.IndexOf(deletedDataRow);
            DataView deletedView = new DataView(sourceDataTable, null, null, DataViewRowState.Deleted);
            DataTable deletedRecordsTable = deletedView.ToTable();
            return deletedRecordsTable.Rows[deletedDataRowIndex];
        }

Accessing deleted row field values from data table with C#.net

Solution #1



          Sometimes we delete data row in data table of dataset. Then we may need again to get deleted row field information from the dataset back for some calculation purpose. It gives error if we want to access the row data from the data table telling "The data row has been deleted ...".

         We can still access the field data information by using DataRowVersion.Original parameter. For example

         int customerId = Convert.ToInt32( DataTable1.Rows[0][0, DataRowVersion.Original];

         This will give the original version of the row data. And voila - we have our original data back.


Solution #2



          We can make a Data View out of the datatable. Then convert that data view into data table again. This will give us the original data. But this time the datarowstate = "added".

          We see one example code for this.

          DataView myView = new DataView(sourceTable, null, null, DataViewRowState.Deleted);
          DataTable myTable = myView.ToTable();

Monday, 19 September 2011

how to add and remove event handers

simple with += and -= operators

/* A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson Publisher: Apress  L.P.  ISBN: 1-893115-62-3  */
// 23 - Events\Custom Add and Remove --- copyright 2000 Eric Gunnerson


using System;
using System.Collections;
using System.Runtime.CompilerServices;


public class EventsCustomAddandRemove
{
    static public void ButtonHandler(object sender, EventArgs e)
    {
        Console.WriteLine("Button clicked");
    }
    
    public static void Main()
    {
        Button button = new Button();
        
        button.Click += new Button.ClickHandler(ButtonHandler);
        
        button.SimulateClick();
        
        button.Click -= new Button.ClickHandler(ButtonHandler);
        
        button.TearDown();
    }
}

Thursday, 18 August 2011

where to register notes to be appeared in intellisent (intelligent)

before the class, add your notes as this:

///
/// This function will create an image with from the given filename
///

public class MyFile
{
... the rest of code

Visual Studio shortcut keys

You are familiar with many of Visual Studio's shortcut keys, but not all of them. Here is a handy reference that can make your .NET lifestyle easier and a lot more productive. The 'must-know' shortcut keys are highlighted.


General

Shortcut Description
Ctrl-X or
Shift-Delete
Cuts the currently selected item to the clipboard
Ctrl-C or
Ctrl-Insert
Copies the currently selected item to the clipboard
Ctrl-V or
Shift-Insert
Pastes the item in the clipboard at the cursor
Ctrl-Z or
Alt-Backspace
Undo previous editing action
Ctrl-Y or
Ctrl-Shift-Z
Redo the previous undo action
Ctrl-Shift-V or
Ctrl-Shift-Insert
Pastes an item from the clipboard ring tab of the Toolbox at the cursor in the file and automatically selects the pasted item. Cycle through the items on the clipboard by pressing the shortcut keys repeatedly
Esc Closes a menu or dialog, cancels an operation in progress, or places focus in the current document window
Ctrl-S Saves the selected files in the current project (usually the file that is being edited)
Ctrl-Shift-S Saves all documents and projects
Ctrl-P Displays the Print dialog
F7 Switches from the design view to the code view in the editor
Shift-F7 Switches from the code view to the design view in the editor
F8 Moves the cursor to the next item, for example in the TaskList window or Find Results window
Shift-F8 Moves the cursor to the previous item, for example in the TaskList window or Find Results window
Shift-F12 Finds a reference to the selected item or the item under the cursor
Ctrl-Shift-G Opens the file whose name is under the cursor or is currently selected
Ctrl-/ Switches focus to the Find/Command box on the Standard toolbar
Ctrl-Shift-F12 Moves to the next task in the TaskList window
Ctrl-Shift-8 Moves backward in the browse history. Available in the object browser or Class View window
Alt-Left Arrow Go back in the web browser history
Alt-Right Arrow Go forward in the web browser history