Link to home
Create AccountLog in
C#

C#

--

Questions

--

Followers

Top Experts

Avatar of Filip Dostál
Filip Dostál

C# MIN MAX VALUE DATAGRIDVIEW ROW
Hello,


how to find min and max value in datagridview (in row)

Thanks in advance!

User generated image

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Pawan KumarPawan Kumar🇮🇳

Pls try -

var Mins = yourDataTable.AsEnumerable().Max(row => Convert.ToInt32(row["MERENI"]));
var Maxs = yourDataTable.AsEnumerable().Min(row => Convert.ToInt32(row["MERENI"]));

Open in new window


Avatar of Filip DostálFilip Dostál

ASKER

how min/max value transfer to textbox?

Avatar of Pawan KumarPawan Kumar🇮🇳

try like..

Txt.Value = (string) Mins

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Pawan KumarPawan Kumar🇮🇳

Txt.Value = (string) Mins;

this code find min value in column - how find min/max value in row?

 int min = 0;
            for (int i = 0; i <= dotaz4DataGridView.Rows.Count - 1; i++)
            {
                if (i == 0)
                {
                    min = int.Parse(dotaz4DataGridView.Rows[i].Cells[0].Value.ToString());
                }

                if (min > int.Parse(dotaz4DataGridView.Rows[i].Cells[0].Value.ToString()))
                {
                    min = int.Parse(dotaz4DataGridView.Rows[i].Cells[0].Value.ToString());
                }
            }
            textBox3.Text = min.ToString();

Open in new window


Avatar of Pawan KumarPawan Kumar🇮🇳

Try like this-

for (int i = 0; i <= dotaz4DataGridView.rows.Count - 1; i++)
{
	for (int j = 0; j <= dotaz4DataGridView.Columns.Count - 1; j++)
	{
		if (i == 0)
		{
			min = int.Parse(dotaz4DataGridView.Rows[i][j].Cells[0].Value.ToString());
		}

		if (min > int.Parse(dotaz4DataGridView.Rows[i][j].Cells[0].Value.ToString()))
		{
			min = int.Parse(dotaz4DataGridView.Rows[i][j].Cells[0].Value.ToString());
		}
	}
}
textBox3.Text = min.ToString();			

Open in new window


Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


User generated image Not work

Avatar of Fernando SotoFernando Soto🇺🇸

Hi Filip;

I think this is what you may be looking for. This assumes that the values in the row are all strings
// From each row in the DataTable find the min and max value of the row
var results = (from DataRow row in dt.AsEnumerable()
               select new
               {
                   Min = row.ItemArray.Where((r, idx) => idx != 0).Min(c => Int32.Parse(c.ToString())),
                   Max = row.ItemArray.Where((r, idx) => idx != 0).Max(c => Int32.Parse(c.ToString()))
               });

// The variable results returns an Anonymous type holding two values Min and Max value of each row
foreach (var minMax in results)
{
    // And to access the Min and Max of the Anonymous type you can do as below
    Console.WriteLine("Min = {0}", minMax.Min);    
    Console.WriteLine("Max = {0}\n", minMax.Max);
}

Open in new window


Hello Fernando,

It is possible to write min / max value from datagrid (from one row) into textbox?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


How are you binding the data to the DataGridView? Do you load a DataTable object and then assign it to the DataGridView.DataSource? How are you loading the DataGridView with data?

Hi Filip;

Try this to see if it works for  you.
var results = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
               where !row.IsNewRow
               let cells = row.Cells.Cast<DataGridViewCell>()
               let Min = cells.Where((c, idx) => idx != 0).Min(c => Int32.Parse(c.Value.ToString()))
               let Max = cells.Where((c, idx) => idx != 0).Max(c => Int32.Parse(c.Value.ToString()))
               select new
               {
                   Min,
                   Max
               }).ToList();


foreach (var minMax in results)
{
    // And to access the Min and Max of the Anonymous type you can do as below
    Console.WriteLine("Min = {0}", minMax.Min);
    Console.WriteLine("Max = {0}\n", minMax.Max);
}

Open in new window


My data source is dotaz4BindingSource.

User generated image

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


That does not answer my questions.

Did the code I last posted which queries the DataGridView, did that work?

If it did not please post the code you used to load the data into the DataGridView.

What collection do you connect to the BindingSource dotaz4BindingSource?

here is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dotaz4BindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.dotaz4BindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.dB_KROUTAKUDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dB_KROUTAKUDataSet.dotaz4' table. You can move, or remove it, as needed.
            this.dotaz4TableAdapter.Fill(this.dB_KROUTAKUDataSet.dotaz4);

        }
    }
}

Open in new window


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


I do not know how to find min/max value from one row in datagridview. I need a min / max value display in a textbox.

OK, lets see if I can get some answers. Did the following code work when you implemented in your project? If it did not please post the exception and inner exception.
var results = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
               where !row.IsNewRow
               let cells = row.Cells.Cast<DataGridViewCell>()
               let Min = cells.Where((c, idx) => idx != 0).Min(c => Int32.Parse(c.Value.ToString()))
               let Max = cells.Where((c, idx) => idx != 0).Max(c => Int32.Parse(c.Value.ToString()))
               select new
               {
                   Min,
                   Max
               }).ToList();


foreach (var minMax in results)
{
    // And to access the Min and Max of the Anonymous type you can do as below
    Console.WriteLine("Min = {0}", minMax.Min);
    Console.WriteLine("Max = {0}\n", minMax.Max);
}

Open in new window


User generated image here is a message

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


I don't know if I can help, it looks like the error message is in a different language.

can you zip up the complete project so I can see if I can figure out what is happening and post it to a site where I can download it from such as Google Docs or some other.

ASKER CERTIFIED SOLUTION
Avatar of Fernando SotoFernando Soto🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Fernado, work it. Thank you. I am indebted to you... :)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Not a problem Filip, glad to help.

https://www.dropbox.com/s/udn7ol6mt2y3l8y/MIN_MAX_TEST%202.zip?dl=0

Hello Fernando,
How to modify a code so I can also use decimal numbers?

Hi Filip;

Please open a new question for this new requirement and when I get some time today I will look at it.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


I modified the code:

let Min = cells.Where((c, idx) => idx != 1).Min(c => Convert.ToDecimal(c.Value.ToString()))
let Max = cells.Where((c, idx) => idx != 1).Max(c => Convert.ToDecimal(c.Value.ToString()))

Open in new window


Work it

Hi Filip;

By changing this section of the two lines from this, (c, idx) => idx != 0, to this, (c, idx) => idx != 1, you are removing the column 1_MERENI from the calculation and adding the column PROVEDL_MERENI to the calculation. Is this what you want to be doing?
C#

C#

--

Questions

--

Followers

Top Experts

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).