关于Winform中对DataGridView实现在输入数据时定位在某一行的心得总结

在winform有时需要直接用DataGridView进行输入,但是在输入时不希望数据行上下滚动,只能定位在当前。

在编修数据时有时是新增一行会在最后一行,有时只是编修其中的某一行,可以用如下方法实现。

1新增时的代码如下,新增后把最后一行定位为当前行:

  private void tbAdd_Click(object sender, EventArgs e)
        {
            DataGridView_DIY.SetGridColumnsReadonly(dgvRole_Group, Can_EditCols, false);
            string msgError = "";
            if (dsP_ROLEGroup_M.Tables.Count == 0)
            {
                dsP_ROLEGroup_M = Query_P_ROLEGroup_M(" and 1<>1", out msgError);
                bsP_ROLEGroup_M.DataSource = dsP_ROLEGroup_M.Tables["P_ROLEGroup_M"];//綁定器的數據源等於數據集的某個表
                dgvRole_Group.DataSource = bsP_ROLEGroup_M;// 控件的數據源等於綁定器

            }

            DataRow NewRow;
            NewRow = dsP_ROLEGroup_M.Tables["P_ROLEGroup_M"].NewRow();
            NewRow["Created_by"] = Program.clUser.P_User.U_ID;
            NewRow["Creation_date"] = ClientDataBase.GetDateTime_DataBase(out msgError);
            NewRow["Last_update_by"] = Program.clUser.P_User.U_ID;
            NewRow["Last_update_date"] = NewRow["Creation_date"];
            dsP_ROLEGroup_M.Tables["P_ROLEGroup_M"].Rows.Add(NewRow);
            dgvRole_Group.CurrentCell = dgvRole_Group.CurrentRow.Cells["G_DM"];
            bsP_ROLEGroup_M.MoveLast();
            dgvRole_Group.BeginEdit(true);
            Load_ckRoles_Permits("");
            base.comdAdd();
            Refresh_ToolBtn();
        }

2 如果是修改数据某一行时,在修改前要把当前的行号用变量保存起来如下代码,当修改完,在保存命令中,需要把该变量值改为-1,后面需要重新修改某一行时又会重新得到当前行号。

 int Selectedindex = 0;//用来存放编修的行号
        private void tbEdit_Click(object sender, EventArgs e)
        {
            DataGridView_DIY.SetGridColumnsReadonly(dgvRole_Group, Can_EditCols, false);
            Selectedindex = dgvRole_Group.CurrentRow.Index;//取当前要编修的行号       
            base.comdEdit();
            Refresh_ToolBtn();
        }


然后在DataGridView的SelectionChanged实件中实现不让它滚动。

Int64 curr_index = 1;//只有上下移動光標時才會執行下面相關過程
        private void dgvRole_Group_SelectionChanged(object sender, EventArgs e)
        {
            string msgError = "";
            string msgMessage = "";
            //滚动数据时检加载数据
            if (dgvRole_Group.Rows.Count > 0 && dgvRole_Group.CurrentRow != null && curr_index != dgvRole_Group.CurrentRow.Index)
            {
                int curCell = dgvRole_Group.CurrentCell.ColumnIndex;//得到当前列
                if (isInNew && dgvRole_Group .CurrentRow .Index !=dgvRole_Group .Rows.Count )//新增时不能滚动
                {
                    dgvRole_Group.CurrentCell = dgvRole_Group.CurrentRow.Cells[curCell];
                    bsP_ROLEGroup_M.MoveLast();
                    
                    return;
                }
               
                if(isInEdited && Selectedindex >=0 && dgvRole_Group .CurrentRow .Index !=Selectedindex )//修改时不能滚动
                {
                    dgvRole_Group.CurrentCell = dgvRole_Group.Rows[Selectedindex].Cells[curCell];
                    
                    return;
                }
                if (dsRole_Perimts != null && !isInBrowse)//如果在修改时滚动时要保存修改的值
                {
                    DataSet ds1 = dsRole_Perimts.GetChanges();
                    if (ds1 != null)
                    {
                        if (SaveP_ROLEGroup_D(ds1, out msgError))
                        {
                            dsRole_Perimts.AcceptChanges();
                        }
                        else
                        {
                            if (msgError != "")
                            {
                                msgMessage = "出错啦,出错原因:" + "\n\r" + msgError;
                                MessageBox.Show(msgMessage, "Error");
                                return;
                            }
                        }
                    }
                }
                curr_index = dgvRole_Group.CurrentRow.Index;
                Load_ckRoles_Permits(dgvRole_Group.CurrentRow.Cells["G_ID"].Value.ToString());
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值