dimanche 29 mars 2015

suppress DataError Event in DatagridView

I got a DataGridView in a C# windows Form, which fills data from an excel file.


I bind the excel to grid like this



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();




Requirement


After filling the grid i can edit grid.. I want some fields as not mandatory.Lets say 3 fields, DOB,Age, Address are not mandatory. so i can clear the data in the cell.




Problem


when i clear the address field its worked fine. now i clear the DOB cell and click the next cell there come a message box like this enter image description here


and same with Age column with message "string was not recognize as a valid Int32"




How can i get rid of the checking....


is there any way to suppress this?


EDIT 1


I found a similar question here


DataError Dialog Box


But did't help me much.


EDIT 2


I put this event



private void GridViewPassenger_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
return;
}


Now the message box gone...But i cant commit the empty cell.. When I clear the cell and then i cant click anywhere else in the form. the only chance is to click Esc and then the edit id gone...it restores to the value in cell.


Is there any way to change the datatype of column to string of the datagridview?




Solved


Myself found a solution. i will post it as answer.


Aucun commentaire:

Enregistrer un commentaire