winform中的 DataGridView 的 AllowUserToDeleteRows属性怎么设置?
//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;
}
//我做的测试很简单.但是我不解的是,怎么还是可以删除行...