-1

I'm trying to eliminate or put empty an value in my DataGridview if the cell is duplicated. I saw an example but it used GridView in my case is DataGridview. Here is that code:

 public void atualiza()
    {
        try
        {
            SqlConnection con = new SqlConnection(Login.conectData);
            con.Open();
            dsFerramenta = new DataSet();
            daFerramenta = new SqlDataAdapter("SELECT Ferramentas.Ferramenta_ID, Ferramentas_Terminais.Terminal_ID,     Ferramentas_Grupos.Grupo_ID, Vedante_ID,                   Imagem, Nome_Afinação, Vedante, Observações, Data_Criação, Utilizador FROM Ferramentas "+ "JOIN Ferramentas_Terminais ON Ferramentas_Terminais.Ferramenta_ID = Ferramentas.ferramenta_ID " +
                "LEFT JOIN Ferramentas_Grupos ON Ferramentas_Grupos.Ferramenta_ID = Ferramentas.Ferramenta_ID " +
                "LEFT JOIN Ferramentas_Vedantes ON Ferramentas_Vedantes.Ferramenta_ID = Ferramentas.Ferramenta_ID " +
                "LEFT JOIN FormasCravação ON FormasCravação.Cravação_ID = Ferramentas.Cravação_ID " +
                "LEFT JOIN TipoAfinação ON TipoAfinação.Afinação_ID = Ferramentas.Afinação_ID", con);
            dsFerramenta.Clear();

            daFerramenta.Fill(dsFerramenta, scrollVal, 100, "Ferramentas".Trim());
            tabelaRelac.DataSource = dsFerramenta;

            tabelaRelac.DataMember = "Ferramentas";
            //para organizar as colunas na tabela

            tabelaRelac.Columns["Terminal_ID"].DisplayIndex = 1;
            tabelaRelac.Columns["Grupo_ID"].DisplayIndex = 2;
            tabelaRelac.Columns["Vedante_ID"].DisplayIndex = 3;
            tabelaRelac.Columns["Vedante"].DisplayIndex = 4;
            tabelaRelac.Columns["Nome_Afinação"].DisplayIndex = 5;
            tabelaRelac.Columns["Imagem"].DisplayIndex = 6;
            tabelaRelac.Columns["Data_Criação"].DisplayIndex = 7;
            tabelaRelac.Columns["Observações"].DisplayIndex = 8;
            tabelaRelac.Columns["Utilizador"].DisplayIndex = 9;

            con.Close();

            string doubleValue = tabelaRelac.Rows[0].Cells[0].ToString().Trim();
            for (int i = 0; i < tabelaRelac.Rows.Count; i++)
            {
                if (tabelaRelac.Rows[i].Cells[0].ToString() == doubleValue)
                {
                    string a = tabelaRelac.Rows[i].Cells[0].ToString().Trim();
                    a = string.Empty;
                }
                else
                {
                    doubleValue=tabelaRelac.Rows[i].Cells[0].Value.ToString().Trim();

                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        tabelaRelac.ClearSelection();
    }

After the con.close(), is my code to eliminate duplicate cell and put an empty value.

I would like some Help.

I use this Video reference to try

I'm using ToString() because DataGridView does't have proprety Text

Thank you.

Lennart
  • 9,657
  • 16
  • 68
  • 84

1 Answers1

0

It is better to handle dublicates in your SQL Query.

Also, Please keep in mind that It is very dangerous to use String Concatenation on SQL Query Generation wince it is open to SQL Injection attacks. Instead use Parameterized Queries.

If you insist on removing dublicates at DataTable level, then I suggest you to look at Ratty's Answer

Here is the code piece from Ratty's post

public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
   Hashtable hTable = new Hashtable();
   ArrayList duplicateList = new ArrayList();

   //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
   //And add duplicate item value in arraylist.
   foreach (DataRow drow in dTable.Rows)
   {
      if (hTable.Contains(drow[colName]))
         duplicateList.Add(drow);
      else
         hTable.Add(drow[colName], string.Empty); 
   }

   //Removing a list of duplicate items from datatable.
   foreach (DataRow dRow in duplicateList)
      dTable.Rows.Remove(dRow);

   //Datatable which contains unique records will be return as output.
      return dTable;
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72