• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张纯睿
博客园    首页    新随笔    联系   管理    订阅  订阅

dataGridView 控件快速绑定数据源后,简单的代码实现


private void dataGridView_Kuaidi_RowLeave(object sender, DataGridViewCellEventArgs e)
{
// 注意下面四局缺一不可,
this.Validate(); // 如果没有这句话,最后一个单元格的编辑,不会提交
this.kuaidiBindingSource.EndEdit();
int nUpdatedRows = this.kuaidiTableAdapter.Adapter.Update(this.clothesShopDataSet);

clothesShopDataSet.AcceptChanges(); // 由于随后要对数据库操作,没有这句话会报错:“违反并发性 UpdateCommand 影响了预期1条记录中的0条”
}

private void dataGridView_Kuaidi_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
{
if (e.ColumnIndex < 0)
{
// 行头的ContextMenuStrip设定
e.ContextMenuStrip = this.contextMenuStrip1;
dataGridView_Kuaidi.CurrentCell = dataGridView_Kuaidi[0, e.RowIndex];
}
}

private void ToolStripMenuItem_Del_Click(object sender, EventArgs e)
{
int CurrentRowIndex = dataGridView_Kuaidi.CurrentCell.RowIndex;
DataGridViewRow row = dataGridView_Kuaidi.Rows[CurrentRowIndex];
dataGridView_Kuaidi.Rows.Remove(row);

//防止滚动条滚到不是想要到的地方。
dataGridView_Kuaidi.CurrentCell = dataGridView_Kuaidi[0, CurrentRowIndex];
}

private void dataGridView_Kuaidi_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if (!e.Row.IsNewRow)
{
DialogResult response = MessageBox.Show("确定删除该行?", "删除行?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (response == DialogResult.No)
e.Cancel = true;
}
}

posted @ 2012-09-11 11:08  张纯睿  阅读(172)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3