hi there,

how can i add a calender column by writing the code in C#,

the below code gives and error:

CalendarColumn c = new (CalendarColumn)dgvAction.Rows[0].Cells[1].OwningColumn;

what i want is when i click a check box in the datagrid view i want the current date to display in the calender column if not i want to make the calender column null, no value(not even the current day)

how can i do this??


thanx

Recommended Answers

All 4 Replies

Try this:

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

namespace daniweb.dgv
{
  public partial class frmCalendar : Form
  {
    private DataTable dt;

    public frmCalendar()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Gets simulatation data
    /// </summary>
    /// <returns></returns>
    private static DataTable GetTestData()
    {
      DataTable result = new DataTable();
      result.Columns.Add(new DataColumn("RecordId", typeof(int)));
      result.Columns.Add(new DataColumn("Name", typeof(string)));
      result.Columns.Add(new DataColumn("Date", typeof(DateTime)));
      result.Columns.Add(new DataColumn("CheckBox", typeof(bool)));
      for (int i1 = 1000; i1 <= 1008; i1++)
      {
        DataRow row = result.NewRow();
        row["RecordId"] = i1;
        row["Name"] = Guid.NewGuid().ToString();
        result.Rows.Add(row);
      }
      return result;
    }

    private void frmGridView5_Load(object sender, EventArgs e)
    {
      dt = GetTestData();
      dataGridView1.DataSource = dt;
      dt.AcceptChanges();
    }

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
      if (dataGridView1.CurrentCell == null)
        return;
      if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["CheckBox"].Index)
      {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        bool bChecked = (dataGridView1.CurrentCell.Value == DBNull.Value ? false : (bool)dataGridView1.CurrentCell.Value);
        dataGridView1.CurrentRow.Cells["Date"].Value = (bChecked ? (object)DateTime.Today : DBNull.Value);
      }
    }
  }
}

Also see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick(VS.80).aspx

For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView.CellValueChanged event instead. Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the DataGridView.CurrentCellDirtyStateChanged event. In that handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

commented: WB to C# +4

Try this:

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

namespace daniweb.dgv
{
  public partial class frmCalendar : Form
  {
    private DataTable dt;

    public frmCalendar()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Gets simulatation data
    /// </summary>
    /// <returns></returns>
    private static DataTable GetTestData()
    {
      DataTable result = new DataTable();
      result.Columns.Add(new DataColumn("RecordId", typeof(int)));
      result.Columns.Add(new DataColumn("Name", typeof(string)));
      result.Columns.Add(new DataColumn("Date", typeof(DateTime)));
      result.Columns.Add(new DataColumn("CheckBox", typeof(bool)));
      for (int i1 = 1000; i1 <= 1008; i1++)
      {
        DataRow row = result.NewRow();
        row["RecordId"] = i1;
        row["Name"] = Guid.NewGuid().ToString();
        result.Rows.Add(row);
      }
      return result;
    }

    private void frmGridView5_Load(object sender, EventArgs e)
    {
      dt = GetTestData();
      dataGridView1.DataSource = dt;
      dt.AcceptChanges();
    }

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
      if (dataGridView1.CurrentCell == null)
        return;
      if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["CheckBox"].Index)
      {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        bool bChecked = (dataGridView1.CurrentCell.Value == DBNull.Value ? false : (bool)dataGridView1.CurrentCell.Value);
        dataGridView1.CurrentRow.Cells["Date"].Value = (bChecked ? (object)DateTime.Today : DBNull.Value);
      }
    }
  }
}

Also see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick(VS.80).aspx

hey 
can u explain the line number s from 55 to 57

thanx

Try this:

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

namespace daniweb.dgv
{
  public partial class frmCalendar : Form
  {
    private DataTable dt;

    public frmCalendar()
    {
      InitializeComponent();
    }

    /// <summary>
    /// Gets simulatation data
    /// </summary>
    /// <returns></returns>
    private static DataTable GetTestData()
    {
      DataTable result = new DataTable();
      result.Columns.Add(new DataColumn("RecordId", typeof(int)));
      result.Columns.Add(new DataColumn("Name", typeof(string)));
      result.Columns.Add(new DataColumn("Date", typeof(DateTime)));
      result.Columns.Add(new DataColumn("CheckBox", typeof(bool)));
      for (int i1 = 1000; i1 <= 1008; i1++)
      {
        DataRow row = result.NewRow();
        row["RecordId"] = i1;
        row["Name"] = Guid.NewGuid().ToString();
        result.Rows.Add(row);
      }
      return result;
    }

    private void frmGridView5_Load(object sender, EventArgs e)
    {
      dt = GetTestData();
      dataGridView1.DataSource = dt;
      dt.AcceptChanges();
    }

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
      if (dataGridView1.CurrentCell == null)
        return;
      if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["CheckBox"].Index)
      {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        bool bChecked = (dataGridView1.CurrentCell.Value == DBNull.Value ? false : (bool)dataGridView1.CurrentCell.Value);
        dataGridView1.CurrentRow.Cells["Date"].Value = (bChecked ? (object)DateTime.Today : DBNull.Value);
      }
    }
  }
}

Also see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick(VS.80).aspx

there is an " Object reference not set to an instance of an object." error in the line

if (dgvAction.CurrentCell.ColumnIndex == dgvAction.Columns["CheckBox"].Index)

why is that???????

>>why is that???????
Your underlying data source has different field names than the one I used as an example.

I have rewritten lines 55-57 without using ternary operators so hopefully this makes a little more sense:

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
      if (dataGridView1.CurrentCell == null)
        return;
      if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.Columns["CheckBox"].Index)
      {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        if ((dataGridView1.CurrentCell.Value == DBNull.Value) || ((bool)dataGridView1.CurrentCell.Value == false))
        {
          dataGridView1.CurrentRow.Cells["Date"].Value = DBNull.Value;
        }
        else
        {
          dataGridView1.CurrentRow.Cells["Date"].Value = DateTime.Today;
        }
        //bool bChecked = (dataGridView1.CurrentCell.Value == DBNull.Value ? false : (bool)dataGridView1.CurrentCell.Value);
        //dataGridView1.CurrentRow.Cells["Date"].Value = (bChecked ? (object)DateTime.Today : DBNull.Value);
      }
    }

I have also attached a sample solution.

Please mark this thread if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.