Torna al Thread
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 VerificaDatiDataGridView
{
public partial class Form1 : Form
{
private Boolean caricamentoDB = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InizializzaDatiDataGridWiew();
}
private void InizializzaDatiDataGridWiew()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("colID", typeof(int)));
dt.Columns.Add(new DataColumn("colName", typeof(string)));
dt.Rows.Add("1", "Mario Rossi");
dt.Rows.Add("2", "Mario Bianchi");
dt.Rows.Add("3", "Mario Verdi");
dataGridView1.DataSource = dt;
caricamentoDB = true;
}
private int trovaRigaValore(String searchValue, int rigaDaEscludere=-1)
{
int rowIndex = -1; //Info row
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Index != rigaDaEscludere) {
if (row.Cells[1].Value.ToString().Equals(searchValue))
{
rowIndex = row.Index;
//her you got the cell to work with
return rowIndex;
}
}
}
return rowIndex;
}
private Boolean cercaValoreColonna(String searchValue,int rigaDaEscludere=-1) {
//int rowIndex = -1; //Info row
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!((row.Cells["colID"].Value == null) && row.Cells["colID"].EditedFormattedValue.ToString() == string.Empty)) {
if (rigaDaEscludere != -1)
{
if (row.Index != rigaDaEscludere)
{
if (row.Cells["colID"].Value.ToString().Equals(searchValue))
{
//rowIndex = row.Index;
//her you got the cell to work with
return true;
}
}
}
}
}
return false;
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (caricamentoDB == true)
{
if (cercaValoreColonna(dataGridView1.Rows[e.RowIndex].Cells["colID"].Value.ToString(),e.RowIndex) == true)
{
Console.Write("errore");
dataGridView1.Rows[e.RowIndex].ErrorText = "Valore ID già presente nel DataGridView";
}
}
}
}
}