使用到的对象:
1、 DataGridView: dataGridView1
2、 BindingNavigator: bindingNavigator1(自带添加按钮btnAdd、删除按钮btnDelete)
3、 ToolStripButton: btnCancelEdit(添加到bindingNavigator1之中)
使用到的事件:
/// <summary>
/// 单元格的值改编后,执行更新、或插入操作;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//如果关键字段"type",说明是在编辑新行的其它字段的值,不需要做如何操作;
string typeTemp = dataGridView1.Rows[e.RowIndex].Cells["type"].FormattedValue.ToString();
if (typeTemp == null || typeTemp.Trim().Length == 0) return;
//
string sqlStr = "select count(*) from Coupler where type='"+
dataGridView1[0, e.RowIndex].FormattedValue.ToString() + "' ";
if (ClsDataBaseOperator.execteCount(sqlStr) < 1)
{
sqlStr = "insert into Coupler(type) values('" +
dataGridView1[0, e.RowIndex].FormattedValue.ToString() + "')";
ClsDataBaseOperator.executeGetLines(sqlStr);
}
else
{
sqlStr = "update Coupler set type='" + dataGridView1[0, e.RowIndex].FormattedValue.ToString() +
"', PHS1900=" + dataGridView1.Rows[e.RowIndex].Cells["PHS1900"].FormattedValue.ToString() +
", DCS1800=" + dataGridView1.Rows[e.RowIndex].Cells["DCS1800"].FormattedValue.ToString() +
", GSM900=" + dataGridView1.Rows[e.RowIndex].Cells["GSM900"].FormattedValue.ToString() +
", CDMA800=" + dataGridView1.Rows[e.RowIndex].Cells["CDMA800"].FormattedValue.ToString() +
", other=" + dataGridView1.Rows[e.RowIndex].Cells["other"].FormattedValue.ToString() +
", IsDefault=" + dataGridView1.Rows[e.RowIndex].Cells["IsDefault"].FormattedValue.ToString() +
" where type='" + dataGridView1[0, e.RowIndex].FormattedValue.ToString() + "'";
ClsDataBaseOperator.executeGetLines(sqlStr);
}
}
/// <summary>
/// 当有单元格进入编辑状态时,需要打开“撤销编辑”按钮的可点击状态;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
this.btnCancelEdit.Enabled = true;
}
/// <summary>
/// 删除一条记录,删除表格的的当前行,并更新数据库;
/// btnDelete为navigator自带的按钮,需要添加下面事件;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
string sqlStr = "delete from Coupler where type='" +
dataGridView1.CurrentRow.Cells["type"].FormattedValue.ToString() + "' ";
}
/// <summary>
/// 撤销修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEdit_Click(object sender, EventArgs e)
{
dataGridView1.CancelEdit();
dataGridView1.EndEdit();
}
/// <summary>
/// 添加新的一行;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells["PHS1900"].Value = "0";
dataGridView1.CurrentRow.Cells["DCS1800"].Value = "0";
dataGridView1.CurrentRow.Cells["GSM900"].Value = "0";
dataGridView1.CurrentRow.Cells["CDMA800"].Value = "0";
dataGridView1.CurrentRow.Cells["other"].Value = "0";
dataGridView1.CurrentRow.Cells["type"].Selected = true;
dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells["type"];
}