C# - How To Get The Min Value In DataGridView Column Using C#
In This C# Tutorial We Will See How To Get The Minimum Value From DataGridView Column And Display It In TextBox Using CSharp Programming Language.
Project Source 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 WindowsFormsApplication1
{
public partial class Csharp_Datagridview_Column_Cells_Min : Form
{
public Csharp_Datagridview_Column_Cells_Min()
{
InitializeComponent();
}
private void Csharp_Datagridview_Column_Cells_Min_Load(object sender, EventArgs e)
{
// populate datagridview with some data
Random r = new Random();
for (int i = 0; i <= 10; i++)
{
dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(), r.Next(20, 65).ToString());
}
dataGridView1.AllowUserToAddRows = false;
// method 1
textBoxMin.Text = (from DataGridViewRow row in dataGridView1.Rows
where row.Cells[2].FormattedValue.ToString() != string.Empty
select Convert.ToInt32(row.Cells[2].FormattedValue)).Min().ToString();
// method 2
int min = 0;
for (int i = 0; i <= dataGridView1.Rows.Count-1; i++ )
{
if(i == 0)
{
min = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
}
if(min > int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString()))
{
min = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
}
}
textBoxMin.Text = min.ToString();
// method 3
int[] columnData = new int[dataGridView1.Rows.Count];
columnData = (from DataGridViewRow row in dataGridView1.Rows
where row.Cells[2].FormattedValue.ToString() != string.Empty
select Convert.ToInt32(row.Cells[2].FormattedValue)).ToArray();
textBoxMin.Text = columnData.Min().ToString();
}
}
}
///////////////OUTPUT:
c# datagridview column minimum value |
Bagikan
C# - How To Get The Minimum Value From DataGridView Column Using C#
4/
5
Oleh
insurance