Hello,
I wrote simple Windows form application,
which consists of one Form containing DataGridView1 control with two columns,
when you enter date into cell in the first column and press enter the
value of the cell in the seckond column of the same row becomes the
day of the week based on the entered date,
and the focus moves to cell in the first column of the next row.
I have used the:
DataGridViewCellEndEdit
event that occurs when edit mode for the current cell ends
afther you press enter, or click on other cell in DataGridView control.
Code for Program.cs
using System;
using System.Windows.Forms;
namespace Date_and_days
{
internal sealed class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
Code for MainForm.cs :
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Date_and_days
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
void DataGridView1CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DateTime Date;
int row = 0;
int column = 0;
bool IsDate = false;
row = dataGridView1.CurrentCell.RowIndex;
column = dataGridView1.CurrentCell.ColumnIndex;
if (column == 0)
{
IsDate = DateTime.TryParse(dataGridView1.CurrentCell.Value.ToString(),out Date);
if (IsDate)
{
dataGridView1[column+1,row].Value = Date.DayOfWeek.ToString();
}
else
{
dataGridView1[column+1,row].Value = String.Empty;
}
}
}
}
}
Code for MainForm.Designer.cs
namespace Date_and_days
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2});
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.dataGridView1.Size = new System.Drawing.Size(540, 230);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataGridView1CellEndEdit);
dataGridViewCellStyle1.Format = "D";
dataGridViewCellStyle1.NullValue = null;
this.Column1.DefaultCellStyle = dataGridViewCellStyle1;
this.Column1.HeaderText = "Date";
this.Column1.MaxInputLength = 100;
this.Column1.Name = "Column1";
this.Column1.Width = 200;
this.Column2.HeaderText = "Day of the week";
this.Column2.Name = "Column2";
this.Column2.Width = 300;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(562, 262);
this.Controls.Add(this.dataGridView1);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Date and days";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
public System.Windows.Forms.DataGridView dataGridView1;
}
}
All the best,
Perić Željko