Tutorial :Displaying customised data in a datagrid view



Question:

I am trying to create an application where I want a data grid view to display the data depending on the date that the user selects in the combo box i.e.cmbDate according to my application. The cmbDate displays the available dates in the database. Below is my source code that I have written but on debugging the compiler gives an error of operator '==' cannot be used to compare 'System.DateTime' and 'object'. I would want that when the user clicks the load button it loads data for the date or month chosen.

namespace linqToSql_trial  {      public partial class frmSample : Form      {          private userLoginDataContext dc;            public frmSample()          {              InitializeComponent();              dc = new userLoginDataContext();          }            private void LoadDate()          {              cmbDate.DataSource = dc.flights.Select(x=>x.date);                cmbDate.DisplayMember = "date";              cmbDate.ValueMember = "date";          }            private void frmSample_Load(object sender, EventArgs e)          {              LoadDate();          }            private void btnLoad_Click(object sender, EventArgs e)          {               this.flightsDataGridView.DataSource = dc.flights.Where (x => x.date == cmbDate.SelectedItem);            }      }  }  


Solution:1

The error arises because you need to convert your selected item (text, type String) to type DateTime:

this.flightsDataGridView.DataSource =      dc.flights.Where (x => x.date == Convert.ToDateTime( cmbDate.SelectedItem));  

This may resolve your problem.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »