PDA

View Full Version : TimeControlInDataGridView



samira3
شنبه 06 اسفند 1390, 14:16 عصر
سلام
من يك گريد دارم يك فيلد من زمان است كه من فقط ساعت ودقيقه اون ميخوام .كه كاربر با كليك بر روي هر كدام از فيلد زمان يك DateTimePickerگذاشتم كه ميتونه وارد كنه
حالا من اين زمان نمي خوام 12ساعت نباشه ميخوام 24 ساعت باشه
يك كلاس دارم كه بعنوان نوع به فيلد م وصل مي كنم وهر راه حلي را تست كردم شما بگيد مشكل كجا


using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text;
using System.Globalization;

namespace UserControls
{
public partial class GridTimeControl : DataGridViewColumn
{
public GridTimeControl(): base()
{
base.CellTemplate = new CalendarCell1();
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (!((value == null)) && !(value.GetType().IsAssignableFrom(typeof(Calendar Cell1))))
{
throw new InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}
}
}
public class CalendarCell1 : DataGridViewTextBoxCell
{

public CalendarCell1()
{
this.Style.Format = "HH:mm";
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{

// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

CalendarEditingControl1 ctl = (CalendarEditingControl1)DataGridView.EditingContr ol;
if (this.RowIndex >= 0)
{
if ((!object.ReferenceEquals(this.Value, DBNull.Value)))
{
if (this.Value != null)
{
if (this.Value != string.Empty)
{
try
{
string strDate = "14:00:00";
//var dtfi = new DateTimeFormatInfo { ShortDatePattern = "HH:mm", DateSeparator = "-" };

//DateTime objDate = Convert.ToDateTime(this.Value.ToString(), dtfi);
// string strDate = "21-07-2006";
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("fa-IR", true);
DateTime objDate = Convert.ToDateTime(strDate, theCultureInfo);

ctl.Value = objDate;

}
catch (Exception ex)
{
}
}
}
}
}
}

public override System.Type EditType
{
get
{
return typeof(CalendarEditingControl1);
}
}

public override System.Type ValueType
{
get
{
return typeof(DateTime);
}
}

public override object DefaultNewRowValue
{
get
{
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("fa-IR", true);
DateTime objDate = Convert.ToDateTime(DateTime.Now.ToShortTimeString( ), theCultureInfo);

return objDate;
}
}
}

class CalendarEditingControl1 : DateTimePicker, IDataGridViewEditingControl
{
private DataGridView dataGridViewControl;
private bool valueIsChanged = false;
private int rowIndexNum;

public CalendarEditingControl1()
{
this.Format = DateTimePickerFormat.Time;
}

public object EditingControlFormattedValue
{
get
{
return this.Value.TimeOfDay;
}
set
{ //----------Change By Ankur-----------------
if (value is string)
{
this.Value = DateTime.Parse(System.Convert.ToString(value));
}
}
}

public object GetEditingControlFormattedValue(DataGridViewDataEr rorContexts context)
{
this.CustomFormat = "HH:mm";
string ff = this.Value.TimeOfDay.ToString();
string g = ff.Substring(0, 5);
return g;
}

public void ApplyCellStyleToEditingControl(DataGridViewCellSty le dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ShowUpDown = true;
this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}

public int EditingControlRowIndex
{
get
{
return rowIndexNum;
}
set
{
rowIndexNum = value;
}
}

public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
if (Keys.KeyCode == Keys.Left || Keys.KeyCode == Keys.Up || Keys.KeyCode == Keys.Down || Keys.KeyCode == Keys.Right || Keys.KeyCode == Keys.Home || Keys.KeyCode == Keys.End || Keys.KeyCode == Keys.PageDown || Keys.KeyCode == Keys.PageUp)
{
return true;
}
else
{
return false;
}
}

public void PrepareEditingControlForEdit(bool selectAll)
{
}

public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}

public DataGridView EditingControlDataGridView
{
get
{
return dataGridViewControl;
}
set
{
dataGridViewControl = value;
}
}

public bool EditingControlValueChanged
{
get
{
return valueIsChanged;
}
set
{
valueIsChanged = value;
}
}

public Cursor EditingControlCursor
{
get
{
return base.Cursor;
}
}

protected override void OnValueChanged(System.EventArgs eventargs)
{
valueIsChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellD irty(true);
base.OnValueChanged(eventargs);
}


#region IDataGridViewEditingControl Members


Cursor IDataGridViewEditingControl.EditingPanelCursor
{
get { return base.Cursor; }//throw new Exception("The method or operation is not implemented."); }
}

#endregion
}
}

samira3
شنبه 06 اسفند 1390, 14:21 عصر
در ضمن وقتي من مقدار فيلد مي زنم وميرم روي فيلد ديگه مقدارش 24 ساعت م83283ي كنه

samira3
شنبه 06 اسفند 1390, 14:29 عصر
this.CustomFormat = "HH:mm";

sepehr.net
شنبه 12 فروردین 1391, 20:25 عصر
سلام

این لینک (http://www.csharp-examples.net/string-format-datetime/) رو مشاهده کنید