绑定到数据库表的DataGridView。我已经使用CellEndEdit事件触发的Update命令设置了tableadapter,但是我在调用update时遇到了一些问题。
更新命令:
UPDATE dbo.EpsProgramacaoSubmontagem
SET GUID = @GUID, CascoID = @CascoID, Casco = @Casco, BlocoID = @BlocoID, Bloco = @Bloco, Sub = @Sub, SSub = @SSub, Linha = @Linha,
Quantidade = @Quantidade, Peso = @Peso, [Inicio Plan Montagem] = @Inicio_Plan_Montagem, Oficina = @Oficina,
[Inicio Plan Submontagem] = @Inicio_Plan_Submontagem, [Termino Plan Submontagem] = @Termino_Plan_Submontagem, [Temino Real] = @Temino_Real,
Turno = @Turno, [Status Processamento] = @Status_Processamento, [Status Sub] = @Status_Sub, [Data ] = @Data_
WHERE (GUID = @Original_GUID)
上面的每个TableAdapter.Update参数都与DataGridView列相关。我已经尝试过了:
private void SubProgDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//UPDATE DE LINHA
DataGridViewRow row = SubProgDGV.Rows[e.RowIndex];
Guid guid = (Guid)row.Cells["gUIDDataGridViewTextBoxColumn"].Value;
int cascoid = Convert.ToInt16(row.Cells["cascoIDDataGridViewTextBoxColumn3"].Value);
string casco = row.Cells["cascoDataGridViewTextBoxColumn11"].Value.ToString();
int blocoid = Convert.ToInt16(row.Cells["blocoIDDataGridViewTextBoxColumn5"].Value);
string bloco = row.Cells["blocoDataGridViewTextBoxColumn11"].Value.ToString();
string sub = row.Cells["subDataGridViewTextBoxColumn1"].Value.ToString();
string ssub = row.Cells["sSubDataGridViewTextBoxColumn1"].Value.ToString();
string linha = row.Cells["linhaDataGridViewTextBoxColumn"].Value.ToString();
int qtd = Convert.ToInt16(row.Cells["quantidadeDataGridViewTextBoxColumn3"].Value);
double peso = Convert.ToDouble(row.Cells["pesoDataGridViewTextBoxColumn4"].Value);
DateTime datacmb = Convert.ToDateTime(row.Cells["inicioPlanMontagemDataGridViewTextBoxColumn"].Value);
string oficina = row.Cells["oficinaDataGridViewTextBoxColumn"].Value.ToString();
DateTime datasub = Convert.ToDateTime(row.Cells["inicioPlanSubmontagemDataGridViewTextBoxColumn"].Value);
DateTime datasubfim = Convert.ToDateTime(row.Cells["terminoPlanSubmontagemDataGridViewTextBoxColumn"].Value);
DateTime datatermino = row.Cells["teminoRealDataGridViewTextBoxColumn"].Value == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(row.Cells["teminoRealDataGridViewTextBoxColumn"].Value);
string turno = row.Cells["turnoDataGridViewTextBoxColumn"].Value.ToString();
string statusproc = row.Cells["statusProcessamentoDataGridViewTextBoxColumn"].Value.ToString();
string statussub = row.Cells["statusSubDataGridViewTextBoxColumn"].Value.ToString();
DateTime data = row.Cells["dataDataGridViewTextBoxColumn1"].Value == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(row.Cells["dataDataGridViewTextBoxColumn1"].Value);
this.epsProgramacaoSubmontagemTableAdapter.Update(cascoid, casco, blocoid, bloco, sub, ssub, linha, qtd, peso, datacmb, oficina, datasub, datasubfim, datatermino, turno, statusproc, statussub, data , guid );
}
但是,每当其中一个datetimes参数为null时,它就会抛出异常。有没有更好的方法来处理这个?既然我已经设置了DGV列和SQL参数之间的关系不应该是一种简单的方法吗?
答案 0 :(得分:0)
我认为您的空日期单元格值实际上不会== DBNull.Value
如果你试过这个怎么办?
DateTime data = row.Cells["dataDataGridViewTextBoxColumn1"].Value == null ? DateTime.MinValue : Convert.ToDateTime(row.Cells["dataDataGridViewTextBoxColumn1"].Value);
或者甚至可能使用string.IsNullOrEmpty()
.FormattedValue
DateTime data = string.IsNullOrEmpty((string)row.Cells["dataDataGridViewTextBoxColumn1"].FormattedValue) ? DateTime.MinValue : Convert.ToDateTime(row.Cells["dataDataGridViewTextBoxColumn1"].Value);
//UPDATE DE LINHA
DataGridViewRow row = SubProgDGV.Rows[e.RowIndex];
Guid guid = (Guid)row.Cells["gUIDDataGridViewTextBoxColumn"].Value;
int cascoid = Convert.ToInt16(row.Cells["cascoIDDataGridViewTextBoxColumn3"].Value);
string casco = row.Cells["cascoDataGridViewTextBoxColumn11"].Value.ToString();
int blocoid = Convert.ToInt16(row.Cells["blocoIDDataGridViewTextBoxColumn5"].Value);
string bloco = row.Cells["blocoDataGridViewTextBoxColumn11"].Value.ToString();
string sub = row.Cells["subDataGridViewTextBoxColumn1"].Value.ToString();
string ssub = row.Cells["sSubDataGridViewTextBoxColumn1"].Value.ToString();
string linha = row.Cells["linhaDataGridViewTextBoxColumn"].Value.ToString();
int qtd = Convert.ToInt16(row.Cells["quantidadeDataGridViewTextBoxColumn3"].Value);
double peso = Convert.ToDouble(row.Cells["pesoDataGridViewTextBoxColumn4"].Value);
DateTime datacmb = Convert.ToDateTime(row.Cells["inicioPlanMontagemDataGridViewTextBoxColumn"].Value);
string oficina = row.Cells["oficinaDataGridViewTextBoxColumn"].Value.ToString();
DateTime datasub = Convert.ToDateTime(row.Cells["inicioPlanSubmontagemDataGridViewTextBoxColumn"].Value);
DateTime datasubfim = Convert.ToDateTime(row.Cells["terminoPlanSubmontagemDataGridViewTextBoxColumn"].Value);
DateTime datatermino = row.Cells["teminoRealDataGridViewTextBoxColumn"].Value == null ? DateTime.MinValue : Convert.ToDateTime(row.Cells["teminoRealDataGridViewTextBoxColumn"].Value);
string turno = row.Cells["turnoDataGridViewTextBoxColumn"].Value.ToString();
string statusproc = row.Cells["statusProcessamentoDataGridViewTextBoxColumn"].Value.ToString();
string statussub = row.Cells["statusSubDataGridViewTextBoxColumn"].Value.ToString();
DateTime data = row.Cells["dataDataGridViewTextBoxColumn1"].Value == null ? DateTime.MinValue : Convert.ToDateTime(row.Cells["dataDataGridViewTextBoxColumn1"].Value);
答案 1 :(得分:0)
我找到的唯一方法是检查它是否为null或不使用IsDBNull如下代码,只记得总是使用DateTime?在所有情况下允许null
DateTime? PrintDate = reader.IsDBNull(reader.GetOrdinal("PrintDate")) ? (DateTime?)null : DateTime.Parse(reader.GetString(reader.GetOrdinal("PrintDate")))
这个解决方案有一些性能价格,但它可以完成这项工作
答案 2 :(得分:0)
我找不到有关检查null或变通方法的解决方案,而是使用了DataGridViewDataBindSource_CurrentItemChanged事件。 每次发生更改时都会触发并自动将更改应用到数据库,我认为这应该是自动显示的,因为选择在绑定的DataGridView上启用编辑。