站內文章

顯示具有 C#-WinForm應用 標籤的文章。 顯示所有文章
顯示具有 C#-WinForm應用 標籤的文章。 顯示所有文章

2014年11月14日 星期五

datagridview 自動填滿、及排序


    // 顯示DataGridview1
    using (SqlDataAdapter da = new SqlDataAdapter("Select Company_Number AS 公司編號  ,Date AS 日期 , " +
        " Case when Path = 1 then '入庫' when Path = 0 then '出庫' END AS 進出庫 , " +
        " Case when Authorize = 1 then '成功' when Authorize = 0 then '失敗' END AS  驗證 , " +
        " Case when State = 1 then '舊品' when State = 0 then '新品' END AS  新舊 " +
        " From DoorPort", conn))
    {
        DataTable sqldt = new DataTable();
        da.Fill(sqldt);
        dataGridView1.DataSource = sqldt;
        // datagridview 以 入庫時間排序 (最新一筆在最上面)
        dataGridView1.Sort(dataGridView1.Columns[1], System.ComponentModel.ListSortDirection.Descending);
        // gridview自動充滿顯示區
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    }

2014年11月4日 星期二

隱藏Winform中的最大化、最小化、關閉按鈕


網路上有兩種方式,一種是利用把按鈕變成灰色(不能按)
另一種則是讓它消失


讓按鈕變灰色
// 參考來源 RiCo技術農場
// http://www.dotblogs.com.tw/ricochen/archive/2009/10/22/11199.aspx

[DllImport("user32.dll")]
public static extern int GetSystemMenu(Int32 hWnd, Int32 bRevert);
[DllImport("user32.dll")]
public static extern int RemoveMenu(Int32 hMenu, Int32 nPosition, Int32 wFlags);
 
private void Form1_Load(Object sender, EventArgs e)
{
    Int32 ControlBoxMenu;
    const Int32 MF_BYPOSITION = 1024;//&H400
    ControlBoxMenu = GetSystemMenu(Handle.ToInt32(), 0);
    RemoveMenu(ControlBoxMenu, 6, MF_BYPOSITION);        
}


讓按鈕整個消失

將Winform的ControlBox的屬性設置成false即可

2014年10月27日 星期一

讓Form 2 Show出來的時候跟Form 1位置一樣



                                // 跳轉到 homepage 
                                this.Hide(); 
                                HomePage home = new HomePage(); // 把要跳轉過去的頁面new出來 -> home
                                // 把目前頁面位置存到home page
                                home.StartPosition = FormStartPosition.Manual;
                                home.Location = new Point(this.Location.X, this.Location.Y);
                                home.Show();

將PC檔案移動至PDA中


原本是想直接從本機讀取PDA裡面的資料庫,結果好像目前還沒有方式能找到PDA路徑
後來就找到了OpenNETCF這個方式,可以copy檔案至PDA 或者從PDA copyfile到PC
雖然說多了一個copy的步驟,但總算解決了要讀到PDA裡面檔案的問題

我的方法用到 OpenNETCF.Desktop.Communication
網路上目前也有很棒的教學(我是看這篇才會的@@) 點我前往
網站上是教你從 OpenNETCF Desktop Communication Library 開始編譯
有興趣就可以上去看看

