Search code examples
c#datagridviewcomboboxrowfilter

Unable to list the dates from filtered datagridview into combobox


My dataGridView_flaggedComments is filled with some original comments.

I then use comboBox_stockIndex_SelectedIndexChanged function to filter the comments in dataGridView_flaggedComments according to Tickers_Ticker_ID.

Next, with the filtered comments, I want to list out the comments' dates to comboBox_stockDates, using PopulateStockDatesIndex() function.

However, PopulateStockDatesIndex() function is listing all the dates (of the original non-filtered comments). How can I only list the dates that come from the filtered comments?

My code as below. Any help would be much appreciated. Thank you!

private void comboBox_stockIndex_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (DataRow db in LoadTickers().Rows)
    {
        if (comboBox_stockIndex.SelectedItem.ToString() == db["Symbol"].ToString())
        {
            (dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter = string.Format("Tickers_Ticker_ID = '{0}'", db["Ticker_ID"].ToString());
        }
    }

    PopulateStockDatesIndex();
}


private void PopulateStockDatesIndex()
{
    comboBox_stockDates.Items.Clear();
    comboBox_stockDates.Items.Add("Choose to Filter");
    comboBox_stockDates.FormatString = "dd-MM-yyyy";
    DataTable dt_filterDate = (DataTable)(dataGridView_flaggedComments.DataSource);
    foreach (DataRow row in dt_filterDate.Rows)
    {
        for (int i = 0; i < dataGridView_flaggedComments.Rows.Count - 1; i++)
        {
            if (dataGridView_flaggedComments.Rows[i].Cells["Comments_Date"].Value.ToString() != "")
            {
                string date = row.Field<DateTime>(1).ToString("dd-MM-yyyy");
                if (!comboBox_stockDates.Items.Contains(date))
                {
                    comboBox_stockDates.Items.Add(date);
                }
            }
        }
    }
}

P/S: I tried my best to explain my question and put my question into proper formatting and I have also done quite some research regarding this, kindly don't downvote me and let me know where I did wrongly or if my question is not clear enough. I apologise if there are similar questions!


Solution

  • private void PopulateStockDatesIndex()
    {
        comboBox_stockDates.Items.Clear();
        comboBox_stockDates.Items.Add("Choose to Filter");
    
        foreach (DataGridViewRow row in dataGridView_flaggedComments.Rows)
        {
            for (int i = 0; i < dataGridView_flaggedComments.Rows.Count - 1; i++)
            {
                if (dataGridView_flaggedComments.Rows[i].Cells["Comments_Date"].Value.ToString() != "")
                {
                    string str = dataGridView_flaggedComments.Rows[i].Cells["Comments_Date"].Value.ToString();
                    DateTime date = DateTime.ParseExact(str, "dd/MM/yyyy h:mm:ss tt", CultureInfo.GetCultureInfo("en-GB"));
                    if (!comboBox_stockDates.Items.Contains(date.ToString("dd/MM/yyyy")))
                    {
                        comboBox_stockDates.Items.Add(date.ToString("dd/MM/yyyy"));
    
                    }
                    comboBox_stockDates.SelectedIndex = 0;
                }
            }
        }
    }