我正在使用 DevExpress DataGrid 我正在从 excel 中填充它。如果用户将字段名称保持为空,我需要给他一条错误消息。这是我的代码:
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
string FirstName = "First Name";
string FatherName = "Father Name";
string LastName = "Last Name";
private void simpleButton1_Click(object sender, System.EventArgs e)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\\Users\\pc\\Documents\\Emp.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes\"";
con.Open();
DataTable dtSchema;
dtSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
OleDbCommand Command = new OleDbCommand ("select * FROM [" + dtSchema.Rows[0]["TABLE_NAME"].ToString() + "]", con);
OleDbDataAdapter da = new OleDbDataAdapter(Command);
DataSet ds = new DataSet ();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
private void gridView3_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
{
GridView view = sender as GridView;
if(view.FocusedColumn.FieldName == "FirstName")
{
string FirName = (e.Value.ToString());
if(FirName == null)
{
e.Valid = false;
e.ErrorText = "Enter a First Name";
}
}
}
private void gridView3_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
{
//Do not perform any default action
e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
//Show the message with the error text specified
MessageBox.Show(e.ErrorText);
}
问题是当我运行程序时..代码无法访问此代码并且没有显示错误..有人能注意到我的错在哪里吗?太感谢了