我使用以下代码在datagridview中设置文本框的值。代码完全符合我的要求。但是当我离开当前行时,CloseDate中的值会保留当前日期,但会丢弃时间并更改为" 00:00:00"。
基础SQL字段设置为datetime。底层数据源中的DataType是system.DateTime,DateTimeMode设置为UnspecifiedLocal。
我错过了什么?
tbl_TransactionsDataGridView.CurrentRow.Cells["CloseDate"].Value = DateTime.Now;
从我的datagridview的EditingControlShowing操作中,我正在查看正在更改的字段。在这种情况下,我想要6。
private void tbl_TransactionsDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null && tbl_TransactionsDataGridView.CurrentCell.ColumnIndex == 3)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}
if (combo != null && tbl_TransactionsDataGridView.CurrentCell.ColumnIndex == 6)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(Status_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(Status_SelectedIndexChanged);
}
}
当列索引为6时,会触发以下内容(我在使用此操作时遇到一些困难,但这是另一个线程的问题。)。当它触发时,它会正确触发,当我停留在当前行时,CloseDate具有我想要的值。但是,如果我转到新行并创建新记录或在datagridview中选择另一条记录,则CloseDate将从" 8/18/2014 8:42:32"到" 2014年8月18日00:00:00"。
private void Status_SelectedIndexChanged(object sender, EventArgs e)
{
object oStatus = new object();
oStatus = ((ComboBox)sender).SelectedValue;
if (Convert.IsDBNull(oStatus) && Convert.ToInt32(oStatus) == 1)
{
tbl_TransactionsDataGridView.CurrentRow.Cells["CheckOutEmployee"].Value = Environment.UserName;
tbl_TransactionsDataGridView.CurrentRow.Cells["CheckInEmployee"].Value = null;
tbl_TransactionsDataGridView.CurrentRow.Cells["CloseDate"].Value = null;
SendKeys.Send("{TAB}");
}
if (!Convert.IsDBNull(oStatus) && Convert.ToInt32(oStatus) > 1)
{
tbl_TransactionsDataGridView.CurrentRow.Cells["CheckInEmployee"].Value = Environment.UserName;
tbl_TransactionsDataGridView.CurrentRow.Cells["CloseDate"].Value = DateTime.Now;
SendKeys.Send("{TAB}");
}
}