Home > Mobile >  datetimepicker in datagridview not allowing to enter same date twice
datetimepicker in datagridview not allowing to enter same date twice

Time:07-27

hello I am using c# and datagridview is already in place in my application where the columns are bind to another class what I have to do is embed a datetimepicker in my datagridview so that I can select date from it which is then bind to the variable and converted into the correct format before inserting into the database. I am only working on UI other developer has done the backend code. my code so far is in main class

DateTimePicker dtp = new DateTimePicker();

then on mouse click event

private void cliOrderStackGrid_CellClick_1(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            if (cliOrderStackGrid.Columns[e.ColumnIndex].Name == "orderDateDataGridViewTextBoxColumn" && e.RowIndex >= 0)
            {
                //DateTimePicker dtp = new DateTimePicker();
                cliOrderStackGrid.Controls.Add(dtp);
                dtp.Format = DateTimePickerFormat.Short;
                Rectangle rectangle = 
                cliOrderStackGrid.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex, true);
                dtp.Size = new Size(rectangle.Width, rectangle.Height);
                dtp.Location = new Point(rectangle.X, rectangle.Y);
                dtp.TextChanged  = new EventHandler(datetimepicker_textchanged);
                dtp.Visible = true;


            }
        }
        catch (Exception ex)
        {

            MessageBox.Show("Error in adding date in column"   ex.Message);
        }
      
       
    }

   private void datetimepicker_textchanged(object sender, EventArgs e)
    {
        cliOrderStackGrid.CurrentCell.Value = dtp.Text;
    }

My issue is if I am selecting the same date twice then the date does not add to the datagridview neither binds but if I am selecting new date everytime its working perfectly Sorry if the question is not correct to ask I am a new programmer and have limited knowledge. thanks in advance

CodePudding user response:

So finally after trying for so many days I get to the root cause of it.. I was not able to enter the same date one after the other but if the date was different I was able to easily enter it so basically if the text is not changed then the date was unable to be added That was because I was using text_changed event handler instead I have added

cliOrderStackGrid.CurrentCell.Value = dtp.Text;

to the cellClick event and the problem is solved!

  • Related