Monday, 23 June 2014

Bind ListView in C# Windows Application

Hai To All.

There is a one simple function written below.
Copy and paste the given below query in your code, and call this function on Event where it required

Private void BindListView()
{

  DataTable dt = new DataTable();
  dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new         DataColumn("Name"), new DataColumn("Country") });
  dt.Rows.Add(1, "Raja Durai", "United States");
  dt.Rows.Add(2, "Raja", "United States");
  dt.Rows.Add(3, "Durai", "India");
  dt.Rows.Add(4, "S.Raja Durai", "India");

  lsvDetails.View = View.SmallIcon;
  lsvDetails.Columns.Clear();

  lsvDetails.Columns.Add("users_id", 0);
  lsvDetails.View = View.Details;
  lsvDetails.Columns.Add("User Name ", 150);
  lsvDetails.Columns.Add("Country", 200);
       
  lsvDetails.Items.Clear();
  if (dt == null || dt.Rows.Count == 0) ;
  else
   foreach (DataRow dr in dt.Rows)
     {
        ListViewItem lvItem = new ListViewItem(dr[0].ToString());
        lvItem.SubItems.AddRange(new String[] { dr[1].ToString(), dr[2].ToString});
        lsvDetails.Items.Add(lvItem);
     }
}

Could not load file or assembly crdb_adoplus.dll in WinApplication



when you received this kind of error:

"Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified."

Try to add this to your .config file (usually app.config)

< startup useLegacyV2RuntimeActivationPolicy="true">
< supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
< /startup>

Sunday, 16 February 2014

Implement Animated cursor using C# Winforms



First you must add three namespaces like below.

using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;


Then write a method (LoadCustomCursor) like given below.
 
public static Cursor LoadCustomCursor(string path)
        {
            IntPtr hCurs = LoadCursorFromFile(path);
            if (hCurs == IntPtr.Zero) throw new Win32Exception();
            var curs = new Cursor(hCurs);
            // Note: force the cursor to own the handle so it gets released properly
            var fi = typeof(Cursor).GetField("ownHandle", BindingFlags.NonPublic | BindingFlags.Instance);
            fi.SetValue(curs, true);
            return curs;
        }


        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr LoadCursorFromFile(string path);


Sample usage:

private void btnSave_Click(object sender, EventArgs e)
{ 
this.Cursor = LoadCustomCursor(Application.StartupPath + "\\images\\Black and Blue Working in Background.ani");

//Enter your C# Code here

this.Cursor = Cursors.Default;

}

How to calculate the sum of the datatable column in asp.net?

To calculate the sum of a column in a DataTable use the DataTable.Compute method.

Example of usage

object sumObject = dtPayment.Compute("Sum(Amount)", "");

Display the result in your Total Amount Label like so:


lblTotalAmount.Text = sumObject.ToString();
 

Wednesday, 18 December 2013

Cannot delete file its being used by another process



delete file error the process cannot access the file because it is being used by another process.the process cannot access the file because it is being used c#.

Solution

Type following code before your code
GC.Collect();
GC.WaitForPendingFinalizers();

Wednesday, 9 October 2013

Time validation C#



using System.Text.RegularExpressions;

string strcd = DateTime.Now.ToString("hh:mm:ss tt"); //02:56:59 PM 
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {



string strcd = DateTime.Now.ToString("hh:mm tt"); //01:06 PM        
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {
     // Do something else.
  }