c# - How to validate a datagridview column with 3 database field combine together -
basically have datagridview retrieve database , 3 of database field column combine column in datagridview.
but problem come, need update before need validate user have editing before click on update button, don't know how validate them when in same column in datagridview.
i able validate column 1 database field
p.s doing editing , updating in datagridview, need validate 3 database field column.
this how call databasase datagridview
ps. position int
protected void bindstaff() { sqlconnection conn = new sqlconnection("data source=.\\sqlexpress;initial catalog=brandcom;integrated security=true"); sqldataadapter adapter = new sqldataadapter("select staffid, (staffname+','+ staffnric+','+ position + ';')as staffdetail, yearin, age, address, email, stayindicator staffbook", conn); dataset ds = new dataset(); adapter.fill(ds); dgvstaffbook.datasource = ds.tables[0]; }
this column saying able validate (only 1 database field column)
private void dgvstaffbook_cellendedit(object sender, datagridviewcelleventargs e) { if (e.rowindex >= 0 && e.columnindex >= 0) { int stayind; if (e.columnindex == 6) { stayind = convert.toint32(dgvstaffbook.rows[e.rowindex].cells[e.columnindex].value); if (stayind != 0 && stayind != 1) { messagebox.show("please enter 1 or 0"); dgvstaffbook.rows[e.rowindex].cells[e.columnindex].value = dbnull.value; } else return; } } }
i try position , give me error: input string not correct format
private void dgvstaffbook_cellendedit(object sender, datagridviewcelleventargs e) { if (e.rowindex >= 0 && e.columnindex >= 0) { if (e.columnindex == 1) { string[] position = null; position = convert.tostring(dgvstaffbook.currentrow.cells[1].value).split(','); if (int.parse(position[2]) != 1 && int.parse(position[2])!=2 ) { messagebox.show("please update number type accordingly:" + environment.newline + "1 - high rank " + environment.newline + "2 -low rank" ); } else return; } } }
imo displaying 3 database fields , allowing user edit same in 1 column dangerous. suggest show them 3 separate columns instead.
maybe can try making 3 corresponding properties , merge them via separator |
not occur in fields via fourth property. when value split same on |
, perform checks , assign respective values 3 corresponding properties. (but wouldn't recommend this) field1
field2
field3
staffdetails
-- field1
+"|" +field2
+"|" + field3
then use e.formattedvalue.tostring() , split on |
to string[] of 3 ,now validate
also can take on creating column of maskedtextbox
user experience gonna pathetic
Comments
Post a Comment