Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Thank you for your time

I have 2 Datagrids dg1 populates times for deliveries. dg2 has lunch times that needs to be excluded from dg1. After days of research i cant find a good example. Suggestions please.

What I have tried:

Days
of
research
on
array
Posted
Updated 4-Dec-16 10:20am
Comments
CHill60 4-Dec-16 14:16pm    
How are you populating dg1?
Member 12349103 4-Dec-16 15:02pm    
From textbox Combo and a datetime picker
CHill60 4-Dec-16 15:10pm    
... as in, post the code you are using to populate dg1
Member 12349103 4-Dec-16 15:15pm    
private void button2_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(tbSnp_Uld.Text) && !string.IsNullOrEmpty(cbSnp_Uld.Text))
{
double d_tbSnp_Uld = Convert.ToDouble(tbSnp_Uld.Text);
double d_cbSnp_Uld = Convert.ToDouble(cbSnp_Uld.Text);
double result1 = Math.Round(d_tbSnp_Uld / d_cbSnp_Uld);
tbTrailer_Needed.Text = result1.ToString();
}
if (!String.IsNullOrEmpty(cbDaily_Prod.Text) && !string.IsNullOrEmpty(tbTrailer_Needed.Text))
{
double d_cbDaily_Prod = Convert.ToDouble(cbDaily_Prod.Text);
double d_tbTrailer_Needed = Convert.ToDouble(tbTrailer_Needed.Text);
double result2 = d_cbDaily_Prod / d_tbTrailer_Needed;
tbTime_Between.Text = result2.ToString();

}


var start_time = dateTimePicker1.Value.ToString("HH:mm");
var start_time_array = start_time.Split(':');
var today = DateTime.Now;
var trailer_count = Convert.ToInt32(tbTrailer_Needed.Text);
var minutes_apart = Convert.ToDouble(tbTime_Between.Text);
var calculated_start_time = new DateTime(today.Year, today.Month, today.Day, Convert.ToInt16(start_time_array[0]), Convert.ToInt16(start_time_array[1]), 00);


var trailers = new List<trailer__time>();
for (var i = 0; i < trailer_count; i++)
{
if (i == 0)
{
calculated_start_time = calculated_start_time.AddMinutes(0);
}
else
{
calculated_start_time = calculated_start_time.AddMinutes(minutes_apart);
}

trailers.Add(new Trailer__Time
{
Trailer_Number = i,
Delivery_Time = calculated_start_time.ToString("HH:mm")
});
}

dataGridView1 .DataSource = trailers;
}

1 solution

Probably the simplest (and clearest way) is to only insert into the trailers if the time is not contained in the list of lunchtimes. For example:
C#
var timesToAvoid = (List<string>)dataGridView2.DataSource;
var trailers = new List<trailer__time>();
for (var i = 0; i < trailer_count; i++)
{
    calculated_start_time = calculated_start_time.AddMinutes(i == 0 ? 0 : minutes_apart);

    if (!timesToAvoid.Contains(calculated_start_time.ToString("HH:mm")))
    {
        trailers.Add(new trailer__time
        {
            Trailer_Number = i,
            Delivery_Time = calculated_start_time.ToString("HH:mm")
        });
    }
}

dataGridView1.DataSource = trailers;

If dataGrideView1 only listed the potential delivery times and not the trailer_number as well, it would be even simpler by using the Except method...
C#
var timesToAvoid = (List&lt;string&gt;)dataGridView2.DataSource;
var altMethod = new List<string>();
  for (var i = 0; i < trailer_count; i++)
  {
      calculated_start_time = calculated_start_time.AddMinutes(i == 0 ? 0 : minutes_apart);
      altMethod.Add(calculated_start_time.ToString("HH:mm"));
  }
  dataGridView1.DataSource = altMethod.Except(timesToAvoid);
 
Share this answer
 
Comments
Member 12349103 4-Dec-16 16:45pm    
Where should i add that code in mine btw Brilliant answer
confused with this line error at lt. & and gt:)
(
var timesToAvoid = (List & lt; string>)dataGridView2.DataSource;)
CHill60 5-Dec-16 10:58am    
The code that I've posted (the first bit) would replace your code ...
var trailers = new List<trailer__time>();
for (var i = 0; i < trailer_count; i++)
{
if (i == 0)
{
calculated_start_time = calculated_start_time.AddMinutes(0);
}
else
{
calculated_start_time = calculated_start_time.AddMinutes(minutes_apart);
}

trailers.Add(new Trailer__Time
{
Trailer_Number = i,
Delivery_Time = calculated_start_time.ToString("HH:mm")
});
}

dataGridView1 .DataSource = trailers;


The line var timesToAvoid = (List<string>)dataGridView2.DataSource; is just getting the contents of dataGridView2 into a List of strings. The (List<string>) is casting the DataSource to a list of strings - better explanation can be found here ... Casting and Type Conversions (C# Programming Guide)[^]
Member 12349103 5-Dec-16 17:11pm    
CHill60
Again excellent
got it to run with an error its on (var timesToAvoid = (List<string>)dataGridView2.DataSource;)it is just a simple CSV class file no time format just text. I think thats the issue

error (unhandled exception 'system.invalidCastexception occured in Trailer time. Unable to cast object of type 'system.window.forms.bindingsource' to type

system.collection.genneric.list' system.string
CHill60 5-Dec-16 18:34pm    
No ... you should be able to cast the DataSource of dataGridView2 to a list of strings IF (and only if) it is as you described ... a list of times (which will be in string format). Alternatively show me how you populate DataGridView2
Member 12349103 5-Dec-16 19:02pm    
Not to string here's the code it reads and writes

private void btnWrite_Click(object sender, EventArgs e)
{
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "csv|*.csv", ValidateNames = true })
{
if (sfd.ShowDialog() == DialogResult.OK)
{
using (var sw = new StreamWriter(sfd.FileName))
{
var writer = new CsvWriter(sw);
writer.WriteHeader(typeof(Lunch));
foreach (Lunch s in lunchBindingSource.DataSource as List<lunch>)
{
writer.WriteRecord(s);
}
}
MessageBox.Show("Your Data has been Successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}



private void btnRead_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
var sr = new StreamReader(new FileStream(ofd.FileName, FileMode.Open));
var csv = new CsvReader(sr);
// lunchBindingSource.DataSource = csv.GetRecords<lunch>().ToString();
lunchBindingSource.DataSource = csv.GetRecords<lunch>(); /// use to create file
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
lunchBindingSource.DataSource = new List<lunch>();
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900