Comparing Rows in a VB.NET DataGridView

  • Hi,

    I have batches of 3 rows, what is the easiest way to compare these 3 rows to determine which row has the highest value in a specific column within these 3 rows please?

    Thanks,

    Best Regards,

    BMM

  • Rather obscure question ... need more information in order to assist you

    Suggest you read the article in my signature block and supply requested information so that some one may help.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • My project involves the input and calculation of horse racing related data.

    I have a button that loads a table's contents into a datagridview component - this can be

    all the data, information for a specific date or information from a specific race.

    One of the databases stores the results of a calculation which produces 3 horses from any

    given race - this database will contain 3 horses from any race stored in the main database.

    Within each set of 3 horses, I am looking for a method of comparing specific columns of

    data to find the highest value within a specific column. The above screen capture should

    provide a better understanding of what I am trying to achieve.

    If further information is required to assist me in finding a solution to this problem, then

    please let me know.

    Thanks

  • Its not a SQL Solution, but you can do it in your .NET application by enumerating the DataGridView.Rows collection and for each DataGridViewRow look at the column you want by name.

    for (int i=0; i<dgv_Actions.Rows.Count; i++)

    {

    DataGridViewRow dgvr = dgv_Actions.Rows;

    DataGridViewTextBoxCell namecell = (DataGridViewTextBoxCell)dgvr.Cells["dgvActions_Name"];

    DataGridViewCheckBoxCell chkcell = (DataGridViewCheckBoxCell)dgvr.Cells["dgvActions_Selected"];

    if (sessionevent.ActionString.Contains(namecell.Value.ToString()))

    _ActionList.Selected = true;

    }

    The above is an example from a opensource app that I am working on that is on codeplex. There is a DataGridView name dgv_Actions that has a DataGridViewTextBoxCell named dgvActions_Name and a DataGridViewCheckBoxCell named dgvActions_Selected. I use it to iterate over check the checked items and set a flag on an object based on whether the box it checked, but the code sample should be enough for you to see how to enumerate the collection, and access the specific column by name.

    To do it in TSQL, if you are on SQL 2005, you could use a series of CTE's to mark the row in each grouping with a bit 1 = max row, 0 = not max row, or you could use a table variable/temp table as a intermediate storage point, and update a bit column for the max row, and then select the data from the table variable/temp table for return to the application.

    Enumerating the collection in the .NET app may or may not be faster, but probably should be faster, depending on the complexity of the logic.

    Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
    My Blog | Twitter | MVP Profile
    Training | Consulting | Become a SQLskills Insider
    Troubleshooting SQL Server: A Guide for Accidental DBAs[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply