mysql - How to change the color of a cell of Datagridview by comparing the cell date with today's Date in C#.net? -
i new c#,this first project on c# language , backend mysql, stuck @ particular point ,i having form containing datagridview binded database value,in have column "tns_date" expiration date of product ,i want if tns_date greater current date background colour of particular cell should red in colured ,how ,please me this
i using winforms , till had tried
foreach (datagridviewrow row in tnsformdgv.rows) { var = datetime.now; var expirationdate = datetime.parse(row.cells[15].value.tostring()); var onemonthbefore = expirationdate.adddays(-30); if (now > onemonthbefore && < expirationdate) { row.defaultcellstyle.backcolor = color.yellow; } else if (now > expirationdate) { row.defaultcellstyle.backcolor = color.red; } }
when executing project getting error
object reference not set instance of object
this whole code particular form please check getting wrong
public partial class tns_subscription : form { public tns_subscription() { initializecomponent(); tnssubscriptionform(); } private void tnsformbackbtn_click(object sender, eventargs e) { new form2().show(); this.hide(); } public void tnssubscriptionform() { string constring = "datasource=localhost;port=3306;username=root;password=ajay"; mysqlconnection condatabase = new mysqlconnection(constring); mysqlcommand cmddatabase = new mysqlcommand("select acmecmpdetails.cmp_number,acmecmpdetails.cmp_name,acmecmpdetails.cmp_adminid,acmecmpdetails.cmp_address1,acmecmpdetails.cmp_country1,acmecmpdetails.cmp_state1,acmecmpdetails.cmp_city1,acmecmpdetails.cmp_postalcode,acmecmpdetails.contact_person1,acmecmpdetails.landline_number1,acmecmpdetails.mobilenumber,acmecmpdetails.cntct_emailid,acmetally_detail.tallyserial_no,acmetally_detail.tally_release,acmetally_detail.tally_edition,acmetally_detail.tns_date,acmetally_detail.tally_prefpartner,acmetally_detail.tally_accountid acmesolutionsdatabase.acmecmpdetails inner join acmesolutionsdatabase.acmetally_detail on acmesolutionsdatabase.acmecmpdetails.tallyserial_no= acmesolutionsdatabase.acmetally_detail.tallyserial_no;", condatabase); try { mysqldataadapter cddsda = new mysqldataadapter(); cddsda.selectcommand = cmddatabase; datatable dbdataset = new datatable(); cddsda.fill(dbdataset); bindingsource bsource = new bindingsource(); bsource.datasource = dbdataset; tnsformdgv.datasource = bsource; cddsda.update(dbdataset); for(int i=0;i<tnsformdgv.rows.count;i++) { if (convert.todatetime(tnsformdgv.rows[i].cells["tns_date"].value.tostring()) > system.datetime.now.date) { tnsformdgv.rows[i].cells["tns_date"].style.backcolor = system.drawing.color.red; } } } catch (exception ex) { messagebox.show(ex.message); } } }
thanks in advance
you this:
for(int i=0;i<mygridview.rows.count;i++) { if (convert.todatetime(mygridview.rows[i].cells["tns_date"].value.tostring()) > system.datetime.now.date) { mygridview.rows[i].cells["tns_date"].style.backcolor = system.drawing.color.red; } }
it change backcolor of cell "tns_date" if value greater current date.
or can use index of column tns_date
like:
if index of column tns_date
3, then:
for(int i=0;i<mygridview.rows.count;i++) { if (convert.todatetime(mygridview.rows[i].cells[3].value.tostring()) > system.datetime.now.date) { mygridview.rows[i].cells[3].style.backcolor = system.drawing.color.red; } }
i hope solve problem.
Comments
Post a Comment