<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.dotnetzone.gr:443/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>WinForms</title><link>https://www.dotnetzone.gr:443/cs/forums/12/ShowForum.aspx</link><description>Θέματα για rich client / windows εφαρμογές, σε οποιαδήποτε γλώσσα (VB.NET, C#, managed C++, κ.α.)</description><dc:language>el</dc:language><generator>CommunityServer 2.1 SP3 (Build: 20423.1)</generator><item><title>Datagridview databinding στο DefaultCellStyle.BackColor</title><link>https://www.dotnetzone.gr:443/cs/forums/thread/20452.aspx</link><pubDate>Thu, 16 Nov 2006 20:37:42 GMT</pubDate><guid isPermaLink="false">2622095e-976c-431a-859e-16783ec7ecd7:20452</guid><dc:creator>KelMan</dc:creator><slash:comments>0</slash:comments><comments>https://www.dotnetzone.gr:443/cs/forums/thread/20452.aspx</comments><wfw:commentRss>https://www.dotnetzone.gr:443/cs/forums/commentrss.aspx?SectionID=12&amp;PostID=20452</wfw:commentRss><description>&lt;P&gt;Λοιπόν, το σενάριο:&lt;/P&gt;
&lt;P&gt;Έχουμε έναν πίνακα που έχει ας πούμε Customers. Θέλουμε να αλλάζουμε το χρώμα του κάθε DataGridViewRow βάσει της τιμής σε κάποιο πεδίο (πχ CredibilityID). Αυτό το πεδίο είναι FK και τα αντίστοιχα χρώματα βρίσκονται στον master πίνακα. &lt;/P&gt;
&lt;P&gt;Μέχρι τώρα, φτιάχνω ένα dictionary που έχει ως key το FK ID και ως value το System.Drawing.Color που διαβάζω από το master πίνακα. Κατόπιν με ένα loop στο Rows collection του DataGridView κάνω set το Βackcolor χρησιμοποιώντας το FK ID&lt;/P&gt;
&lt;P&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;        &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Dim&lt;/span&gt; myColorList &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;As&lt;/span&gt; Dictionary(Of &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Integer&lt;/span&gt;, System.Drawing.Color) &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;=&lt;/span&gt; GetColorList()&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;For&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Each&lt;/span&gt; row &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;As&lt;/span&gt; DataGridViewRow &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;In&lt;/span&gt; CustomerDataGridView.Rows&lt;br /&gt;            &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Dim&lt;/span&gt; colorKey &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;As&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Integer&lt;/span&gt; &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;=&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;CType&lt;/span&gt;(row.Cells.&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Item&lt;/span&gt;(0).Value, &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Integer&lt;/span&gt;)&lt;br /&gt;            row.DefaultCellStyle.BackColor &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;=&lt;/span&gt; myColorList(colorKey)&lt;br /&gt;        &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Next&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;H λύση είναι αξιοπρεπής δεδομένου ότι το DataGridView δείχνει μερικές δεκάδες γραμμές και είναι για read-only use.&lt;/P&gt;
&lt;P&gt;Αυτό όμως που έχω φαγωθεί να κάνω είναι να πετύχω να γίνεται set το Backcolor μέσω databinding. Σε ένα textbox, αυτό γίνεται κάπως έτσι:&lt;/P&gt;
&lt;P&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Me&lt;/span&gt;.TextBox1.DataBindings.&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Add&lt;/span&gt;(&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;New&lt;/span&gt; System.Windows.Forms.Binding(&lt;span style="color: #666666;background-color: #e4e4e4;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;"BackColor"&lt;/span&gt;, &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Me&lt;/span&gt;.CustomerBindingSource, &lt;span style="color: #666666;background-color: #e4e4e4;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;"CredibilityColor"&lt;/span&gt;, &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;True&lt;/span&gt;))&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Όμως στο DataGridView υπάρχουν δύο tricky σημεία:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Το χρώμα βρίσκεται στον master πίνακα (οκ, αυτό είναι doable, από&amp;nbsp;detail row μπορούμε να βγούμε στη master)&lt;/LI&gt;
&lt;LI&gt;Πρέπει να πεις κάτι σαν &lt;/LI&gt;&lt;/UL&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt; CustomerDataGridView.DataBindings.&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Add&lt;/span&gt;(&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;New&lt;/span&gt; System.Windows.Forms.Binding(&lt;span style="color: #666666;background-color: #e4e4e4;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;"DefaultCellStyle.BackColor"&lt;/span&gt;, &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;Me&lt;/span&gt;.CustomerBindingSource, &lt;span style="color: #666666;background-color: #e4e4e4;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;"CredibilityColor"&lt;/span&gt;, &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;True&lt;/span&gt;))&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;που όμως το "DefaultCellStyle.BackColor" δεν μπορεί να το κάνει resolve.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Μένει να το ψάξω μήπως γίνεται να παίξει πάνω σε κάποιο event κατά το databinding. Αν έχει κάποιος καμιά άλλη ιδέα...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item></channel></rss>