private void dgvStockFirst_CellClick(object sender, DataGridViewCellEventArgs e)
{//点击单元格 获取单元格的值
a = dgvStockFirst.CurrentRow.Cells["MainTop_MyCode"].Value.ToString();
}
private void dgvStockFirst_CellValidated(object sender, DataGridViewCellEventArgs e)
{//单元格完成时验证if (e.ColumnIndex == 4)//单元格所在列
{
string b = dgvStockFirst.Rows[e.RowIndex].Cells["MainTop_MyCode"].Value.ToString();
string zi = dgvStockFirst.Rows[e.RowIndex].Cells["MainTop_MyCode"].Value.ToString();//获取输入的单元格的值
string sql = "select MainTop_MyCode from T_OutIn_MainTop where MainTop_MyCode='" + zi+ "and MainTop_ID<>" + dgvStockFirst.Rows[e.RowIndex].Cells["MainTop_ID"].Value.ToString() + "";
ds1 = DataAccess.GetDataSet(sql, false, null);
if (ds1.Tables[0].Rows.Count != 0)
{//查询数据库判断单元格值是否已存在
PublicClass.ShowMessage("当前自编号已存在!");
dgvStockFirst.Rows[e.RowIndex].Cells["MainTop_MyCode"].Value = a;
return;
}
if (dgvStockFirst.Rows[e.RowIndex].Cells["MainTop_MyCode"].Value.ToString() != a)
{//与原单元格值不同 更新数据库表
sql = "update T_OutIn_MainTop set MainTop_MyCode ='" + dgvStockFirst.CurrentRow.Cells["MainTop_MyCode"].Value.ToString() + "'where MainTop_ID=" + dgvStockFirst.CurrentRow.Cells["MainTop_ID"].Value.ToString() + "";
int n = DataAccess.ExecuteNonQuery(sql, false, null);
if (n > 0)
{
PublicClass.ShowMessage("修改成功!");
return;
}
}
else
{
return;
}
}
}