2011년 12월 16일 금요일

How to keep expanded state in RadTreeListView with MVVM

////// Model

public class Task

{

public int id;

public string name;

public ObservableCollection childrens;

}


////// ViewModel


pubilc class TaskViewModel

{

// Define dictionary for storing expanded state

// int: id of item of RadTreeListView

// bool: expanded state, true(expanded) false(folded)

private Dictionary _Expanded;


// Define Get/Set method

public void SetExpanded(int id, bool expanded)

{

if (_Expanded.ContainsKey(id))

{

_Expanded[id] = expanded;

}

else

{

_Expanded.Add(id, expanded);

}

}


public bool GetExpanded(int id)

{

if (_Expanded.ContainsKey(id))

{

return _Expanded[id];

}

return false;

}

}


////// View


// store expanded state when it is changing

private void radTreeListView_RowIsExpandedChanging(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)

{

TaskViewModel viewModel = this.DataContext as TaskViewModel;

Task task = e.Row.DataContext as Task;

TreeListViewRow row = (TreeListViewRow)TemplateGrid.ItemContainerGenerator.ContainerFromItem(e.Row.Item);

if (row != null && task != null)

viewModel.SetExpanded(task.id, row.IsExpanded);

}


// Set expanded state in DataLoaded event

private void radTreeListView_DataLoaded(object sender, EventArgs e)

{

var source = radTreeListView.ItemsSource as ObservableCollection;

SetExpanded(source)

}


// Recursive method for self-hierarchy collection

private void SetExpanded(ObservableCollection tasks)

{

if (tasks == null)

return;


TaskViewModel viewModel = this.DataContext as TaskViewModel;

foreach (Task task in tasks)

{

if (task.children == null || task.children.Count == 0)

return;


SetExpanded(task.children);


if (viewModel != null)

{

if (viewModel.GetExpanded(task.id))

radTreeListView.ExpandHierarchyItem(task);

else

radTreeListView.CollapseHierarchyItem(task);

}

}

}

2011년 11월 7일 월요일

Set Excel File As A Database in MS-SQL

1. Requirements

- Microsoft Access Database Engine 2010

(If you are on the Windows 64bit, you need both Microsoft Access Database 64bit and Microsoft Office 64 bit)

Download - http://www.microsoft.com/download/en/details.aspx?id=13255


2. Execute Stored Procedure

USE msdb
GO
EXEC sp_addlinkedserver
 @server = 'EXCEL_DB',                              
 @srvproduct = 'ACE 12.0',
 @provider = 'Microsoft.ACE.OLEDB.12.0',
 @datasrc = 'C:\FileName.xls',                // full path of excel file
 @provstr = 'Excel 12.0; HDR=Yes'         // HDR=yes, when excel file has header row

Table name is set by sheet name of excel file.


3. How To Access

- SELECT

SELECT * FROM EXCEL_DB...[Customers$]

- IMPORT

SELECT * INTO XLImport1 FROM EXCEL_DB...[Customers$]

2011년 8월 25일 목요일

desc equivalent in MS SQL

sp_columns 'table_name'

MS-SQL table level backup

export:
bcp "select * from [MyDatabase].dbo.Customer " queryout "Customer.bcp" -N -S localhost -T -E

import:
bcp [MyDatabase].dbo.Customer in "Customer.bcp" -N -S localhost -T -E

2011년 8월 17일 수요일

Rename column name in MS-SQL

exec sp_rename 'TABLE_NAME.[ORIGINAL COLUMN_NAME]', 'NEW COLUMN_NAME', 'column'

2011년 7월 20일 수요일

Config file for Silverlight

1. Use ServiceReferences.ClientConfig file like app.config in C# application

- Add configuration in ServiceReferences.ClientConfig

eg)

<configuration>
<appSettings>
<add key="MAX_UPLOAD_SIZE" value="3145728" />
</appSettings>
<system.serviceModel>
....
</system.serviceModel>
</configuration>


2. Create your own ConfigurationManager
- Add reference System.Xml.Linq;

eg)

public static class ConfigurationManager
{
static ConfigurationManager()
{
AppSettings = new Dictionary();
ReadSettings();
}

public static Dictionary AppSettings { get; set; }

private static void ReadSettings()
{
string strKey = string.Empty;
string strValue = string.Empty;
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = new XmlXapResolver();
XmlReader reader = XmlReader.Create("ServiceReferences.ClientConfig");
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "add")
{
if (reader.HasAttributes)
{
strKey = reader.GetAttribute("key");
if (!string.IsNullOrEmpty(strKey))
{
strValue = reader.GetAttribute("value");
AppSettings.Add(strKey, strValue);
}
}
}
}
}
}

3. Create property in your class

eg)

public class Consts
{
public Int64 MAX_UPLOAD_SIZE
{
get
{
string MaxUploadSize = ConfigurationManager.AppSettings["MAX_UPLOAD_SIZE"];
return int.Parse(MaxUploadSize);
}
}
}

4. Create instance in App.xaml

eg)
<local:Consts x:Key="Consts" />

5. Bind with StaticResource

eg)
<telerik:RadUpload MaxFileSize="{Binding Source={StaticResource Consts}, Path=MAX_UPLOAD_SIZE}" />

2011년 7월 14일 목요일

2011년 3월 7일 월요일

파일 첨부해서 이메일 보내기

function mail_attachment($from , $to, $subject, $message, $attachment)
{
$fileatt = $attachment; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
// $start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
// $fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment
$fileatt_name = $attachment;

$email_from = $from; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it

$email_to = $to; // Who the email is to

$headers = "From: ".$email_from."\r\n";
//$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$msg_txt="";

$semi_rand = md5(time());
$mime_boundary = "Multipart_Boundary_x{$semi_rand}x";
$headers .= "Bcc: kbw815@yahoo.com\r\n";
$headers .= "MIME-Version: 1.0\r\n".
"Content-Type: multipart/mixed;".
" boundary={$mime_boundary}\r\n";

$email_txt .= $msg_txt;
$email_message .= "--{$mime_boundary}\r\n".
"Content-Type:text/html; charset=utf-8\r\n\r\n".
$email_txt."\r\n\r\n";

$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\r\n".
"Content-Type: {$fileatt_type};".
" name={$fileatt_name}\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n".
$data."\r\n\r\n".
"--{$mime_boundary}--\r\n";

$ok = mail($email_to, $email_subject, $email_message, $headers);

if($ok)
{
echo "Mail is sent";
}
else
{
echo "Mail is failed";
}
}
?>