[Reconstruction of computer room]-dataGridView control application

foreword

Computer room reconstruction After completing the registration, recharge, card withdrawal, activation and other forms, I suddenly found that the operator’s operation and management of the card can be implemented on a form through the application of the dataGridView control. So I tried to write it, although some functions are repeated in this way, but it doesn’t matter, just be happy.

Student card management form

Common properties of dataGridView

  • DisplayedCells adjusts column widths to fit the contents of all cells in the row and column currently displayed on the screen, including header cells.
  • The font size of the header column of DataGridViewCellStyle is set here
  • DefaultCellStyle The font size of the non-title column is set here

Some features of dataGridView

  • Click on a column header or a cell in a column to select an entire column
  • dataGridView add right-click menu
  • dataGridView read only
  • Get the value of a cell in the selected row

Part of the function code

private void StuCardRecord_UI_Load(object sender, EventArgs e)
        {
            # region shows all student card records 
            Facade.StuCardRecord_Facade cardfact = new Facade.StuCardRecord_Facade();
            List<Entity.StuCard_Info> list = cardfact.ShowAllStuCard();
            dataGridView1.DataSource = list;
            #endregion

            # region Click a column header or a cell in the column to select the entire column       
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            #endregion           

            dataGridView1.ReadOnly = true ; //read only
        }

        # region selects a row, right-click menu 
        private  void  dataGridView1_CellMouseDown ( object sender, DataGridViewCellMouseEventArgs e )
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    //If the row is already selected, it will no longer be set 
                    if (dataGridView1.Rows[e.RowIndex].Selected == false )
                    {
                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[e.RowIndex].Selected = true;
                    }
                    //Set the active cell 
                    if only one row is selected (dataGridView1.SelectedRows.Count == 1 )
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    // pop up the action menu
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }
        #endregion            

        # region refund card

        private void BackCard_Click(object sender, EventArgs e)
        {
            //Get the card number of the selected row            
            int a = dataGridView1.CurrentRow.Index;
             int b = Convert.ToInt32(dataGridView1.Rows[a].Cells[ 0 ].Value);

            {
                MessageBox.Show( "This card has been refunded" );
            }
            {
                MessageBox.Show( "This card is being loaded, please exit first and return the card!" );
            }
            else
            {
                # region update student card - balance is 0, card has been refunded

                Entity.StuCard_Info stuCard = new Entity.StuCard_Info();
                Facade.BackCard_Facade backCard = new Facade.BackCard_Facade();

                stuCard.StuID = Convert.ToInt32(b);
                stuCard.IsBackcard = "Backcard" ;
                stuCard.Balance = Convert.ToDecimal("0");

                int table = backCard.UpdateBCstuCard(stuCard);

                #endregion

                # region delete user table 
                Entity.User_Info Userinfo = new Entity.User_Info();

                Userinfo.UserID = Convert.ToInt32(b);

                int dt1 = backCard.DeleteUserID(Userinfo);
                #endregion

                # region add card return table 
                Entity.BackCard_Info backcardinfo = new Entity.BackCard_Info();

                backcardinfo.stuID = Convert.ToInt32(b);
                backcardinfo.stuName = Convert.ToString(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value);
                backcardinfo.backMoney = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value);
                backcardinfo.backTime = DateTime.Now;
                backcardinfo.Head = Common_UI.userid;
                backcardinfo.isCheck = "Open Checkout" ;

                int backTB = backCard.AddBackCard(backcardinfo);

                if (backTB != 0)
                {
                    MessageBox.Show( "Successful card withdrawal!" );
                }
                else
                {
                    MessageBox.Show( "Failed to return the card!" );
                }
                #endregion
            }           
        }
        #endregion

        private void Recharge_Click(object sender, EventArgs e)
        {
            {
                MessageBox.Show( "This card has been returned and cannot be recharged!" );
            }
            else
            {
                //Get the card number of the selected row            
                int a = dataGridView1.CurrentRow.Index;
                 int b = Convert.ToInt32(dataGridView1.Rows[a].Cells[ 0 ].Value);

                Recharge_UI frm = new Recharge_UI();
                frm.Show();
                Recharge_UI.txtstuID.Text = Convert.ToString(b);
            }            
        }
        #endregion

        private void ReActivate_Click(object sender, EventArgs e)
        {
            MessageBox.Show( "Activate!" );
        }
        #endregion

        # region update shows all student card records 
        private  void  btnUpdate_Click ( object sender, EventArgs e )
        {          
            Facade.StuCardRecord_Facade cardfact = new Facade.StuCardRecord_Facade();

            List<Entity.StuCard_Info> list = cardfact.ShowAllStuCard();

            dataGridView1.DataSource = list;          
        }
        #endregion{{EJS0}}

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

postscript

Standing on the shoulders of giants, the integration of these functions has been completed. The above code part can be further optimized. The more excellent and powerful functions of dataGridView are still waiting for us to learn.

[Check out the meaning of the four properties of SelectionMode in datagridviewz in the blog assembly number C#] DataGridView add rightclick
menu and other skills update to database



Leave a Comment

Your email address will not be published. Required fields are marked *