Wednesday, 30 October 2013

How to Check Duplicate Item Names in Data Grid

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex == dataGridView1.Columns["MyCombo"].Index)
    {
        var query = from DataGridViewRow row in dataGridView1.Rows
                    where row.Cells[e.ColumnIndex].Value != null && row.Cells[e.ColumnIndex].Value.ToString() == e.FormattedValue.ToString()
                    where row.Index != e.RowIndex
                    select row;

        if (query.Any())
        {
            MessageBox.Show(string.Format("{0} already exists", e.FormattedValue.ToString()));
            e.Cancel = true;
        }
    }
}

No comments:

Post a Comment

SQL SERVER – Disk Space Monitoring – Detecting Low Disk Space on Server

CREATE PROCEDURE [CSMSDVLP].[DiskSpaceMonitor] @mailProfile nvarchar(500), @mailto nvarchar(4000), @threshold INT, @logfile nvarchar(40...