码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-08-12 21:10:34      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

         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;
             }
        }

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

标签:

原文地址:http://www.cnblogs.com/gracexy/p/4725381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!