DataGridView 無綁定資料庫與自訂顯示

1 篇文章 / 0 new
author
DataGridView 無綁定資料庫與自訂顯示
public partial class Form1 : Form
{
    #region "bitmaps"
    private byte[] myImage = new byte[] {
        0x42, 0x4D, 0xC6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x76,
        0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0xB, 0x0, 0x0, 0x0, 0xA,
        0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x50,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10,
        0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x80, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x80, 0x80, 0x0,
        0x80, 0x0, 0x0, 0x0, 0x80, 0x0, 0x80, 0x0, 0x80, 0x80, 0x0,
        0x0, 0xC0, 0xC0, 0xC0, 0x0, 0x80, 0x80, 0x80, 0x0, 0x0, 0x0,
        0xFF, 0x0, 0x0, 0xFF, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xFF,
        0x0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0x0,
        0xFF, 0xFF, 0xFF, 0x0, 0xFF, 0xFF, 0x0, 0xF, 0xFF, 0xF0,
        0x0, 0x0, 0xFF, 0x0, 0xFF, 0xF0, 0xF, 0xF0, 0x0, 0x0, 0xF0,
        0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF0, 0xFF, 0xFF,
        0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF, 0xFF, 0xFF, 0xFF, 0xFF,
        0x0, 0x0, 0x0, 0xF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0,
        0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xF0, 0xFF,
        0xFF, 0xFF, 0xF0, 0xF0, 0x0, 0x0, 0xFF, 0x0, 0xFF, 0xF0,
        0xF, 0xF0, 0x0, 0x0, 0xFF, 0xFF, 0x0, 0xF, 0xFF, 0xF0, 0x0,
        0x0};
    #endregion
    private bool _CheckChange = true;
 
    public Form1()
    {
        InitializeComponent();
        this.dateTimePicker1.Height = this.GV_List.Height;
        this.cbPartType.SelectedIndex = 0;
    }
 
    private void btnAdd_Click(object sender, EventArgs e)
    {
        Bitmap blank;
        DataGridViewRowCollection rows = this.GV_List.Rows;
 
        //blank = new Bitmap(new MemoryStream(myImage));
        //blank = new Bitmap(Image.FromFile("d:\\vs\\01.jpg"));//載入圖形
        blank = (new Icon(@"d:\eXtplorer.ico")).ToBitmap();//載入ICON
        rows.Add(new Object[] {
            this.tbName.Text, this.numPrice.Value, this.dateTimePicker1.Text,
            this.cbPartType.Text, "yes", "button", blank});
    }
 
    private void GV_List_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (GV_List.CurrentCell.Value == null) return;
        //
        string fieldName = this.GV_List.Columns[e.ColumnIndex].Name.ToLower();
        Rectangle r = this.GV_List.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
        r = this.GV_List.RectangleToScreen(r);
        Point location = this.RectangleToClient(r).Location;
        //
        this._CheckChange = true;
        if (fieldName == "date")
        {//自訂型態為 DateTimePicker
            fieldName.Split(' ');
            this.dateTimePicker1.Location = location;//this.RectangleToClient(r).Location;
            this.dateTimePicker1.Size = r.Size;
            this.dateTimePicker1.Text = this.GV_List.CurrentCell.Value.ToString();
            this.dateTimePicker1.Visible = true;
        }
        else
        {
            this.dateTimePicker1.Visible = false;
        }
        if (fieldName == "price")
        {//自訂型態為 NumericUpDown
            this.cellNumber.Location = location;
            this.cellNumber.Size = r.Size;
            this.cellNumber.Value = Int32.Parse(this.GV_List.CurrentCell.Value.ToString());
            this.cellNumber.Visible = true;
        }
        else
            this.cellNumber.Visible = false;
        //
        this._CheckChange = false;
    }
 
    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        if (_CheckChange) return;
        this.GV_List.CurrentCell.Value = this.dateTimePicker1.Text;
 
    }
 
    private void cellNumber_ValueChanged(object sender, EventArgs e)
    {
        if (_CheckChange) return;
        this.GV_List.CurrentCell.Value = this.cellNumber.Value;
    }
 
    private void GV_List_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {//DataGridViewButtonColumn type 的觸發            
        //MessageBox.Show(this.GV_List.CurrentCell.GetType().ToString());
        //非 DataGridViewTextBoxColumn 形態的控制
        if (this.GV_List.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
        {
            ((DataGridViewButtonColumn)this.GV_List.Columns[e.ColumnIndex]).Text = "完成";
        }
        //取得選取 row 的某 cell 資料
        //MessageBox.Show(this.GV_List.CurrentRow.Cells[0].Value.ToString());
        //focus cell
        //MessageBox.Show(this.GV_List.CurrentCell.Value.ToString());
    }
}
Free Web Hosting