//store the selected row index.
private int selectedRowIndex = 0;
public Form1() {
InitializeComponent();
}
//Initialize the Data.
private void Form1_Load(object sender, EventArgs e) {
string strConnection = @"Data Source=.\SQLExpress;Initial Catalog=DB_Person;Integrated Security=True";
using(SqlConnection connect = new SqlConnection(strConnection)) {
SqlCommand cmd = connect.CreateCommand();
cmd.CommandText = "select * from [T_Employee]";
DataSet set = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(set, "T_Employee");
dataGV.DataSource = set.Tables["T_Employee"]; //specify the DataSource.
dataGV.ReadOnly = true; //read only not work when trying to delete or add rows.
//can not add or delete rows.
dataGV.AllowUserToAddRows = false;
dataGV.AllowUserToDeleteRows = false;
}
}
//Try to delete the DataGridView row.
private void btn_DeleteRow_Click(object sender, EventArgs e) {
dataGV.Rows.Remove(dataGV.Rows[selectedRowIndex]);
}
//Get the index of the selected row.
private void dataGV_CellClick(object sender, DataGridViewCellEventArgs e) {
selectedRowIndex = dataGV.CurrentCell.RowIndex;
}
//我做的测试很简单.但是我不解的是,怎么还是可以删除行...
private int selectedRowIndex = 0;
public Form1() {
InitializeComponent();
}
//Initialize the Data.
private void Form1_Load(object sender, EventArgs e) {
string strConnection = @"Data Source=.\SQLExpress;Initial Catalog=DB_Person;Integrated Security=True";
using(SqlConnection connect = new SqlConnection(strConnection)) {
SqlCommand cmd = connect.CreateCommand();
cmd.CommandText = "select * from [T_Employee]";
DataSet set = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(set, "T_Employee");
dataGV.DataSource = set.Tables["T_Employee"]; //specify the DataSource.
dataGV.ReadOnly = true; //read only not work when trying to delete or add rows.
//can not add or delete rows.
dataGV.AllowUserToAddRows = false;
dataGV.AllowUserToDeleteRows = false;
}
}
//Try to delete the DataGridView row.
private void btn_DeleteRow_Click(object sender, EventArgs e) {
dataGV.Rows.Remove(dataGV.Rows[selectedRowIndex]);
}
//Get the index of the selected row.
private void dataGV_CellClick(object sender, DataGridViewCellEventArgs e) {
selectedRowIndex = dataGV.CurrentCell.RowIndex;
}
//我做的测试很简单.但是我不解的是,怎么还是可以删除行...
我上代码只是为了方便...
搞不懂为什么设置 DataGridView的属性 AllowUserToAddRows 和 AllowUseToDeleteRows属性设置为false后,还能删除....
AllowUseToDeleteRows属性只是在DataGridView里面执行行删除,那是它本身带有的功能,就像它的排序一样,而你通过删除按钮来删除这样肯定可以,所以说你禁止的只是DataGridView里面的删除功能
非DataGridView内操作是不控制的。
AllowUserToAddRows这个属性应该是对对代码约束吧?
这个测试貌似又麻烦了一点...
Add 和Delete属性都一样,对代码无效的?
这两个都一样么?...
AllowUserToDeleteRows和AllowUserToAddRows是针对用户体验的!!