c# - Draw marks on a DataGridView vertical Scrollbar -
i working on project cells in datagridview highlighted. , wondering if can create marks on scrollbar indicate highlights are. ideas may helpful.
the yes, no , maybe
the yes: according this possible. however, links answer; not sure lead..
the no: according cody gray's first-class analysis in reply this post painting on scrollbar not possible.
but maybe workaround solve problem..?
here idea:
you add together lean panel
either overlays scrollbar or attaches left. should lean , go on height of scrolbar; gets redrawn usual paint events.
you maintain list of rows, marks should shown. list re-created or maintained upon:
adding & removingrows
changing target row possibly when sorting or filtering here little code, quick proof of concept. more robust solution guess create decorator class datagridview
register.
now, when move lift towards marks find target rows. lot of room improvement, start imo..
you have alter isrowmarked()
function needs. have chosen test first cell's backcolor..
you can utilize different colors different marks; maybe copying them marked row/cell.
public form1() { initializecomponent(); datagridview1.controls.add(indicatorpanel); indicatorpanel.width = 6; indicatorpanel.height = datagridview1.clientsize.height - 39; indicatorpanel.top = 20; indicatorpanel.left = datagridview1.clientsize.width - 21; indicatorpanel.paint += indicatorpanel_paint; datagridview1.paint += datagridview1_paint; } panel indicatorpanel = new panel(); list<datagridviewrow> tgtrows = new list<datagridviewrow>(); void datagridview1_paint(object sender, painteventargs e) { indicatorpanel.invalidate(); } void indicatorpanel_paint(object sender, painteventargs e) { // check if there hscrollbar int hs = ((datagridview1.scrollbars & scrollbars.vertical) != scrollbars.none ? 20 : 0); e.graphics.fillrectangle(brushes.silver, indicatorpanel.clientrectangle); foreach (datagridviewrow trow in tgtrows) { int h = (int)(1f * (indicatorpanel.height - 20 + hs) * trow.index / datagridview1.rows.count); e.graphics.fillrectangle(brushes.red, 0, h-3, 6, 4); } } bool isrowmarked(datagridviewrow row) { homecoming row.cells[0].style.backcolor == color.red; // <<-- change! } // phone call in: datagridview1_rowsremoved, datagridview1_rowsadded // whenever set or alter markings , after sorting or filtering void findmarkers() { tgtrows.clear(); foreach (datagridviewrow row in datagridview1.rows) if (isrowmarked(row) ) tgtrows.add(row); indicatorpanel.invalidate(); }
note have removed first reply original requirements talk of 'marks' not 'a few marks'. sec version seems lot nicer me, now.
c# .net winforms datagridview scrollbar
No comments:
Post a Comment