Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

7.12.11

Chuẩn hóa xâu nhập họ tên

public static string FormatProperCase(string str)
        {
            CultureInfo cultureInfo = new CultureInfo("vi-VN");
            TextInfo textInfo = cultureInfo.TextInfo;
            str = textInfo.ToLower(str);
            // Replace multiple white space to 1 white  space
            str = System.Text.RegularExpressions.Regex.Replace(str, @"\s{2,}", " ");
           //Upcase like Title
            return textInfo.ToTitleCase(str);
        }

24.3.11

Row footer trong Xtragrid

1- Gridview/OptionView: ShowFooter = true
2- Columns/SummaryItem: Xác lập displayformat, summaryType

17.8.09

Dataset To XML và XML to Dataset, Object To XML và XML to Object

Khi cần chuyển dữ liệu qua webservice, do yêu cầu bảo mật dữ liệu, ta có thể mã hóa dữ liệu cho từng trường, nhưng như vậy cấu trúc dữ liệu vẫn có thể bị lộ. Giải pháp là chuyển dữ liệu về dạng XML (chuyển hẳn về dạng string) và sau đó convert lại. Sau đây là các hàm convert cực đơn giản để phục vụ việc này:

Dataset To XML và XML to Dataset

public static string DataSetToString(DataSet ds)

{

StringWriter stringWriter = new StringWriter();

ds.WriteXml(stringWriter);

return stringWriter.ToString();

}

public static DataSet StringToDataSet(string str)

{

DataSet ds = new DataSet();

try

{

XmlDocument doc = new XmlDocument();

doc.LoadXml(str);

ds.ReadXml(new XmlNodeReader(doc));

}

catch { }

return ds;

}

Object To XML và XML to Object

public static object Xml2Object(string xml, System.Type objType)

{

StringReader stream = null;

XmlTextReader reader = null;

try

{

// serialise to object

XmlSerializer serializer = new XmlSerializer(objType);

stream = new StringReader(xml); // read xml data

reader = new XmlTextReader(stream); // create reader

// covert reader to object

return serializer.Deserialize(reader);

}

catch

{

return null;

}

finally

{

if (stream != null) stream.Close();

if (reader != null) reader.Close();

}

}

public static string Object2Xml(object obj)

{

MemoryStream stream = null;

TextWriter writer = null;

try

{

stream = new MemoryStream(); // read xml in memory

writer = new StreamWriter(stream, Encoding.Unicode);

// get serialise object

XmlSerializer serializer = new XmlSerializer(obj.GetType());

serializer.Serialize(writer, obj); // read object

int count = (int)stream.Length; // saves object in memory stream

byte[] arr = new byte[count];

stream.Seek(0, SeekOrigin.Begin);

// copy stream contents in byte array

stream.Read(arr, 0, count);

UnicodeEncoding utf = new UnicodeEncoding(); // convert byte array to string

return utf.GetString(arr).Trim();

}

catch

{

return string.Empty;

}

finally

{

if (stream != null) stream.Close();

if (writer != null) writer.Close();

}

}

27.2.09

Một số thứ linh tinh

Lấy Version ClickOnce
ApplicationDeployment.CurrentDeployment.CurrentVersion

Thêm column cho table của access

OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
try
{
string sql = "SELECT TOP 1 * FROM TableName";
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
DataTable table = new DataTable();
da.Fill(table);
int i = table.Columns.IndexOf(fileldName);
if (i == -1)
{
string sqlAdd = "ALTER Table TableName Add Column fileldName DataType(Leght)";
OleDbCommand cmd = new OleDbCommand(sqlAdd, con);
cmd.ExecuteNonQuery();
}
table.Dispose();
con.Close();
con.Dispose();
}

3.12.08

Load cursor from file - using user32.dll

Đảm bảo Cursor màu mè rất đẹpsengihnampakgigi
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
using System;

namespace myNamespace
{
public class cCursor
{
[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string filename);
public static Cursor GetColorCursor(string pathCursor)
{
Cursor mycursor = new Cursor(Cursor.Current.Handle);
//dinosau2.ani is in windows folder??
IntPtr colorcursorhandle = LoadCursorFromFile(pathCursor);
mycursor.GetType().InvokeMember("handle", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField, null, mycursor, new object[] { colorcursorhandle });
return mycursor;
}

}
}