這邊我直接附上編譯好的 dll  點我下載


                using OpenNETCF.Desktop.Communication; // 需引入 .dll

                string path = @"C:\Documents and Settings\lict\My Documents\WindowsCE My Documents\InBoundPDA.sdf";
                RAPI m_rapi = new RAPI(); ; // 宣告 RAPI 物件
                m_rapi.Connect(true);  // 建立與裝置的同步連接
                // 判斷是否連線
                try
                {
                    if (!m_rapi.Connected)
                    {
                        MessageBox.Show("尚未與裝置連線!");
                        return;
                    }
                    // 將下載的檔案(原本在本機) copy 到 PDA上
                    m_rapi.CopyFileToDevice(path, @"\DB\InBoundPDA.sdf", true); // PC的檔案複製到裝置
                    m_rapi.Disconnect();  // 中斷與裝置的連接
                    File.Delete(path); // 刪除本機檔案
                    MessageBox.Show("下載完成!");

                    // 將PDA的檔案 copy 到 電腦上
                    // m_rapi.CopyFileFromDevice(path, @"\DB\FixTest.sdf", true);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }


附上libray的使用方式
Connect 建立與裝置的同步連接。
CopyFileFromDevice 將裝置中的檔案複製到 PC。
CopyFileOnDevice 將裝置某個位置的檔案複製到裝置的另一新位置。
CopyFileToDevice 將 PC 中的檔案複製到裝置。
CreateDeviceDirectory 在裝置中創建目錄。
CreateProcess 啟動裝置中的應用程式。
DeleteDeviceFile 刪除裝置中的檔案。
DeviceFileExists 檢查裝置中是否存在檔案。
Disconnect 中斷與裝置的連接。
EnumFiles 提供與 FileName 參數提供的條件相匹配的 FileInformation 清單。
GetDeviceCapabilities 檢索裝置的特定裝置訊息。
GetDeviceFileAttributes 檢索特定裝置檔案的屬性。
GetDeviceFileSize 檢索裝置檔案的大小,以位元組為單位。
GetDeviceFileTime 檢索裝置檔案的日期時間。
GetDeviceMemoryStatus 檢索裝置的記憶體狀態。
GetDeviceSystemFolderPath 檢索到裝置系統檔案夾的路徑。
GetDeviceSystemInfo 檢索裝置的系統詳細訊息。
GetDeviceSystemPowerStatus 檢索裝置的電源狀態。
GetDeviceVersion 檢索裝置的作業系統版本。
MoveDeviceFile 將現有裝置檔案移到或重命名到一個新位置。
RemoveDeviceDirectory 刪除裝置中的目錄。
SetDeviceFileAttributes 設定裝置中檔案的屬性。
SetDeviceFileTime 設定裝置中檔案的日期時間。


文末附上之前看到可以找執行檔案位置的方法
原本還以為這是可以抓PDA的位置-.- 結果不行
不過他能抓到PDA的執行檔案位置(在PDA執行時),如果再PC執行則是抓PC的執行檔位置


                foreach (string dir in Directory.GetDirectories("\\"))
                {
                    if (((new DirectoryInfo(dir)).Attributes & FileAttributes.NotContentIndexed) > 0)
                    {
                        // dir 即是 執行檔案的位置
                        // 可用相對路徑來取得你要的檔案位置
                    }
                }

在datagridview中產生按鈕進行點選


一開始先在pageload建立按鈕

                // 建立 PDA下載 按鈕
                // new 一個datagridview的欄位 名稱為DownPDA
                DataGridViewButtonColumn DownPDA = new DataGridViewButtonColumn(); 
                DownPDA.HeaderText = "PDA下載"; // 按鈕名稱
                DownPDA.CellTemplate.Style.BackColor = Color.Pink; // 設定按鈕顏色
                dataGridView1.Columns.Insert(8,DownPDA); // 在第8欄插入 剛建好的按鈕欄位
                (dataGridView1.Columns[8] as DataGridViewButtonColumn).DefaultCellStyle.NullValue = "PDA下載";

                // gridview自動充滿顯示區
                // 調整可參考 http://www.cnblogs.com/scottckt/archive/2007/10/09/917874.html
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

建一個datagridview的 CellClick (接受點擊事件)


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 如果點了第8欄則進行
            if (e.ColumnIndex == 8)
            {
                // 抓取點選那一行的 第1欄位的資料 (補充:欄位是從0起始開始數)
                string DeviceLotNM = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                // 抓取點選那一行的 第2欄位的資料
                string DeviceCount = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            }
        }