We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to check duplicates for a column using GridDataBoundGrid using C#2010?

Hi there, I am doing Checking duplicates by using the following code in Normal Datagridview

    if (dgvwDtls.Columns[this.dgvwDtls.CurrentCell.ColumnIndex].Name == "Column2")
            {
                if (!string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    foreach (DataGridViewRow dgvwRw in dgvwDtls.Rows)
                    {
                        if (e.RowIndex != dgvwRw.Index)
                        {
                            if (e.FormattedValue.ToString() == dgvwRw.Cells["Column2"].FormattedValue.ToString())
                            {
                                MessageBox.Show(e.FormattedValue.ToString() + " is already entered", Classes.Gnrlprps._MsgCptn);
                                dgvwRw.Cells["Column2"].Value = string.Empty;
                                e.Cancel = true;
                                return;
                            }
                        }
                    }
                }
            }

But in sync fusion how to use foreach, i just tried like this
 GridCurrentCell cc = this.gdbgrDtls.CurrentCell;
            if (cc.ColIndex == 2)
            {
                if (!string.IsNullOrEmpty(this.gdbgrDtls[cc.RowIndex, cc.ColIndex].FormattedText))
                {
                    foreach (DataGridViewRow dgvwRw in dgvwDtls.Rows)
                    {
                        if (e.RowIndex != dgvwRw.Index)
                        {
                            if (e.FormattedValue.ToString() == dgvwRw.Cells["Column2"].FormattedValue.ToString())
                            {
                                MessageBox.Show(e.FormattedValue.ToString() + " is already entered", Classes.Gnrlprps._MsgCptn);
                                dgvwRw.Cells["Column2"].Value = string.Empty;
                                e.Cancel = true;
                                return;
                            }
                        }
                    }
                }
            }


1 Reply

SA Solai A L Syncfusion Team May 10, 2014 09:35 AM UTC

Hi Venkata,

 

Sorry for the delay.

 

Kindly refer the below provided code snippet and sample  for validating the cell duplicates. We have prepared the code snippet similar to the one provided by you. In this code we have checked the values of a specific column with the current cell. Please let us know whether the given solution achieves your requirement.

 

 

void gridDataBoundGrid1_CellClick(object sender, Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e)

        {

            GridCurrentCell cc=this.gridDataBoundGrid1.CurrentCell;

            if (e.ColIndex == 1)

            {

                if (!string.IsNullOrEmpty(this.gridDataBoundGrid1[cc.RowIndex, cc.ColIndex].FormattedText))

                {

                    for (int i = 0; i < gridDataBoundGrid1.Model.RowCount;i++ )

                    {

                        if(e.RowIndex!=i)

                        {

                            if (this.gridDataBoundGrid1[cc.RowIndex, cc.ColIndex].CellValue.ToString() == gridDataBoundGrid1[i, e.ColIndex].CellValue.ToString())

                            {                                                  

                                MessageBox.Show(this.gridDataBoundGrid1[cc.RowIndex, cc.ColIndex].CellValue.ToString() + " is already entered");

                                this.gridDataBoundGrid1[cc.RowIndex, cc.ColIndex].CellValue = string.Empty;

                                e.Cancel = true;

                                return;

                            }

                        }

                    }

                }

            }

        }

 

Please let us know if you have any other concerns.

 

Thanks & Regards,

AL.Solai.


Attachment: WindowsFormsApplication8_ca49712b.zip

Loader.
Live Chat Icon For mobile
Up arrow icon