Loop through multiple DGVS and run loops through each one, vb.net -
i using vb.net windows forms sql end. have windows form has multiple datagridviews , need run through line items , run sql check on each row. curent code long , require me copy , paste code on , on again , form pushing 1.2k lines of code want keep short. considering each dgv has specific name wondering if generate list of strings loop through or somehow use pane. here code far:
try dim chickenz string dim mydogtank string 'set loop cn integer = 1 dgv1.rowcount dim variable1 date = dgv1.rows(cn - 1).cells(1).value using conn1 new sqlconnection(connstring) conn1.open() using comm1 new sqlcommand (sqlcommand, conn1) comm1.parameters.addwithvalue("@parameter", variable1) dim dt new datatable dim sql new sqldataadapter(comm1) sql.fill(dt) each row datarow in dt.rows chickenz = row.item("col2") mydogtank = row.item("col1") '--------sql check using conn2 new sqlconnection(connstring) conn2.open() using comm2 new sqlcommand(sqlcommand, conn2) comm2.parameters.addwithvalue("@job", chickenz) dim eggz new datatable dim yolk new sqldataadapter(comm2) yolk.fill(eggz) if eggz.rows.count >= 1 msgbox("warningbox") end if end using conn2.close() end using next end using conn1.close() end using next catch ex exception msgbox(ex.tostring) end try in example, dgv1 change between multiple dgv names , depending on how code variable 1 change. question how change dgv different names need become. otherwise going have copy , paste code 8 times.
since know names of datagridviews
dim dgvnames string() = {"dgv1", "dgv2", "dgv3","names of datagridviews"} each dgvn string in dgvnames dim dgv datagridview = ctype(me.controls(dgvn), datagridview) rest of code goes here........ next
Comments
Post a Comment