如何锁定datagridview? [英] How to lock datagridview?

查看:154
本文介绍了如何锁定datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了包含Datagridview的表单。

Datagridview包含每月员工的开始时间和结束时间列。

我想防止用户在过去几个月将任意行插入datagridview

我怎样才能实现?

I've created form containing Datagridview.
The Datagridview contains "Start Time" and "End Time" columns for employees for every month.
I want to prevent user from insert any row to datagridview at past months.
How can I achive that?

推荐答案

你可以签到例如 SelectionChanged 该行是否属于过去几个月,如果是,只需设置 DataGridView.AllowUserToAddRows属性 [ ^ ]为假。



如果您想允许添加行,请不要忘记将其设置为true:)
You can check in e.g. SelectionChanged whether the row belongs to the past months and if yes just set DataGridView.AllowUserToAddRows Property[^] to false.

Don't forget to set it to true in case you want to allow the adding of the row :)


我相信你想要实现的只是让某些列只读。这可以这样做..

I believe what you want to achieve is to make only certain columns read only. This could be done as so..
dataGridView1.Columns["pastmonth"].ReadOnly = true;


 You need to write this code in onRowDataBoundEvent

protected void grd_OnRowDataBound(object sender, GridviewEventArgs e)
{
DateTime lblEndDate = IRDateUtils.ConvertToDate(End Time); 
                DateTime currentDate = IRDateUtils.ConvertToLocal(Convert.ToDateTime(DateTime.Now.ToShortDateString()));

       if (lblEndDate < currentDate) // here we are comparing end time with current time
         {
              // here show an alert you cant edit this record because time is expired
         }
}


这篇关于如何锁定datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