c# - Event Paint Crash When Scrolling and Draw in TopLeftHeader datagridview -
i have problem eventpaint
in datagridview
. eventpaint
crashed when datagridview
scrolling, , image show in topleftheader
in datagridview
. how can solved it? sorry can't show code.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using system.globalization; using system.configuration; using system.data.sqlclient; using system.reflection; using system.diagnostics; namespace mdacs_aop_cfstl { public partial class mainformactivity : form { public string parameternamakaryawan { get; private set; } public string parametersesi { get; private set; } public datetime parametertanggal { get; private set; } public string headercell; public string headerrow; public string hari = ""; karyawan mykaryawan = new karyawan(); activity myactivity = new activity(); validasitanggal mytanggal = new validasitanggal(); contextmenustrip m = new contextmenustrip(); project myproject = new project(); datetime tglawal; datetime tglakhir; warna coloring = new warna(); string headervalue = ""; datetime time; public string[,] kodeprojecttoproblem = new string[10,10]; public mainformactivity() { initializecomponent(); } private void form3_load(object sender, eventargs e) { /// /// setting property double buffered dari datagridview /// karena propertynya ter hiden jadi harus manual /// datagridview1.topleftheadercell.style.backcolor = color.white; typeof(datagridview).invokemember("doublebuffered", bindingflags.nonpublic | bindingflags.instance | bindingflags.setproperty, null, datagridview1, new object[] { true }); setcombobox(); setcolumn(); setrow(); datagridview1.clearselection(); (int j = 0; j < datagridview1.rows.count; j++) { datagridview1.rows[j].frozen = false; (int = 0; < datagridview1.columns.count; i++) { datagridview1.columns[i].frozen = false; } } datagridview1.scrollbars = scrollbars.both; datagridview1.autosize = false; } protected override void onpaint(painteventargs pevent) { base.onpaint(pevent); } private static void freezeband(datagridviewband band) { band.frozen = true; datagridviewcellstyle style = new datagridviewcellstyle(); style.backcolor = color.whitesmoke; band.defaultcellstyle = style; } private void datagridview1_columnwidthchanged(object sender, datagridviewcolumneventargs e) { //rectangle rtheader = this.datagridview1.displayrectangle; //rtheader.height = this.datagridview1.columnheadersheight / 2; //this.datagridview1.invalidate(rtheader); } private void datagridview1_scroll(object sender, scrolleventargs e) { rectangle rtheader = this.datagridview1.displayrectangle; rtheader.height = this.datagridview1.columnheadersheight / 2; this.datagridview1.invalidate(rtheader); } private void datagridview1_paint(object sender, painteventargs e) { /// /// menggambar header column menjadi terlihat seperti di merge cells /// image newimage = image.fromfile(application.startuppath + "\\sampimag.jpg", true); string[] monthes = (string[])mykaryawan.getkaryawan().toarray(typeof(string)); (int j = 0; j < mykaryawan.countkaryawan() * 2; ) { rectangle recbackground = this.datagridview1.getcelldisplayrectangle(j, -1, true); rectangle recimage = this.datagridview1.getcelldisplayrectangle(j, -1, true); rectangle recstring = this.datagridview1.getcelldisplayrectangle(j, -1, true); int w2 = this.datagridview1.getcelldisplayrectangle(j + 1, -1, true).width; recbackground.x += 1; recbackground.y += 1; recimage.x += 1; recimage.y += 1; recstring.x += (recstring.width + w2) / 2; recstring.y += 1; recbackground.width = recbackground.width + w2 - 2; recbackground.height = (recbackground.height / 2 - 2) + recbackground.height / 4; recimage.width = (recimage.width + w2) / 2 - 2; recimage.height = (recimage.height / 2 - 2) + recimage.height / 4; recstring.width = (recstring.width + w2) / 2 - 2; recstring.height = (recstring.height / 2 - 2) + recstring.height / 4; e.graphics.fillrectangle(new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.backcolor), recbackground); stringformat format = new stringformat(); format.alignment = stringalignment.center; format.linealignment = stringalignment.center; e.graphics.drawstring(monthes[j / 2], this.datagridview1.columnheadersdefaultcellstyle.font, new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.forecolor), recstring, format); e.graphics.drawimage(newimage, recimage); j += 2; } /// /// initializing variable yang akan digunakan untuk menggambar /// flag untuk menandakan posisi koordinat untuk looping gambar /// string awal = datagridview1.rows[0].headercell.value.tostring().substring(6).tostring(); string akhir = datagridview1.rows[6].headercell.value.tostring().substring(6).tostring(); tglawal = convert.todatetime(awal); tglakhir = convert.todatetime(akhir); var split = new string[25]; int countrows = myactivity.getactivitypagi(tglawal, tglakhir).tables[0].rows.count; int countcolumns = myactivity.getactivitypagi(tglawal, tglakhir).tables[0].columns.count; font font1 = new font("arial", 10, fontstyle.bold, graphicsunit.point); font font2 = new font("arial", 8, fontstyle.regular, graphicsunit.point); int[,] flag = new int[datagridview1.rows.count, datagridview1.columns.count]; int[,] flag2 = new int[datagridview1.rows.count, datagridview1.columns.count]; int[,] flag3 = new int[datagridview1.rows.count, datagridview1.columns.count]; int dgrows = datagridview1.rows.count; int dgcolumns = datagridview1.columns.count; /// /// draw background colour dan draw string /// pertama looping semua isi data set kedalam string array /// kemudian looping per cell untuk menggambar stringnya dan fill rectangle /// string[,] activity = new string[myactivity.getactivitypagi(tglawal, tglakhir).tables[0].rows.count, 6]; (int = 0; < countrows; i++) { (int j = 0; j < countcolumns; j++) { activity[i, j] = myactivity.getactivitypagi(tglawal, tglakhir).tables[0].rows[i][j].tostring(); } } (int = 0; < dgrows; i++) { (int j = 0; j < dgcolumns; j++) { (int k = 0; k < countrows; k++) { headervalue = datagridview1.rows[i].headercell.value.tostring(); split = headervalue.split(','); time = datetime.parse(split[1]); if (time == convert.todatetime(activity[k, 5]) && datagridview1.columns[j].name == activity[k, 3] + activity[k, 4]) { rectangle r1 = this.datagridview1.getcelldisplayrectangle(j, i, true); rectangle r2 = this.datagridview1.getcelldisplayrectangle(j, i, true); rectangle r3 = this.datagridview1.getcelldisplayrectangle(j, i, true); int w2 = this.datagridview1.getcelldisplayrectangle(j, i, true).width; r1.x += 1; r1.y += flag[i, j] + 1; r1.width = 65; r1.height = 15; r2.x += 1; r2.y += flag2[i, j] + 15; r2.width = 65; r2.height = 15; r3.x += 1; r3.y += flag3[i, j] + 30; r3.width = 70; r3.height = 46; flag[i, j] = flag[i, j] + 76; flag2[i, j] = flag2[i, j] + 76; flag3[i, j] = flag3[i, j] + 76; if (flag3[i, j] == datagridview1.rows[i].minimumheight) { datagridview1.rows[i].minimumheight += 76; } stringformat format = new stringformat(); stringformat format2 = new stringformat(); format2.alignment = stringalignment.near; format2.linealignment = stringalignment.near; format.alignment = stringalignment.center; format.linealignment = stringalignment.center; if (coloring.getcolor(activity[k, 1]) == color.black) { e.graphics.fillrectangle(new solidbrush(color.white), r1); e.graphics.drawstring(activity[k, 0], font1, new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.forecolor), r1, format2); e.graphics.fillrectangle(new solidbrush(coloring.getcolor(activity[k, 1])), r2); e.graphics.drawstring(activity[k, 1], font1, new solidbrush(color.white), r2, format); e.graphics.drawstring(activity[k, 2], font2, new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.forecolor), r3, format2); } else { e.graphics.fillrectangle(new solidbrush(color.white), r1); e.graphics.drawstring(activity[k, 0], font1, new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.forecolor), r1, format2); e.graphics.fillrectangle(new solidbrush(coloring.getcolor(activity[k, 1])), r2); e.graphics.drawstring(activity[k, 1], font1, new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.forecolor), r2, format); e.graphics.drawstring(activity[k, 2], font2, new solidbrush(this.datagridview1.columnheadersdefaultcellstyle.forecolor), r3, format2); } } } } } } private void datagridview1_cellpainting(object sender, datagridviewcellpaintingeventargs e) { if (e.rowindex == -1 && e.columnindex > -1) { rectangle r2 = e.cellbounds; r2.y += e.cellbounds.height / 2; r2.height = e.cellbounds.height / 2; e.paintbackground(r2, true); e.paintcontent(r2); e.handled = true; } } private void datagridview1_cellbeginedit(object sender, datagridviewcellcanceleventargs e) { } private void button5_click(object sender, eventargs e) { var legendform = new viewlegenda(); legendform.show(); } private void button6_click(object sender, eventargs e) { /// /// membuat event paint melakukan re-draw /// (int = 0; < datagridview1.rows.count; i++) { datagridview1.rows[i].minimumheight = 152; } this.datagridview1.invalidate(); this.datagridview1.paint +=new painteventhandler(datagridview1_paint); //for (int = 0; <= datagridview1.columns.count - 1; i++) //{ // (int j = 0; j <= datagridview1.rows.count - 1; j++) // { // datagridview1.rows[j].cells[i].value = ""; // } //} /// /// validasi textbox tahun /// if (convert.toint32(txttahun.text) > 9999 || txttahun.text == "" || txttahun.text == "0"|| txttahun.text == null) { messagebox.show("masukan tahun yang benar"); } else { /// /// membuat format tanggal pada header row /// tanggalnya diambil perminggu /// menentukan tanggal perminggunya ada di class validasi tanggal /// validasitanggal v = new validasitanggal(); (int = 0; < this.datagridview1.rows.count; i++) { if (i == 0) { hari = "monday,"; } if (i == 1) { hari = "tuesday,"; } if (i == 2) { hari = "wednesday,"; } if (i == 3) { hari = "thursday,"; } if (i == 4) { hari = "friday,"; } if (i == 5) { hari = "saturday,"; } if (i == 6) { hari = "sunday,"; } if (convert.toint32(v.firstdateofweek(convert.toint32(txttahun.text), convert.toint32(cmbminggu.selecteditem.tostring()), calendarweekrule.firstfullweek, convert.toint32(cmbbulan.selectedvalue.tostring())).day) + > convert.toint32(mytanggal.lastdayofmonth(int.parse(txttahun.text), int.parse(cmbbulan.selectedvalue.tostring())).day)) { if (int.parse(cmbbulan.selectedvalue.tostring()) < 12) { this.datagridview1.rows[i].headercell.value = convert.tostring(hari) + " " + convert.tostring(int.parse(cmbbulan.selectedvalue.tostring()) + 1) + "/" + convert.tostring(convert.toint32(v.firstdateofweek(convert.toint32(txttahun.text), convert.toint32(cmbminggu.selecteditem.tostring()), calendarweekrule.firstfullweek, convert.toint32(cmbbulan.selectedvalue.tostring())).day) + - convert.toint32(mytanggal.lastdayofmonth(int.parse(txttahun.text), int.parse(cmbbulan.selectedvalue.tostring())).day)) + "/" + txttahun.text; } else { this.datagridview1.rows[i].headercell.value = convert.tostring(hari) + " " + convert.tostring(int.parse(cmbbulan.selectedvalue.tostring()) - 11) + "/" + convert.tostring(convert.toint32(v.firstdateofweek(convert.toint32(txttahun.text), convert.toint32(cmbminggu.selecteditem.tostring()), calendarweekrule.firstfullweek, convert.toint32(cmbbulan.selectedvalue.tostring())).day) + - convert.toint32(mytanggal.lastdayofmonth(int.parse(txttahun.text), int.parse(cmbbulan.selectedvalue.tostring())).day)) + "/" + convert.tostring(int.parse(txttahun.text)+1); } } else { this.datagridview1.rows[i].headercell.value = convert.tostring(hari) + " " + cmbbulan.selectedvalue + "/" + convert.tostring(convert.toint32(v.firstdateofweek(convert.toint32(txttahun.text), convert.toint32(cmbminggu.selecteditem.tostring()), calendarweekrule.firstfullweek, convert.toint32(cmbbulan.selectedvalue.tostring())).day) + i) + "/" + txttahun.text; } } } string awal = datagridview1.rows[0].headercell.value.tostring().substring(6).tostring(); string akhir = datagridview1.rows[6].headercell.value.tostring().substring(6).tostring(); tglawal = convert.todatetime(awal); tglakhir = convert.todatetime(akhir); } private void txttahun_keypress(object sender, keypresseventargs e) { e.handled = !char.isdigit(e.keychar) && !char.iscontrol(e.keychar); } //-------------------------------set datagridview-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public void setcombobox() { dictionary<string, string> test = new dictionary<string, string>(); test.add("1", "januari"); test.add("2", "februari"); test.add("3", "maret"); test.add("4", "april"); test.add("5", "mei"); test.add("6", "juni"); test.add("7", "juli"); test.add("8", "agustus"); test.add("9", "september"); test.add("10", "oktober"); test.add("11", "november"); test.add("12", "desember"); cmbbulan.datasource = new bindingsource(test, null); cmbbulan.displaymember = "value"; cmbbulan.valuemember = "key"; cmbminggu.selectedindex = 0; txttahun.text = datetime.now.year.tostring(); } public void setcolumn() { datagridview1.dock = dockstyle.none; datagridview1.anchor = (anchorstyles.top | anchorstyles.bottom | anchorstyles.left | anchorstyles.right); this.datagridview1.scrollbars = scrollbars.both; this.datagridview1.readonly = true; (int = 0; < mykaryawan.countkaryawan(); i++) { this.datagridview1.columns.add(mykaryawan.getkaryawan()[i] + "pagi", "07.00-12.00"); this.datagridview1.columns.add(mykaryawan.getkaryawan()[i] + "siang", "13.00-16.00"); this.datagridview1.columns[i].defaultcellstyle.wrapmode = datagridviewtristate.true; } datagridview1.columnheadersheight = 100; this.datagridview1.rowcount = 8; this.datagridview1.allowusertoresizecolumns = false; this.datagridview1.allowusertoaddrows = false; this.datagridview1.scrollbars = new scrollbars(); (int j = 0; j < this.datagridview1.columncount; j++) { this.datagridview1.columns[j].minimumwidth = 70; } } public void setrow() { validasitanggal v = new validasitanggal(); (int = 0; < this.datagridview1.rows.count; i++) { int j = 0; string hari = ""; if (i == 6) { j = 7; } if (i == 0) { hari = "monday,"; } if (i == 1) { hari = "tuesday,"; } if (i == 2) { hari = "wednesday,"; } if (i == 3) { hari = "thursday,"; } if (i == 4) { hari = "friday,"; } if (i == 5) { hari = "saturday,"; } if (i == 6) { hari = "sunday,"; } this.datagridview1.rows[i].minimumheight = 152; this.datagridview1.rows[i].headercell.value = convert.tostring(hari) + " " + cmbbulan.selectedvalue + "/" + convert.tostring(convert.toint32(v.firstdateofweek(2014, convert.toint32(cmbminggu.selecteditem.tostring()), calendarweekrule.firstfullweek, convert.toint32(cmbbulan.selectedvalue)).day) + i) + "/" + txttahun.text; datagridview1.rowheaderswidth = 120; } this.datagridview1.columnheadersheightsizemode = datagridviewcolumnheadersheightsizemode.enableresizing; this.datagridview1.columnheadersheight = this.datagridview1.columnheadersheight * 2; this.datagridview1.columnheadersdefaultcellstyle.alignment = datagridviewcontentalignment.bottomcenter; this.datagridview1.scroll +=new scrolleventhandler(datagridview1_scroll); } private void button2_click(object sender, eventargs e) { datagridviewselectedcellcollection c = this.datagridview1.selectedcells; label1.text = c.tostring() ; } private void datagridview1_cellclick(object sender, datagridviewcelleventargs e) { } private void datagridview1_cellenter(object sender, datagridviewcelleventargs e) { this.invalidate(); string header = ""; //headercell = datagridview1.columns[e.columnindex].name; //headerrow = e.rowindex.tostring(); //parameternamakaryawan = "a"; var split = new string[25]; if (datagridview1.rows[e.rowindex].headercell.value == null) { } else { header = datagridview1.rows[e.rowindex].headercell.value.tostring(); split = header.split(','); parametertanggal = datetime.parse(split[1]); } parametersesi = datagridview1.columns[e.columnindex].name.substring(datagridview1.columns[e.columnindex].name.length - 4, 4); if (datagridview1.columns[e.columnindex].name.substring(datagridview1.columns[e.columnindex].name.length - 4, 4) == "pagi") { parametersesi = datagridview1.columns[e.columnindex].name.substring(datagridview1.columns[e.columnindex].name.length - 4, 4); parameternamakaryawan = datagridview1.columns[e.columnindex].name.substring(0, datagridview1.columns[e.columnindex].name.length - 4); } else if (datagridview1.columns[e.columnindex].name.substring(datagridview1.columns[e.columnindex].name.length - 5, 5) == "siang") { parametersesi = datagridview1.columns[e.columnindex].name.substring(datagridview1.columns[e.columnindex].name.length - 5, 5); parameternamakaryawan = datagridview1.columns[e.columnindex].name.substring(0, datagridview1.columns[e.columnindex].name.length - 5); } //parametertanggal = convert.todatetime(datagridview1.rows[e.rowindex].headercell.tostring().substring(6)); } private void button4_click(object sender, eventargs e) { //this.datagridview1.cellenter +=new datagridviewcelleventhandler(datagridview1_cellenter); //formaddproblem addproblem = new formaddproblem(parameternamakaryawan, parametersesi, parametertanggal); //addproblem.show(); } private void button3_click(object sender, eventargs e) { } private void datagridview1_mouseclick(object sender, mouseeventargs e) { if (e.button == mousebuttons.right) { m.items.clear(); m.items.add("add activity"); m.items.add("edit activity"); m.items.add("add problem"); int currentmouseoverrow = datagridview1.hittest(e.x, e.y).rowindex; int currentmouseovercolumn = datagridview1.hittest(e.x, e.y).columnindex; //if (currentmouseoverrow >= 0) //{ // m.items.add((string.format("do row {0} ", currentmouseoverrow.tostring()))); //} this.datagridview1.rows[currentmouseoverrow].cells[currentmouseovercolumn].selected = true; m.show(datagridview1, new point(e.x, e.y)); m.itemclicked -= new toolstripitemclickedeventhandler( contexmenuuu_itemclicked); m.itemclicked += new toolstripitemclickedeventhandler( contexmenuuu_itemclicked); } } } }
Comments
Post a Comment