'2 same value on gridview

hello i am currently having search button on my form and i want to search is between 2 date and i want to have a alert message if i have 2 same record on other column this is my code for now

SqlConnection sqlCon = new SqlConnection(ConnectionString);
        if (sqlCon.State == ConnectionState.Closed)
            sqlCon.Open();
        SqlDataAdapter sqlData = new SqlDataAdapter("DateFilter", sqlCon);
        sqlData.SelectCommand.CommandType = CommandType.StoredProcedure;
        sqlData.SelectCommand.Parameters.AddWithValue("@Date", TxtFromDate.Text);
        sqlData.SelectCommand.Parameters.AddWithValue("@Date2", TxtToDate.Text);
        DataTable dtbl = new DataTable();
        sqlData.Fill(dtbl);
        sqlCon.Close();
        Gridview1.DataSource = dtbl;
        Gridview1.DataBind();
        Gridview1.UseAccessibleHeader = true;
        Gridview1.HeaderRow.TableSection = TableRowSection.TableHeader;


Solution 1:[1]

It is really hard decrypting your question. It seems you have an existing SQL result in your DataGridView and then want to check via a button if there are multiple entries with the same "CreatedDate" value.

(1) You should use defined objects in order to be able to work with object items instead of DataTable row/column indexes which is a hassle.

Therefore you need to define your object, map your DataTable rows as objects items in a collection (here: a simple List<>) and then set this collection to your DataGridView.DataSource, e.g.:

// define single object
public class FinishedGood {
  public int      ID          { get; set; }
  public DateTime CreatedDate { get; set; }
  ...
}

// define object collection
public class FinishedGoods : List<FinishedGood> { }

// execute your existing SQL query and fill the DataTable
...

// convert DataTable to FinishedGoods
FinishedGoods finishedGoods = new FinishedGoods();
foreach (DataRow row in dtbl.Rows) {
  finishedGoods.Add(new FinishedGood() {
    ID          = Convert.ToInt32(row["ID"]),
    CreatedDate = DateTime.Parse(row["CreatedDate"].ToString());,
    ...
  });
}

// set the collection as datasource
gv.DataSource = finishedGoods;

(2) Now, you can check the DataSource for duplicates via Linq:

using System.Linq;

private HashSet<FinishedGood> CheckForDuplicateCreatedDates() {
  HashSet<FinishedGood> result = new HashSet<FinishedGood>();

  // collect all createdDates
  HashSet<DateTime> createdDates = new HashSet<DateTime>();
  foreach (FinishedGood finishedGood in gv.DataSource){
    if (!createdDates.Contains(finishedGood.createdDate)) {
      createdDates.Add(finishedGood.createdDate);
    }
  }

  // loop through all createdDates
  foreach (DateTime createdDate in createdDates) {
    // check if there are more than 2 entries with the createdDate in your DataSource
    if (gv.DataSource.Count(x => x.CreatedDate == createdDate) > 2) {
      // add those entries to your duplicate result list
      foreach (FinishedGood finishedGood in gv.DataSource.Where(x => x.CreatedDate == createdDate)) {
        result.Add(finishedGood);
      }
    }
  }

  return result;
}

(3) To show the alert popup, you can use MessageBox.Show().

(4) To highlight the corresponding rows in the DataGridview, you can use the result of (2) to find the corresponding row indexes and then adjust their DefaultCellStyle.

The button click would look something like this:

private void buttonCheck_Click(object sender, EventArgs e) {
  // get duplicates
  HashSet<FinishedGood> duplicates = CheckForDuplicateCreatedDates();

  if (duplicates.Count() > 0) {
    // show alert popup
    MessageBox.Show("Duplicates found");

    // highlight the corresponding rows
    foreach (DataGridViewRow row in gv.Rows) {
      if (duplicates.Contains((row.DataBoundItem as FinishedGood))) {
        row.DefaultCellStyle.BackColor = Color.DarkRed;
        row.DefaultCellStyle.ForeColor = Color.White;
      }
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1