vb.net - Dual check box selection vb / winform -
i after little bit of help:
i have datagridview control add couple of columns (as check boxes) in order select multiple rows. when select first checkbox column want second selected automatically, can de-selected if required. , if first checkbox deselected, 2nd auto deselected too.
i have working following code:
private sub dgvbikeavailability_cellcontentclick(sender system.object, e system.windows.forms.datagridviewcelleventargs) handles dgvbikeavailability.cellcontentclick debug.print("row index = " + e.rowindex.tostring + ". column index = " + e.columnindex.tostring + ". column name = ") 'debug.print() 'when bike selected, helmet automatically selected, can deselected if customer requires if e.columnindex = 0 dgvbikeavailability.rows(e.rowindex).cells(0).value = not dgvbikeavailability.rows(e.rowindex).cells(0).value dgvbikeavailability.rows(e.rowindex).cells(1).value = dgvbikeavailability.rows(e.rowindex).cells(0).value end if end sub
unfortunately, wen datagridview refreshed, column indexes incremented, , column indexes 2 check box columns 2 , 3.
i able refer them name. declared in sub refreshes datagridview:
colbikeselectioncheckbox.headertext = "select bike" colbikeselectioncheckbox.name = "colselectbike") colhelmetcheckbox.name = "colselecthelmet" dgvbikeavailability.columns.add(colhelmetcheckbox)
but system.windows.forms.datagridviewcelleventargs class not allow me select column name.
any ideas , suggestions appreciated!
edit: more in depth code segment:
private sub frmbikehire_load(sender system.object, e system.eventargs) handles mybase.load refreshgrid() getstaffmember() end sub 'loads , refreshes dgv private sub refreshgrid() dim integer 'initially fill in rental dates current day txtdatefrom.text = cstr(monthcalendar1.selectionrange.start) txtdateto.text = cstr(monthcalendar1.selectionrange.end) 'grab data db, set dgv datasource startdate = cstr(monthcalendar1.selectionrange.start) enddate = cstr(monthcalendar1.selectionrange.end) dtbikeavailability = sqlfuncdb_getdatatable("select * tb_bikedetail bikeid not in (select distinct bikeid tb_bikemovements bikemovementdate between '" + func_convertdatesqlserver(startdate) + "' , '" + func_convertdatesqlserver(enddate) + "') , bikeid not in (select tb_bikemovements.bikeid tb_bikemovements join (select bikeid, max(bikemovementdate) bmd tb_bikemovements bikemovementdate < '" + func_convertdatesqlserver(startdate) + "' group bikeid) lastmove on lastmove.bikeid=tb_bikemovements.bikeid , lastmove.bmd=tb_bikemovements.bikemovementdate bikemovementtype = '0')") dvbikeavailability = new dataview(dtbikeavailability) dgvbikeavailability.datasource = dvbikeavailability 'switch off columns = 0 dgvbikeavailability.columns.count - 1 dgvbikeavailability.columns(i).visible = false next 'displays relevant column(s) dgvbikeavailability.columns("bikename").visible = true dgvbikeavailability.columns("bikename").headertext = "bike name" dgvbikeavailability.columns("bikestyle").visible = true dgvbikeavailability.columns("bikestyle").headertext = "bike style" dgvbikeavailability.columns("bikecolour").visible = true dgvbikeavailability.columns("bikecolour").headertext = "bike colour" 'remove line program deployment dgvbikeavailability.columns("bikeid").visible = true dgvbikeavailability.columns("bikeid").headertext = "bike number" 'add new check box column selecting bike dim colbikeselectioncheckbox new datagridviewcheckboxcolumn colbikeselectioncheckbox.datapropertyname = "propertyname" colbikeselectioncheckbox.headertext = "select bike" colbikeselectioncheckbox.name = "colselectbike" dgvbikeavailability.columns.add(colbikeselectioncheckbox) 'add new column selecting helmet - consider adding default setting dim colhelmetcheckbox new datagridviewcheckboxcolumn colhelmetcheckbox.datapropertyname = "propertyname" colhelmetcheckbox.headertext = "helmet?" colhelmetcheckbox.name = "colselecthelmet" dgvbikeavailability.columns.add(colhelmetcheckbox) dgvbikeavailability.autosizecolumnsmode = datagridviewautosizecolumnsmode.fill end sub private sub monthcalendar1_datechanged(sender system.object, e system.windows.forms.daterangeeventargs) handles monthcalendar1.datechanged refreshgrid() end sub private sub dgvbikeavailability_cellclick(sender object, e system.windows.forms.datagridviewcelleventargs) handles dgvbikeavailability.cellclick debug.print("row index = " + e.rowindex.tostring + ". column index = " + e.columnindex.tostring + ". column name = " + dgvbikeavailability.rows(e.rowindex).cells("colselectbike").owningcolumn.tostring) if dgvbikeavailability.columns(e.columnindex).name = "colselectbike" dgvbikeavailability.rows(e.rowindex).cells("colselectbike").value = not dgvbikeavailability.rows(e.rowindex).cells("colselectbike").value dgvbikeavailability.rows(e.rowindex).cells("colselecthelmet").value = dgvbikeavailability.rows(e.rowindex).cells("colselectbike").value end if end sub
i unsure why doesn't work. each time calendar control changed refreshes datagridview, increasing column index.
you can use column index provided datagridviewcelleventargs
class retrieve column , name compare.
so like:
if dgvbikeavailability.columns(e.columnindex).name == "colselectbike" ' logic here end if
for referencing columns within code toggle checkboxes, can happily use names, , in fact not need index provided event args. event args index appears used check 1 of desired columns selected.
dgvbikeavailability.rows(e.rowindex).cells("colselectbike").value = not dgvbikeavailability.rows(e.rowindex).cells("colselectbike").value dgvbikeavailability.rows(e.rowindex).cells("colselecthelmet").value = dgvbikeavailability.rows(e.rowindex).cells("colselectbike").value
although code above should work appears odd happening grid. quick solution instead of using .add()
use .insert()
method of datagridview, provided index parameter, allowing directly control column goes.