asp.net datagridview按上下键添加删除行

         private bool IsPost = false;
        private int ColIndex = -1;
        private void dgvMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 40)//下
            {
                dgvMessage.Rows.Add();
                ColIndex = dgvMessage.CurrentCellAddress.X;
                IsPost = true;
            }
             else if (e.KeyValue == 38)  //上
            {
                if (dgvMessage.Rows.Count > 1)
                {
                    dgvMessage.Rows.RemoveAt(dgvMessage.Rows.Count - 1);
                    ColIndex = dgvMessage.CurrentCellAddress.X;
                    IsPost = true;
                }

            }
        }
        private void dgvMessage_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
             if (IsPost)
             {
                 this.dgvMessage.CurrentCell = this.dgvMessage.Rows[dgvMessage.Rows.Count - 1].Cells[ColIndex];
                 IsPost = false;
             }
        }

posted on 2015-08-12 19:34  乖乖的小莹儿  阅读(265)  评论(0编辑  收藏  举报