Custom DataGridView

Hi,
i am created custom datagridview when i am enter the data in to the cell it is disappear immediately. base paint method paint the cell so i cant able to enter the data plz help me




Answer this question

Custom DataGridView

  • Jensk

    Hi,
    we cant stop the paint method. so it is possible do like outlook2007 reminder u have any idea about develop control like that


  • adorer

    In my mind, paint wouldn't effect datasource of the DGV

    the repaint make your data disappear I think it because you haven't save it back to the datasource,

    so would you mind post some code can it would help our supporters to understand your problem clearly



  • DodgySwampy

    Hi I am just wandering did you ever get this working

    I am looking do achieve something similar myself but I am fairly new to C# and I am struggling somewhat... Have you posted your control on sourceforge or similar as i would like to try something like this in my app

    thanks

    Simon

  • TimDeveloper

    So, do you use databinding for the datagridview if so , you need to check whether the datasource table for this datagridview is changed after you enter data,

  • Imesh

    Hi,
    This is My code Using This Code I am Creating Custom DataGridView Calender

    using System;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;

    namespace Appointment
    {
    public enum DateMode
    {
    OneDay = 0,
    FiveDay = 1,
    SevenDay=2,
    }
    public partial class Calendar :DataGridView
    {
    private DateMode m_dateMode = DateMode.OneDay;
    private DateTime m_startTime = DateTime.Now;

    public Calendar()
    {
    InitializeComponent();
    this.AllowUserToAddRows = true;
    this.AllowUserToOrderColumns = false;
    this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    StartDisplay();
    }

    public void StartDisplay()
    {
    try
    {
    if (this.ColumnCount > 0)
    this.Columns.Clear();
    switch (DateMode)
    {
    case DateMode.OneDay:
    this.RowHeadersVisible = true;
    this.ColumnHeadersVisible = true;
    DateColumn dc = new DateColumn(this.StartTime);
    this.Columns.Add(dc);
    this.RowCount = 48;
    for (int i = 0; i < this.RowCount; i++)
    this.RowsIdea.HeaderCell = new RowHeaderCell();
    this.RowHeadersWidth = 50;
    this.ColumnHeadersHeight = 18;
    this.FirstDisplayedScrollingRowIndex = 16;
    this.MultiSelect = true;

    break;
    case DateMode.FiveDay:
    this.RowHeadersVisible = true;
    this.ColumnHeadersVisible = true;
    for (int i = 0; i < 5; i++)
    {
    DateColumn d = new DateColumn(this.StartTime.AddDays(i));
    this.Columns.Add(d);
    }
    this.RowCount = 48;
    this.FirstDisplayedScrollingRowIndex = 16;
    for (int i = 0; i < this.RowCount; i++)
    this.RowsIdea.HeaderCell = new RowHeaderCell();
    this.RowHeadersWidth = 50;
    this.ColumnHeadersHeight = 18;

    this.MultiSelect = true;
    break;
    case DateMode.SevenDay:
    this.RowHeadersVisible = true;
    this.ColumnHeadersVisible = true;

    for (int i = 0; i < 7; i++)
    {
    DateColumn d = new DateColumn(this.StartTime.AddDays(i));
    this.Columns.Add(d);
    }
    this.RowCount = 48;
    this.FirstDisplayedScrollingRowIndex = 16;
    for (int i = 0; i < this.RowCount; i++)
    this.RowsIdea.HeaderCell = new RowHeaderCell();
    this.RowHeadersWidth = 50;
    this.ColumnHeadersHeight = 18;
    this.FirstDisplayedScrollingRowIndex = 16;
    this.MultiSelect = true;
    break;

    default:
    break;
    }
    this.ClearSelection();
    }
    catch(Exception ex)
    {
    Trace.WriteLine(ex.ToString());
    }
    }



    public Calendar(IContainer container)
    {
    container.Add(this);


    }
    private DateTime selectionStart;
    public DateTime SelectionStart
    {
    get { return selectionStart; }
    set { selectionStart = value; }
    }

    private DateTime selectionEnd;

    public DateTime SelectionEnd
    {
    get { return selectionEnd; }
    set { selectionEnd = value; }
    }
    This method always running so i cant able to enter the data and load the data
    protected override void OnPaint(PaintEventArgs pe)
    {
    // Calling the base class OnPaint


    base.OnPaint(pe);

    }

    public DateMode DateMode
    {
    get
    {
    return m_dateMode;
    }
    set
    {
    m_dateMode = value;
    StartDisplay();
    }
    }
    public DateTime StartTime
    {
    get
    {
    DateTime d = new DateTime(m_startTime.Year, m_startTime.Month, m_startTime.Day);
    if (this.DateMode == DateMode.OneDay || d.DayOfWeek == DayOfWeek.Monday)
    return d;
    else
    {
    //Get the previous Monday(Want To Show Selected Day First Only Return d)
    while (d.DayOfWeek != DayOfWeek.Monday)
    d = d.Subtract(new TimeSpan(1, 0, 0, 0));
    return d;
    }
    }
    set
    {
    if (!m_startTime.Equals(value))
    {
    m_startTime = value;
    StartDisplay();
    }
    }
    }
    public DateTime EndTime
    {
    get
    {
    if (this.DateMode == DateMode.OneDay)
    return this.StartTime.AddDays(1);
    else if (this.DateMode == DateMode.FiveDay)
    return this.StartTime.AddDays(5);
    else
    return this.StartTime.AddDays(7);

    }
    }




    //*******************

    }
    }
    This code for Datagridview cell



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Drawing.Drawing2D;


    namespace Appointment
    {
    class Cell : DataGridViewTextBoxCell
    {
    private int m_rowIndex = 0;

    public Cell(): base()
    {
    ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
    ToolStripMenuItem item = new ToolStripMenuItem("New Appointment");
    contextMenuStrip.Items.Add(item);
    item.Click += new EventHandler(item_Click);
    }
    //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);
    // Calendar ctl = DataGridView.EditingControl as Calendar;
    // this.Value = (string)this.Value;
    // if (this.Value != null)
    // {
    // TextBox txtbox = new TextBox();
    // txtbox.Text = this.Value.ToString();


    // }
    //}

    void item_Click(object sender, EventArgs e)
    {
    try
    {

    }
    catch (Exception ex)
    {
    Trace.WriteLine(ex.ToString());
    }
    }

    public DateTime StartTime
    {
    get
    {
    try
    {
    // Each cell represent 30 minutes starting from 12 am
    DateColumn dc = this.OwningColumn as DateColumn;
    DateTime startTime = dc.StartTime;
    int rowIndex = (this.RowIndex < 0) m_rowIndex : this.RowIndex;
    int minutes = (rowIndex) * 30;
    return startTime.AddMinutes(minutes);
    }
    catch (Exception ex)
    {
    Trace.WriteLine(ex.ToString());
    return DateTime.MinValue;
    }
    }
    }

    public DateTime EndTime
    {
    get
    {
    return this.StartTime.AddMinutes(30);
    }
    }

    public bool IsWorkingTime
    {
    get
    {
    DateColumn dc = this.OwningColumn as DateColumn;
    if (dc.IsWeekend)
    return false;

    DateTime workingHourStartTime = dc.StartTime.AddHours(dc.WorkingHourStart);
    DateTime workingHourEndTime = dc.StartTime.AddHours(dc.WorkingHourEnd);

    if (this.StartTime.CompareTo(workingHourStartTime) >= 0 && this.EndTime.CompareTo(workingHourEndTime) <= 0)
    return true;
    else
    return false;
    }
    }

    public DateColumn DayColumn
    {
    get
    {
    return this.OwningColumn as DateColumn;
    }
    }
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
    try
    {
    m_rowIndex = rowIndex;

    Color workingColor = Color.LightYellow;
    Color nonWorkingColor = Color.FromArgb(250, 240, 180);

    Color halfHourEndingColor = Color.FromArgb(230, 210, 160);
    Color hourEndingColor = Color.FromArgb(210, 190, 50);

    Color leftMarginFillColor = Color.White;
    Color itemBackColor = Color.White;

    Color selectedBackColor = SystemColors.MenuHighlight;

    if (IsWorkingTime)
    graphics.FillRectangle(new SolidBrush(workingColor), cellBounds);
    else
    graphics.FillRectangle(new SolidBrush(nonWorkingColor), cellBounds);

    if (this.Selected)
    graphics.FillRectangle(new SolidBrush(selectedBackColor), cellBounds);

    int leftMargin = 4;


    // Draw the left margin vertical line and fill
    Pen pen = new Pen(new SolidBrush(Color.Black));
    graphics.DrawLine(pen, cellBounds.Left + leftMargin, cellBounds.Top, cellBounds.Left + leftMargin, cellBounds.Bottom);
    graphics.FillRectangle(new SolidBrush(leftMarginFillColor), cellBounds.Left, cellBounds.Top, leftMargin, cellBounds.Height);

    // Draw the bottom end of cell
    if (this.EndTime.Minute == 0)
    graphics.DrawLine(new Pen(new SolidBrush(hourEndingColor)), cellBounds.Left + leftMargin + 1, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);
    else
    graphics.DrawLine(new Pen(new SolidBrush(halfHourEndingColor)), cellBounds.Left + leftMargin + 1, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);

    // Draw the right end of the cell
    graphics.DrawLine(pen, cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom);
    }
    catch (Exception ex)
    {
    Trace.WriteLine(ex.ToString());
    }
    }

    }
    }



  • Thomas Cunningham

    Hi,
    I am created custom control using datagridview its like daycalendar .i want to enter some data in to my control. its like this
  • Cam70

    Hi , as we know, when we use DGV,

    we select cell (click or double click the cell)

    the enter form keyboard,

    but I am puzzled what do you means disappear are you mean your DGV always have only one row

    would you mind post some code here we can have a look



  • Custom DataGridView