在 DatagridView 中抑制 DataError 事件
suppress DataError Event in DatagridView
我在 C# windows 表单中得到了一个 DataGridView,它从 excel 文件中填充数据。
我像这样将 excel 绑定到网格
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
OleDbConnection oledbconnection = new OleDbConnection(connectionString);
oledbconnection.Open();
OleDbCommand oledbcommand = new OleDbCommand("SELECT * FROM [StudentInformationSheet$]", oledbconnection);
oledbadapter = new OleDbDataAdapter(oledbcommand);
ds = new DataSet();
oledbadapter.Fill(ds, "Passengers");
DataTable dt = ds.Tables[0];
GridViewPassenger.DataSource = ds.Tables[0].DefaultView;
oledbconnection.Close();
要求
填充网格后,我可以编辑网格。我想要一些字段,因为 mandatory.Lets 说 3 个字段,DOB、Age、Address 不是强制性的。所以我可以清除单元格中的数据。
问题
当我清除地址字段时,它工作正常。现在我清除 DOB 单元格并单击下一个单元格出现这样的消息框
与带有消息 "string was not recognize as a valid Int32"
的年龄列相同
我怎样才能摆脱检查....
有什么办法可以抑制这种情况吗?
编辑 1
我在这里发现了一个类似的问题
DataError Dialog Box
但对我帮助不大。
编辑 2
我把这个事件
private void GridViewPassenger_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
return;
}
现在消息框消失了...但是我无法提交空单元格.. 当我清除单元格然后我无法单击表单中的其他任何地方。唯一的机会是单击 Esc 然后编辑 ID 消失...它恢复到单元格中的值。
有没有办法将列的数据类型更改为 datagridview 的字符串?
已解决
我自己找到了解决办法。我会 post 它作为答案。
您可以在 cell_leave 事件中处理错误,而不是抑制错误:
Private Sub DGV_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.RowEnter
Dim theDate As DateTime
If Date.TryParse(DGV(0, e.RowIndex).Value.ToString, theDate) Then
DTP.CustomFormat = "dd MMMM yyyy"
DTP.Value = theDate
Else
DTP.CustomFormat = " "
End If
End Sub
以上代码在下面link的答案中有进一步解释:
https://social.msdn.microsoft.com/Forums/en-US/4cdf1a3e-ed84-41ae-9e22-a8108a371dff/null-value-from-datagridview-to-datetimepicker?forum=Vsexpressvb
我找到了解决办法。在将 dt 绑定到网格之前,我更改了 dt 列的数据类型。
这是我的附加代码。
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
OleDbConnection oledbconnection = new OleDbConnection(connectionString);
oledbconnection.Open();
OleDbCommand oledbcommand = new OleDbCommand("SELECT * FROM [StudentInformationSheet$]", oledbconnection);
oledbadapter = new OleDbDataAdapter(oledbcommand);
ds = new DataSet();
oledbadapter.Fill(ds, "Passengers");
DataTable dt = ds.Tables[0];
DataTable dtCloned = dt.Clone();
dtCloned.Columns[0].DataType = typeof(string);
dtCloned.Columns[1].DataType = typeof(string);
foreach (DataRow row in dt.Rows)
{
dtCloned.ImportRow(row);
}
GridViewPassenger.DataSource = dtCloned.DefaultView;
oledbconnection.Close();
这解决了我的问题。
我在 C# windows 表单中得到了一个 DataGridView,它从 excel 文件中填充数据。
我像这样将 excel 绑定到网格
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
OleDbConnection oledbconnection = new OleDbConnection(connectionString);
oledbconnection.Open();
OleDbCommand oledbcommand = new OleDbCommand("SELECT * FROM [StudentInformationSheet$]", oledbconnection);
oledbadapter = new OleDbDataAdapter(oledbcommand);
ds = new DataSet();
oledbadapter.Fill(ds, "Passengers");
DataTable dt = ds.Tables[0];
GridViewPassenger.DataSource = ds.Tables[0].DefaultView;
oledbconnection.Close();
要求
填充网格后,我可以编辑网格。我想要一些字段,因为 mandatory.Lets 说 3 个字段,DOB、Age、Address 不是强制性的。所以我可以清除单元格中的数据。
问题
当我清除地址字段时,它工作正常。现在我清除 DOB 单元格并单击下一个单元格出现这样的消息框
与带有消息 "string was not recognize as a valid Int32"
的年龄列相同我怎样才能摆脱检查....
有什么办法可以抑制这种情况吗?
编辑 1
我在这里发现了一个类似的问题
DataError Dialog Box
但对我帮助不大。
编辑 2
我把这个事件
private void GridViewPassenger_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
return;
}
现在消息框消失了...但是我无法提交空单元格.. 当我清除单元格然后我无法单击表单中的其他任何地方。唯一的机会是单击 Esc 然后编辑 ID 消失...它恢复到单元格中的值。
有没有办法将列的数据类型更改为 datagridview 的字符串?
已解决
我自己找到了解决办法。我会 post 它作为答案。
您可以在 cell_leave 事件中处理错误,而不是抑制错误:
Private Sub DGV_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.RowEnter
Dim theDate As DateTime
If Date.TryParse(DGV(0, e.RowIndex).Value.ToString, theDate) Then
DTP.CustomFormat = "dd MMMM yyyy"
DTP.Value = theDate
Else
DTP.CustomFormat = " "
End If
End Sub
以上代码在下面link的答案中有进一步解释: https://social.msdn.microsoft.com/Forums/en-US/4cdf1a3e-ed84-41ae-9e22-a8108a371dff/null-value-from-datagridview-to-datetimepicker?forum=Vsexpressvb
我找到了解决办法。在将 dt 绑定到网格之前,我更改了 dt 列的数据类型。
这是我的附加代码。
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
OleDbConnection oledbconnection = new OleDbConnection(connectionString);
oledbconnection.Open();
OleDbCommand oledbcommand = new OleDbCommand("SELECT * FROM [StudentInformationSheet$]", oledbconnection);
oledbadapter = new OleDbDataAdapter(oledbcommand);
ds = new DataSet();
oledbadapter.Fill(ds, "Passengers");
DataTable dt = ds.Tables[0];
DataTable dtCloned = dt.Clone();
dtCloned.Columns[0].DataType = typeof(string);
dtCloned.Columns[1].DataType = typeof(string);
foreach (DataRow row in dt.Rows)
{
dtCloned.ImportRow(row);
}
GridViewPassenger.DataSource = dtCloned.DefaultView;
oledbconnection.Close();
这解决了我的问题。