c# - Set datagrid row background color WPF - Loop -
i have 1 task
go throw datagridrow
, task. when finish set background color
row. added cancel button
stop task, , continue button
continue finished last time. work perfect except changing background color row.
this xaml code, i'm new wpf it's not big datagrid
<datagrid name="datagridviewmygroups" grid.row="0" columnwidth="*" verticalscrollbarvisibility="auto" isreadonly="true" selectionunit="fullrow" selectionmode="single" mousedoubleclick="datagridviewmygroups_mousedoubleclick"> </datagrid>
here c# code changing background color.
datagridrow rowcolor = (datagridrow)datagridviewmygroups.itemcontainergenerator .containerfromindex(number); rowcolor.background = new solidcolorbrush(system.windows.media.color.fromrgb(223, 227, 238));
this code work when click on start button
, change background
color each row
. problem when click cancel button
, click on continue button
, got nullreferenceexception
. continue button check last id in database table
.
int number=0; foreach (groupsinlist group in datagridviewmygroups.items) { if (fbgroupid != null && check==true) { number++; if (fbgroupid != grouplink) { continue; } check = false; continue; } //do , change background (code above). number++; }
code continue button
work except changing row's background.
update: code cancel button
:
if (messagebox.show("do want stop posting?", "confirmation", messageboxbutton.yesno, messageboximage.question) == messageboxresult.yes) { tokensource.cancel(); }
code continue button
:
int postid; string fbgroupid; int listid; using (oledbconnection connection = new oledbconnection(conn)) { //code getting value `db table` postid = list[0].postid; fbgroupid = list[0].fbgroupid; listid = list[0].listforgroupid; } cmbselectlist.selectedvalue = listid; cmbsavedposts.selectedvalue = postid; loadgroupsinlist(); //maybe problem because here update(reload) datagrid again. tokensource = new cancellationtokensource(); try { await taskposttogroup(tokensource.token, fbgroupid, true); } catch (operationcanceledexception ex) { system.windows.messagebox.show(ex.message, "canceled", messageboxbutton.ok, messageboximage.stop); } catch (nullreferenceexception) { //i don't have solution changing background color each row when continue button clicked } catch (exception ex) { system.windows.messagebox.show(ex.message + "\n\n" + ex.stacktrace, "exception", messageboxbutton.ok, messageboximage.error); }
ok think see problem now. it's there in own code:
int number=0; foreach (groupsinlist group in datagridviewmygroups.items) { if (fbgroupid != null && check==true) { number++; if (fbgroupid != grouplink) { continue; } check = false; continue; } //do , change background (code above). number++; }
your foreach loop runs many times there rows in datagrid. inside increment number
variable twice in cases depending on logic. is, if go in if
statement increase once, , again @ end of loop. whenever go in if
statement increment row count twice.
which why counter goes higher value actual number of rows have. need remove increment inside if
statement.
Comments
Post a Comment