2011-07-27

DataGrid – Committing changes cell-by-cell

Private isManualEditCommit As Boolean = False
    Private Sub DG_CellEditEnding(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridCellEditEndingEventArgs) Handles DG.CellEditEnding
        If Not isManualEditCommit Then
            isManualEditCommit = True
            DG.CommitEdit(DataGridEditingUnit.Row, True)
            isManualEditCommit = False
        End If
    End Sub

2011-07-26

Get DataGrid row and cell

There are no simple built-in methods for accessing individual rows or columns in WPF DataGrid. The following code samples will provide simple methods for accessing these items.
First of all we need a helper function for selecting a visual child:
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
       if (child != null)
       {
           break;
       }
   }
       return child;
}
There is a simple method for getting the current (selected) row of the DataGrid:
public static DataGridRow GetSelectedRow(this DataGrid grid)
{
    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}
We can also get a row by its indices:
public static DataGridRow GetRow(this DataGrid grid, int index)
{
    DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // May be virtualized, bring into view and try again.
        grid.UpdateLayout();
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}
Now we can get a cell of a DataGrid by an existing row:
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
    if (row != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        if (presenter == null)
        {
            grid.ScrollIntoView(row, grid.Columns[column]);
            presenter = GetVisualChild<DataGridCellsPresenter>(row);
        }

        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        return cell;
    }
    return null;
}
Or we can simply select a row by its indices:
public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
    DataGridRow rowContainer = grid.GetRow(row);
    return grid.GetCell(rowContainer, column);
}
The functions above are extension methods. Their use is simple:
var selectedRow = grid.GetSelectedRow();
var columnCell = grid.GetCell(selectedRow, 0);

2011-03-23

Import from EXCEL which within columns content length greater than 255

當使用Enterprise manager要從Excel import資料時, 常常遇到當某些欄位內容長度大於255時會造成import失敗
在系統註冊表內
x86 : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel
x64: HKLM\SOFTWARE\wow6432node\microsoft\jet\4.0\engines\excel
在import時會根據TypeGuessRows的值來判斷要在SQL中建立多大的欄位
預設值是8, import工具會根據excel裏的前8行來判斷該建立什麼欄位
TypeGuessRows的有效值是0~16, 當設為0時, 會檢查前16384行的資料, 當在匯入大筆資料時, 可能會對效能產生影響
除了修改註冊表之外, 比較簡單的方式是將有超出255字元的資料移到EXCEL的前8筆, 這樣IMPORT工具會可以判斷而將欄位建立為Nvarchar(Max)

2011-03-04

如何於泛型處理常式 .ashx 中存取工作階段變數(Session Variable)

要能夠在泛型處理常式中存取工作階段變數(Session Variable),其類別必須實作System.Web.SessionState 命名空間中的 IRequiresSessionState 介面
using System;
using System.Web;
using System.IO;
using System.Web.SessionState;
public class confirm : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
MemoryStream gdata = new MemoryStream();
BuildImage bd_img = new BuildImage();
string mdata = “";
mdata = context.Session["confirm"].ToString();
// 取得驗證圖型的資料(設定驗證圖型尺寸及驗證字串)
gdata = bd_img.GenerateImage(200, 54, mdata);
// 設定輸出格式
context.Response.ContentType = “image/png";
// 送出資料內容
context.Response.BinaryWrite(gdata.ToArray());
}
public bool IsReusable
{
get
{
return false;
}
}
}