Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 24th, 2007, 05:50 AM
Authorized User
 
Join Date: Oct 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataGridView Loop + Compare

Hi,

I have 2 columns in my DataGridView.

The first column - "IDColumn" is populated from a datasource unrelated to the other column.

I want to use each "IDColumn" value to compare against values returned from another Dataset query (myDataSet).

Then if there's a match I want the image to display in the "MessageColumn" only.

How do I do this? Thanks.

My code below displays the image in both columns.


Code:
private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
for (int iRow = 0; iRow < myDataGridView.Rows.Count -1; iRow++)
{

string SelectedID = myDataGridView.Rows[iRow].Cells["IDColumn"].Value.ToString();

foreach (DataRow myRow in this.myDataSet.Tables["Approved"].Rows)
{
if (myRow["Portfolio"].ToString() == SelectedID)
{
if (myDataGridView.Columns[e.ColumnIndex].Name == "MessageColumn")
{
e.Value = Properties.Resources.LightGreen;
}
}
}
}
}
 
Old December 30th, 2007, 07:53 PM
Registered User
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

im not sure if you want to simply change the color of the cell in a row that has a matching profolio id of the approved portfolio's, but it should probably done a bit differently. Currently your code requires # of items in the DataGridView * # of items in the DataSet. Whats worse is that you are trapping the CellFormatting handler which is fired each time a row needs to be repainted.

Quick Solution:
Replace
Code:
e.Value = Properties.Resources.LightGreen;
 with
Code:
try{
 myDataGridView.Rows[iRow].Cells["MessageColumn"].Style.BackColor = Color.LightGreen;
} catch (Exception e){ Console.WriteLine(e.Message); }
Long Solution:

Rewrite this to trap the DataSourceChanged or CellValueChanged depending on other factors of your app. When filling your dataset use adapter.FillSchema(ds) first to gather any primary keys and metadata. Assuming you have primary keys defined, you can query the dataset for specific items using (Assuming your DataSet is named ds)
Code:
ds.Tables[0].Rows.Find({PrimaryKey})
or
Code:
ds.Tables[0].Select ("Portfolio=" + SelectedID); //Returns a DataRow[] array but in your case should only have 1 row
then you can simply say
Code:
if(ds.Tables[0].Select ("Portfolio=" + SelectedID) != null){
//then a match was found; mark this row
  myDataGridView.Rows[iRow].Cells["MessageColumn"].Style.BackColor = Color.LightGreen; 
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compare the value elayaraja.s XSLT 3 July 28th, 2008 07:48 AM
compare Neha XSLT 6 July 24th, 2008 08:11 AM
DataGridView Loop obrienkev C# 2005 1 November 1st, 2007 12:07 PM
compare these date fields and compare and get the susanring Oracle 1 July 24th, 2006 04:58 PM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.