.net - How to reference a DataGridViewCell by column name in a DataGridViewRow? -
i'm populating datagridview
shown in code below. while each created row associated parent datagridview
, in columns created, cannot reference cells in row column name.
is there way me reference column name? rather avoid using integer based index.
edit: note columns of datagridview has been created , correctly named using visual studio designer.
private sub setserviceorders() dim row datagridviewrow = nothing dim rowvalues iserviceorderdatagridviewrowvalues = nothing me.serviceordersdatagridview.rows.clear() if not _serviceorders nothing andalso _serviceorders.count > 0 each serviceorder serviceorder in _serviceorders.values rowvalues = serviceorder row = new datagridviewrow() me.serviceordersdatagridview.rows.add(row) row 'this fails: "specified argument out of range of valid values." .cells("statusimagecolumn").value = my.resources.bulletgreen24 .cells("orderdatecolumn").value = rowvalues.created .cells("createdbycolumn").value = rowvalues.ownername end me.serviceordersdatagridview.rows.add() next end if end sub
you not able reference datagridviewcell
column name because datagridviewrow
not correctly created:
row = new datagridviewrow() '=> new datagridview row no knowledge datagridview parent me.serviceordersdatagridview.rows.add(row) '
i believe line: me.serviceordersdatagridview.rows.add(row) associate row datagridview.
it seems no. should work instead:
private sub setserviceorders() dim row datagridviewrow = nothing dim rowvalues iserviceorderdatagridviewrowvalues = nothing dim rowindex integer 'index of row me.serviceordersdatagridview.rows.clear() if not _serviceorders nothing andalso _serviceorders.count > 0 each serviceorder serviceorder in _serviceorders.values rowvalues = serviceorder '/////create new row , index///// rowindex = me.serviceordersdatagridview.rows.add() '//////get reference new row /////// row = me.serviceordersdatagridview.rows(rowindex) row 'this won't fail since columns exist .cells("statusimagecolumn").value = my.resources.bulletgreen24 .cells("orderdatecolumn").value = rowvalues.created .cells("createdbycolumn").value = rowvalues.ownername end '//// think line not required ' me.serviceordersdatagridview.rows.add() next end if end sub
Comments
Post a Comment