Recent Post

Thursday, March 28, 2013

Kiểm tra tính đúng đắn dữ liệu trong datagridview

by Unknown  |  at  12:53 AM

Nhiều lúc cần làm thao thác dữ liệu trên datagridview để cho trực quan,và bạn muốn kiểm tra dữ liệu lun trên đó thì có các là đơn giản như sau :
//bắt sự kiện sửa đổi trong datagridview
private void dgvBC34_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
        {
            //kiểm tra cột cần bắt sự kiện ở đây là cột thứ 2
            if (this.dgvBC34.CurrentCellAddress.X == dgvBC34.Columns[2].DisplayIndex)
            {
                TextBox txt = e.Control as TextBox;
                txt.Name = "SoChuyenThu";
                if (txt != null)
                {
                    txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress);
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);                
                }
            }
      //kiểm tra cột thứ 3
         if (this.dgvBC34.CurrentCellAddress.X == dgvBC34.Columns[3].DisplayIndex)
            {
                TextBox txtsotui = e.Control as TextBox;
                txtsotui.Name = "SoTui";
                if (txtsotui != null)
                {
                    txtsotui.KeyPress -= new KeyPressEventHandler(txtsotui_KeyPress);
                    txtsotui.KeyPress += new KeyPressEventHandler(txtsotui_KeyPress);
                 
                }
            }  
         
        }  
// kiểm tra dữ liệu nè :

  private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kim tra cot
            TextBox txt = (TextBox)sender;
            if (txt.Name == "SoChuyenThu")
            {
                //Kiem tra do dai txtbox ko được nhấp wa 4 kí tự
                if (txt.Text.Length < 4)
                {
                    e.Handled = false;                

                }
                else {
              //không cho nhập hơn 4 kí tự luôn hehe ^^
                    if (e.KeyChar == (Char)Keys.Delete || e.KeyChar == (Char)Keys.Back)
                        e.Handled = false;
                    else
                    e.Handled = true;

                }

            }
        }


//Bạn có thể chỉ cho nhập số như sau :
// đối với sự kiện của cột thứ 3

 private void txtsotui_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kim tra cot
            TextBox txt = (TextBox)sender;
            if (txt.Name == "SoTui")
            {
                //Kiem tra do dai txtbox
                if (txt.Text.Length < 3)
                {
                    e.Handled = false;

                    //Là các ký t? s? ho?c di?u khi?n
                    if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }

                }
                else
                {
                    if (e.KeyChar == (Char)Keys.Delete || e.KeyChar == (Char)Keys.Back)
                        e.Handled = false;
                    else
                        e.Handled = true;
                }

            }
        }

0 comments:

Proudly Powered by Blogger.