asked on
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
double x = 0;
double y = 0;
int index1 = 0;
int a = 0;
double[] array = new double[12];
while (x <= 2)
{
index1 = 0;
y = 0;
while (y <= 2)
{
array[index1] = Math.Cos(x) + Math.Sin(y);
y = y + 0.2;
index1++;
}
dataGridView1.Rows.Add(array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10], array[11]);
dataGridView1.Rows[a].HeaderCell.Value = Convert.ToString(x);
a++;
x = x + 0.2;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
table.JPG
ASKER
List<Decimal> maxes = new List<Decimal>()
foreach (DataRow row in datagridview.Rows())
{
maxes.Add(FindMax(row)); // FindMax function will return the value of the max in a row
}
Now maxes[0] will be the max in the first row.ASKER
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace EE_Q29065398
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void OnLoad(object sender, EventArgs e)
{
var tangents = (from x in Enumerable.Range(0, 11) select
(from y in Enumerable.Range(0, 11)
select new KeyValuePair<string, double>($"{y * .2}", Math.Cos(x * .2) + Math.Sin(y * .2))).ToDictionary(k => k.Key, v => v.Value));
var table = new DataTable("Tangents");
table.Columns.AddRange(tangents.First().Select(c => new DataColumn { ColumnName = c.Key, DataType = typeof(double) }).ToArray());
tangents.ToList().ForEach(r => table.Rows.Add(r.Select(c => c.Value).Cast<object>().ToArray()));
dataGridView1.DataSource = table;
}
private void OnDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (sender is DataGridView)
{
var grid = sender as DataGridView;
if (grid.Rows.Count > 0)
{
foreach (var row in grid.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow))
row.HeaderCell.Value = grid.Columns[row.Index].HeaderCell.Value;
}
}
}
private void OnClick(object sender, EventArgs e)
{
var maxValues = (from row in dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow)
select $"Row {row.Index} has a Max Value of {(row.DataBoundItem as DataRowView).Row.ItemArray.Cast<double>().Max()}");
MessageBox.Show(string.Join(Environment.NewLine, maxValues.ToArray()));
}
}
}
Form1.Designer.cs -
namespace EE_Q29065398
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 60;
this.dataGridView1.Size = new System.Drawing.Size(1162, 333);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.OnDataBindingComplete);
//
// button1
//
this.button1.Location = new System.Drawing.Point(989, 351);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(166, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Get Row Max Values";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.OnClick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1185, 386);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.OnLoad);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
}
}
Produces the following results -ASKER
double cmax = 0.0; //Caution - if numbers can be negative use a large negative number
foreach (DataGridViewCell c in dataGridView1.Rows[0].Cells) //replace zero with the row index you require
{
if (cmax < Convert.ToDouble(c.Value))
{
cmax = Convert.ToDouble(c.Value);
label1.Text = "Max value: " + cmax;
}
}
ASKER
label1.Text = "Max value: " + cmax;
Can I have:label[some index? How to write it?].Text = "Max value: " + cmax;
?
ASKER
ASKER
double cmax = -5.0;
int indexx = 0;
foreach (DataGridViewCell c in dataGridView1.Rows[indexx].Cells)
{
if (c.Equals("MaxReiksme")) // Max = name of column
continue; // skip this loop iteration
if (cmax < Convert.ToDouble(c.Value))
{
cmax = Convert.ToDouble(c.Value);
//label1.Text = "Max value: " + cmax;
dataGridView1.Rows[12].Cells.Add(cmax);
indexx++;
}
}
ASKER
ASKER
dataGridView1.Rows[indexx].Cells[12].Value = cmax.ToString();
12 because it's column number or name: "Column12" because MaxReiksme is only a header name
ASKER
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace EE_Q29065398
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void OnLoad(object sender, EventArgs e)
{
var tangents = (from x in Enumerable.Range(0, 11) select
(from y in Enumerable.Range(0, 11)
select new KeyValuePair<string, double>($"{y * .2}", Math.Cos(x * .2) + Math.Sin(y * .2))).ToDictionary(k => k.Key, v => v.Value).AddMaxColumn());
var table = new DataTable("Tangents");
table.Columns.AddRange(tangents.First().Select(c => new DataColumn { ColumnName = c.Key, DataType = typeof(double) }).ToArray());
tangents.ToList().ForEach(r => table.Rows.Add(r.Select(c => c.Value).Cast<object>().ToArray()));
dataGridView1.DataSource = table;
}
private void OnDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (sender is DataGridView)
{
var grid = sender as DataGridView;
if (grid.Rows.Count > 0)
{
foreach (var row in grid.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow))
row.HeaderCell.Value = grid.Columns[row.Index].HeaderCell.Value;
}
}
}
private void OnClick(object sender, EventArgs e)
{
var maxValues = (from row in dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => !r.IsNewRow)
select $"Row {row.Index} has a Max Value of {(row.DataBoundItem as DataRowView).Row.ItemArray.Take(10).Cast<double>().Max()}");
MessageBox.Show(string.Join(Environment.NewLine, maxValues.ToArray()));
}
}
static class Extensions
{
public static Dictionary<string, V> AddMaxColumn<V>(this Dictionary<string, V> source) where V : IComparable
{
source.Add("MaxValue", source.Aggregate((l, r) => l.Value.CompareTo(r.Value) > 0 ? l : r).Value);
return source;
}
}
}
Which produces the following output - private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
this.dataGridView1.Rows.Add(new object[] { r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0 });
this.dataGridView1.Rows.Add(new object[] { r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0 });
this.dataGridView1.Rows.Add(new object[] { r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0 });
this.dataGridView1.Rows.Add(new object[] { r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0 });
this.dataGridView1.Rows.Add(new object[] { r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0 });
this.dataGridView1.Rows.Add(new object[] { r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0, r.Next(1000) / 100.0 });
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow r in this.dataGridView1.Rows)
{
double max = 0.0;
for(int i = 0; i < 6; i++)
{
if (max < Convert.ToDouble(r.Cells[i].Value))
max = Convert.ToDouble(r.Cells[i].Value);
}
r.Cells[6].Value = max.ToString();
}
}
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).
TRUSTED BY
ASKER
With this:
Open in new window
in foreach loop? When Rows.[] index will be element in foreach?