venp

Hi,

how to specify a column as dateedit like combo,textbox

thanks

venp--




Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

Bob zhu - MSFT

Hi here is an datagridview calendar column sample;

hpoe fit your requiirement:)

http://dzaebel.net/CalendarColumn.htm

Best Regards!






Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

Zhi-Xin Ye - MSFT

An alternative method without developing your own DataGridView column:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsApplication1

{

public partial class Form27 : Form

{

public Form27()

{

InitializeComponent();

}

private void Form27_Load(object sender, EventArgs e)

{

this.dataGridView1.AutoGenerateColumns = false;

DataGridViewTextBoxColumn clmID = new DataGridViewTextBoxColumn();

clmID.HeaderText = "OrderID";

clmID.DataPropertyName = "OrderID";

this.dataGridView1.Columns.Add(clmID);

DataGridViewTextBoxColumn clmAddr = new DataGridViewTextBoxColumn();

clmAddr.HeaderText = "ShipAddress";

clmAddr.DataPropertyName = "ShipAddress";

this.dataGridView1.Columns.Add(clmAddr);

DataGridViewTextBoxColumn clmDate = new DataGridViewTextBoxColumn();

clmDate.HeaderText = "ShippedDate";

clmDate.DataPropertyName = "ShippedDate";

this.dataGridView1.Columns.Add(clmDate);

//picker is a DateTimePicker control.

this.picker.Visible = false;

this.dataGridView1.Controls.Add(picker);

this.dataGridView1.DataSource = getData().Tables[0];

}

private void picker_ValueChanged(object sender, EventArgs e)

{

this.dataGridView1.CurrentCell.Value = picker.Text;

}

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)

{

if (this.dataGridView1.Focused && this.dataGridView1.CurrentCell.ColumnIndex == 2)

{

picker.Location = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,false).Location;

picker.Visible = true;

picker.Value = (DateTime)this.dataGridView1.CurrentCell.Value;

}

else

{

picker.Visible = false;

}

}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

if (this.dataGridView1.Focused && this.dataGridView1.CurrentCell.ColumnIndex == 2)

{

this.dataGridView1.CurrentCell.Value = picker.Text;

}

}

public DataSet getData()

{

string connstr = "Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=Northwind;server=localhost";

SqlConnection conn = new SqlConnection(connstr);

string sql = "SELECT OrderID,ShipAddress,ShippedDate from Orders";

SqlDataAdapter da = new SqlDataAdapter(sql, conn);

DataSet ds = new DataSet();

da.Fill(ds, "Orders");

return ds;

}

}

}

Hope it helps.Best Regards.

Ye






Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

venp

HI Zhi-Xin Ye,

my application crashes after selecting a date from datetime ctrl i'm getting dataerror says cannot convert string to datetime something. raising system.format exception .

I like the above piece of code. it's really cool and simple.

Please help me with this.

thanks

venp--






Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

venp

HI,

its working now. but still sometimes i get format error. why.

venp--






Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

venp

HI,

will the below statement help me in future to get rid off the format exception.

private void dgBilling_DataError(object sender, DataGridViewDataErrorEventArgs e)

{

//exit out

return;

}

venp---






Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

Zhi-Xin Ye - MSFT

Hi,venp

Make sure: the column which uses DateTimePicker control is bound to a database field of datetime type or other type alike.

And how about post some code to let me help you find out the problem

Best Regards.

Ye






Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

venp

HI Zhi-Xin Ye - MSFT,

  here is my code.I'm not able to replicate the error again.

 

particualr col is bind to date field like the below.

dgcol = new DataGridViewTextBoxColumn();

dgcol.DataPropertyName = "BillingDate";  --datetime datatype

dgcol.Name = "BillingDate";

dgcol.HeaderText = "Billing Date";

dgcol.Visible = true;

dgcol.Width = 100;

dgcol.ReadOnly = true;

dg.Columns.Add(dgcol);

 

private void dgrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

 if (this.dgrid.Focused && this.dgrid.CurrentCell.ColumnIndex == 2)

 {

 this.dgrid.CurrentCell.Value = picker.Text;

 }

 else

 picker.Visible = false;

}

private void dgrid_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)

{

 if (this.dgrid.Focused && this.dgrid.CurrentCell.ColumnIndex == 2)

 {

 picker.Location = this.dgrid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;

 picker.Visible = true;

 picker.Value = Convert.ToDateTime(this.dgrid.CurrentCell.Value);

 }

 else

 {

 picker.Visible = false;

 }

}

private void picker_ValueChanged(object sender, EventArgs e)

{

 this.dgrid.CurrentCell.Value = picker.Text;

}

 

private void drid_DataError(object sender, DataGridViewDataErrorEventArgs e)

{

return;

}

thanks

venp--






Re: Windows Forms Data Controls and Databinding datagridview with dateedit column

Zhi-Xin Ye - MSFT

Hi,venp

Change codes in the CellBeginEdit event to:

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)

        {

            if (this.dataGridView1.Focused && this.dataGridView1.CurrentCell.ColumnIndex == 1) // Make sure the columnIndex is the index of the column which you want to display a datetimepicker,in my example it's 1,in your code may not.

            {

                picker.Location = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,false).Location;

                picker.Visible = true;

                if (this.dataGridView1.CurrentCell.Value != DBNull.Value)

//Because the date stored in the database maybe null if you set this field to allow null value, so here need a judgement.

                    picker.Value = (DateTime)this.dataGridView1.CurrentCell.Value;

                else

                    picker.Value = DateTime.Now;//Set today as default date;

            }

            else

            {

                picker.Visible = false;

            }

        }

Put atttention to the statement in bold and the comments.

Best Regars.Ye