基本上它的一些事件和啓用/禁用AllowUserToAddRow屬性的簡單打法:
public Form1()
{
InitializeComponent();
//creating a test DataTable and adding an empty row
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Rows.Add(dt.NewRow());
//binding to the gridview
dataGridView1.DataSource = dt;
//Set the property AllowUserToAddRows to false will prevent a new empty row
dataGridView1.AllowUserToAddRows = false;
}
現在事件... 當細胞識別的編輯就會觸發一個叫做CellBeginEdit事件。當它在編輯模式下設置的AllowUserToAddRows假
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
}
當細胞識別編輯它會觸發一個叫做CellEndEdit事件的結束。當它結束編輯模式檢查您的條件。根據結果設置AllowUserToAddRows爲true使其保持爲false。
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//instead of MessageBox there could be as well your check conditions
if (MessageBox.Show("Cell edit finished, add a new row?", "Add new row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
dataGridView1.AllowUserToAddRows = true;
else dataGridView1.AllowUserToAddRows = false;
}
它不練琴用戶定位鼠標,可能是他們通過鍵盤接口 – 2012-04-03 06:56:01