Hi all,

What I want to do is, when I leave a cell I want to make sure that that cell is not null, that is there is a date data there, and when I leave, I want to add some days to date and place this result on another cell. This is how I tried it.

private void tblgrid1_CellValidating(object sender, DataGridViewCellValidatingEventArgs  e)
        {

            
                DataGridViewRow dgvRow = tblgrid1.CurrentRow;
//here I can't get this code to function even if I'm on right cell....problem here...
               if (dgvRow.Cells["colStartDate"].Selected)
                {

// here I actually want to test if null, appreciate a better code if you have
                    if (Convert.ToDateTime(dgvRow.Cells["colStartDate"].Value) > Convert.ToDateTime("01/01/1900"))
                    {
                       if (Convert.ToInt32(dgvRow.Cells["colDayPast"].Value) > 0 )
                       {
                           dgvRow.Cells["colEndDate"].Value = (Convert.ToDateTime(dgvRow.Cells["colStartDate"].Value)).AddDays(Convert.ToInt32(dgvRow.Cells["colDayPast"].Value)).ToShortDateString();
                       }
                    }
                }

           

        }

I appreciate any help
thanks
snky

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
{
  public partial class frmGridView4 : Form
  {
    private DataTable dt;

    public frmGridView4()
    {
      InitializeComponent();
    }

    private void frmGridView4_Load(object sender, EventArgs e)
    {
      dt = new DataTable();
      //The string columns are unused
      dt.Columns.Add(new DataColumn("string1", typeof(string)));
      dt.Columns.Add(new DataColumn("string2", typeof(string)));
      dt.Columns.Add(new DataColumn("string3", typeof(string)));
      dt.Columns.Add(new DataColumn("string4", typeof(string)));
      dt.Columns.Add(new DataColumn("Date1", typeof(DateTime)));
      dt.Columns.Add(new DataColumn("Date2", typeof(DateTime)));
      //Date1+Date2 = Date3
      dt.Columns.Add(new DataColumn("Date3", typeof(DateTime)));

      //Get some test rows
      for (int i1 = 0; i1 < 5; i1++)
      {
        DataRow row = dt.NewRow();
        row["string1"] = Guid.NewGuid().ToString();
        row["string2"] = Guid.NewGuid().ToString();
        row["string3"] = Guid.NewGuid().ToString();
        row["string4"] = Guid.NewGuid().ToString();
        dt.Rows.Add(row);
      }


      dataGridView1.AutoGenerateColumns = true;
      dataGridView1.DataSource = dt;

      dataGridView1.Columns[0].DataPropertyName = "string1";
      dataGridView1.Columns[0].Name = "string1";
      dataGridView1.Columns[1].DataPropertyName = "string2";
      dataGridView1.Columns[1].Name = "string2";
      dataGridView1.Columns[2].DataPropertyName = "string3";
      dataGridView1.Columns[2].Name = "string3";
      dataGridView1.Columns[3].DataPropertyName = "string4";
      dataGridView1.Columns[3].Name = "string4";
      dataGridView1.Columns[4].DataPropertyName = "Date1";
      dataGridView1.Columns[4].Name = "Date1";
      dataGridView1.Columns[5].DataPropertyName = "Date2";
      dataGridView1.Columns[5].Name = "Date2";
      dataGridView1.Columns[6].DataPropertyName = "Date3";
      dataGridView1.Columns[6].Name = "Date3";
    }

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
      if ((e.ColumnIndex == dataGridView1.Columns["Date1"].Index) ||
          (e.ColumnIndex == dataGridView1.Columns["Date2"].Index))
      {
        DataGridViewRow dr = dataGridView1.Rows[e.RowIndex];
        DateTime dt1, dt2;
        if (DateTime.TryParse(Convert.ToString(dr.Cells["Date1"].Value), out dt1) && (DateTime.TryParse(Convert.ToString(dr.Cells["Date2"].Value), out dt2)))
        {
          //This is just so we dont overflow the dates. This is a nonsensical way to add dates :)
          DateTime dtResult = dt1.AddDays(dt2.Day);
          dataGridView1.Rows[e.RowIndex].Cells["Date3"].Value = dtResult;
        }
      }

    }
  }
}

Thank you Scott,

It is a great effort you are putting in.

I still have problem though,

on < if ((e.ColumnIndex == dataGridView1.Columns["Date1"].Index....> line , when it comes here it does not sense cell_validating event and skips to the end. I adopted your code to mine, I sticked to your grid_cellvalidating event as is.
Could it be some grid config values issue ?

thanks
snakay

You have to click on the grid and wire up the event. Click on the grid, look in your properties window, flip over to the events tab, select the CellValidatingEvent, hit the dropdown, and select the method

O.K. Scott,

that wasn't the problem, I had already done it. It was my mistake from the very beginning, that I mixed up the column names, with wrong column names it would not pass the logical test, therefore nothing to do with event handling. Thanks a lot You have been great help

snakay

You're welcome

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

ahaa,

still have a curious problem. When I leave the second cell to invoke cellvalidating and get the result to the third column, it doesn't. When I erase the content of second cell and re enter the date and leave the cell, it does work this time and I get the result to the third column

snakay

when I do that I get "System.NullReferenceException was unhandled" exception on

if ((e.ColumnIndex == dataGridView1.Columns["Date1"].Index) ||          (e.ColumnIndex == dataGridView1.Columns["Date2"].Index)) 

line.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You probably don't have a column named "Date1" or "Date2". In the sample I posted I had named the columns:

dataGridView1.Columns[4].DataPropertyName = "Date1";
      dataGridView1.Columns[4].Name = "Date1";
      dataGridView1.Columns[5].DataPropertyName = "Date2";
      dataGridView1.Columns[5].Name = "Date2";
      dataGridView1.Columns[6].DataPropertyName = "Date3";
      dataGridView1.Columns[6].Name = "Date3";

Hi Scott,

I actually use my own column names, I included yours in the message just not to cause any confusion. This line worked well with the previous "CellValidating" event without any problem. My actual code for this line is;

if ((e.ColumnIndex == tblIzinDokum.Columns["colIzinbasla"].Index) || (e.ColumnIndex == tblIzinDokum.Columns["colGunk"].Index))

so I have exception on this line

One of those columns isn't defined then. Put test code in there:

if (tblIzinDokum.Columns["colIzinbasla"] == null)
  System.Diagnostics.Debugger.Break();
else if (tblIzinDokum.Columns["colGunk"] == null)
  System.Diagnostics.Debugger.Break();

See if you hit a breakpoint.

doesn't help I still get "NullReferenceException was unhandled . Check to determine if the object is Null before calling method" error

I think, when grid was populated these columns are null, nothing comes from database, so CellValueChanged is wired at the time of datagridview populating.

I must add, break is caught in the first "if"

if (tblIzinDokum.Columns["colIzinbasla"] == null)  System.Diagnostics.Debugger.Break();

I think objection to null value of colIzinbasla...It is defined it works for the whole application, but initially when populated it is a null cell

No... that means you have a mis-spelling in your column name. That should return the column of the grid regardless of the data. Please double check your names

column names are no problem. I switched to the previos sample with cellvalidating, it works with the defect I mentioned. So the same column names.

it is at the bottom of the project

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

namespace Insan_Kaynaklari
{
    public partial class frmPersizin : Form
    {
       
        private OleDbCommand cmd;
        private OleDbDataAdapter da;
        private DataSet ds;
        private DataTable tb;
        private OleDbConnection cn;
        string sSqlCommand;
        private string strWhere;
        
        private string strConn = @"Provider=IBMDADB2;Database=pen2009;PROTOCOL=TCPIP;HOSTNAME=boss;PORT=50000;uid=db2admin;pwd=db2admin";
        
        public frmPersizin()
        {
            InitializeComponent();
            rbGiris.Checked = true;
            rbTek.Checked = true;
            
           
        }

        private void PopulateTable()
        {
            bool IsConnecting = true;

            while (IsConnecting)
            {
                try
                {
                    // Kum saatini balat
                    Cursor.Current = Cursors.WaitCursor; 
                    // Connect to the Database
                    // Instantiate the connection
                    cn = null;
                    cn = new OleDbConnection(strConn);
                    cn.Open();
                   // da = new OleDbDataAdapter(cmd);
                   // ds = new DataSet();

                    if (rbGiris.Checked )
                    {
                        sSqlCommand ="select a.sicil_no ,a.adi||\' \'||a.soyadi as isim,b.unvan_aciklama,a.ilkgir_tarihi,a.ayrilma_tarihi,a.sua,a.dogum_tarihi  "
		                    + "from per015_ssk a,per003_ssk b";

                        if (rbTek.Checked & dfSicilNo.Text != "")
                        {                          
                            if (cbgenel.Checked)
                            {
                                strWhere = " where  a.sicil_no='" + dfSicilNo.Text + "' and a.unvan_kodu=b.unvan_kodu and date(a.ayrilma_tarihi) is Null and a.sicil_no not  in (select sicil_no from per010_ssk  )";
                                sSqlCommand = sSqlCommand + strWhere;
                            }
                            else
                            {
                                strWhere = " where  a.sicil_no='" + dfSicilNo.Text + "' and a.unvan_kodu=b.unvan_kodu and date(a.ayrilma_tarihi) is Null and a.sicil_no not  in  "
                               + "  (select sicil_no from per010_ssk where donem='" + dfHesYil.Text + "' 	and izin_tipi=\'Yllk zin\' and hareket_tipi=\'Hakedi\' ) ";
                                sSqlCommand = sSqlCommand + strWhere;
                            }
                        }
                        else if (rbBolum.Checked & dfBolumKodu.Text!="")
                        {
                           

                            if (cbgenel.Checked)
                            {
                                strWhere = " where a.birim_kodu='" + dfBolumKodu.Text + "' and a.unvan_kodu=b.unvan_kodu and date(a.ayrilma_tarihi) is Null  and a.sicil_no not  in (select sicil_no from per010_ssk  )";
                                sSqlCommand = sSqlCommand + strWhere;
                            }
                            else
                            {
                                strWhere = " where a.birim_kodu='" + dfBolumKodu.Text + "' and a.unvan_kodu=b.unvan_kodu and date(a.ayrilma_tarihi) is Null and a.sicil_no not  in  "
                               + " (select sicil_no from per010_ssk where donem='" + dfHesYil.Text + "' and izin_tipi=\'Yllk zin\' and hareket_tipi=\'Hakedi\')  ";
                                sSqlCommand = sSqlCommand + strWhere;
                            }
                        }
                        else if (rbTum.Checked)
                        {
                            
                            if (cbgenel.Checked)
                            {
                                strWhere = " where  a.unvan_kodu=b.unvan_kodu and date(a.ayrilma_tarihi) is Null and a.sicil_no not  in (select sicil_no from per010_ssk )";
                                sSqlCommand = sSqlCommand + strWhere;
                            }
                            else
                            {
                                strWhere = " where a.sicil_no not  in (select sicil_no from per010_ssk where donem='" + dfHesYil.Text + "'  and izin_tipi=\'Yllk zin\' "
                                + "and hareket_tipi=\'Hakedi\') and a.unvan_kodu=b.unvan_kodu and date(a.ayrilma_tarihi) is Null";

                                sSqlCommand = sSqlCommand + strWhere;

                            }
                        }
                        cmd = new OleDbCommand(sSqlCommand, cn);
                    }
                    else if (rbListe.Checked)
                    {
                        sSqlCommand = "select a.sicil_no ,a.adi||\' \'||a.soyadi as isim,b.izin_tipi,b.hareket_tipi,b.izin_sure,b.kullanim_sure,b.baslama_tarih, "
                            + "b.bitis_tarih,b.hakedis_tarih,a.ilkgir_tarihi,a.ayrilma_tarihi,c.unvan_aciklama,b.sua,a.sua as suahak,a.dogum_tarihi "
                            + "from per015_ssk a,per010_ssk b,per003_ssk c";

                        if (rbTek.Checked & dfSicilNo.Text != "")
                        {


                            if (cbgenel.Checked)
                            {
                                strWhere = " where  a.sicil_no='" + dfSicilNo.Text + "' and a.sicil_no=b.sicil_no and a.unvan_kodu=c.unvan_kodu "
                                + " order by b.donem,b.hareket_tipi,b.baslama_tarih ,b.izin_tipi DESC";

                                sSqlCommand = sSqlCommand + strWhere;
                            }
                            else 
                            {
                                strWhere = "  where  a.sicil_no='" + dfSicilNo.Text + "' and a.sicil_no=b.sicil_no and a.unvan_kodu=c.unvan_kodu and b.donem='" + dfHesYil.Text + "' and "
                                + " date(a.ayrilma_tarihi) is Null 	order by b.hareket_tipi,b.baslama_tarih,b.izin_tipi DESC";

                                sSqlCommand = sSqlCommand + strWhere;
                            }
                        }
                        else if (rbBolum.Checked & dfBolumKodu.Text != "")
                        {
                            

                            if (cbgenel.Checked)
                            {
                                strWhere = " where a.birim_kodu='" + dfBolumKodu.Text + "' and a.sicil_no=b.sicil_no and a.unvan_kodu=c.unvan_kodu  "
                              + " order by b.sicil_no,b.donem,b.hareket_tipi,b.baslama_tarih,b.izin_tipi DESC";
                                sSqlCommand = sSqlCommand + strWhere;
                            }
                            else 
                            {
                                strWhere = " where a.birim_kodu='" + dfBolumKodu.Text + "' and a.sicil_no=b.sicil_no and a.unvan_kodu=c.unvan_kodu and b.donem='" + dfHesYil.Text + "'  and date(a.ayrilma_tarihi) is Null "
                                 + " order by b.sicil_no,b.hareket_tipi,b.baslama_tarih,b.izin_tipi DESC";

                                sSqlCommand = sSqlCommand + strWhere;
                            }
                        }
                        else if (rbTum.Checked)
                        {
                            

                            if (cbgenel.Checked)
                            {
                                strWhere = " where a.sicil_no=b.sicil_no  and a.unvan_kodu=c.unvan_kodu  "
                                 + " order by b.sicil_no,b.donem,b.hareket_tipi,b.izin_tipi DESC,b.baslama_tarih";
                                sSqlCommand = sSqlCommand + strWhere;
                            }
                            else
                            {
                                strWhere = " where a.sicil_no=b.sicil_no and b.donem='" + dfHesYil.Text + "' and a.unvan_kodu=c.unvan_kodu and date(a.ayrilma_tarihi) is Null "
                                                            + " order by b.sicil_no,b.hareket_tipi,b.izin_tipi DESC,b.baslama_tarih ";

                                sSqlCommand = sSqlCommand + strWhere;
                            }
                        }



                        cmd = new OleDbCommand(sSqlCommand, cn);
                    }
                  
                    da = new OleDbDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "Izin");
                    tb = ds.Tables[0];
                    cn.Close();
                   
                    tb.DefaultView.Sort = "SICIL_NO";
                    // binding tblIzinDokum gridview binding
                    bindingSource1.DataSource = ds.Tables[0];
                    tblIzinDokum.DataSource = bindingSource1;
                  // tblIzinDokum.DataSource = ds.Tables[0];
                   //Kolonlarn geniliini o stunda yer alan en uzun deere gre ayarlar
                   tblIzinDokum.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                   //cell geniliini o stunda yer alan en uzun deere gre ayarlar
                   tblIzinDokum.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
                    // lk satrn Selected Property nin false edilmesi ile ilk satr seili gibi grnmez
                   tblIzinDokum.Rows[0].Selected = false;
                   // The AutoGenerateColumns property is set to false, since the code programmatically 
                    //populates the columns collection
                   tblIzinDokum.AutoGenerateColumns = false;
                   tblIzinDokum.Columns["colDogumTarihi"].Visible = false;
                   tblIzinDokum.Columns["colAyrilma"].Visible = false;
                   tblIzinD

Half of the set up for that class is in the designer file which is why I asked you to upload the project, not a file.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
      foreach (DataGridViewColumn dc in dataGridView1.Columns)
        Console.WriteLine(dc.Name);

See if the column you're after shows up in the output

I notice you're using calls to .Cells and .Columns but I can't tell what class the variables are without the whole project.

The code is running before the form initializes.

First:

public partial class frmPersizin : Form
    {
      private bool loaded = default(bool); //add this

Next:

private void frmPersizin_Load(object sender, EventArgs e)
        {
            dfHesYil.Text = Convert.ToString(DateTime.Now.Year);

            int Sene = Convert.ToInt32(dfHesYil.Text);
            DateTime d = new DateTime(Sene, 12, 31);
            dfDegerTarih.Text = Convert.ToDateTime(d).ToString("dd/MM/yyyy");

            loaded = true; //add this
        }
private void tblIzinDokum_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
          if (!loaded) //add these two lines
            return;

when I check if columns show, I don't expect them to show at the grid population process. That is to do with program logic. So the columns;

if ((e.ColumnIndex == tblIzinDokum.Columns["colIzinbasla"].Index) || (e.ColumnIndex == tblIzinDokum.Columns["colGunk"].Index))

are not found in the loop. My expectation of the CellValueChanged is when I key enter something into the cells, not when grid is populated in the load.

Apparently CellValueChanged is wired when grid is populating and those columns are not addressed at that point we get exception.

Whereas I only want to trigger an event when I key in something into the cell and right after I leave the cell, first I want cell data be validated and secondly I generate some other values for other cells, which we partly succeeded with cellvalidaring with a funny behavior. This is my interpratation

Later I did the following addition;

if (rbGiris.Checked == true) // this I added now
             {
               if ((e.ColumnIndex == tblIzinDokum.Columns["colIzinbasla"].Index) || (e.ColumnIndex == tblIzinDokum.Columns["colGunk"].Index))

now it works, I think we have to watch out this CellValueChanged event and control it when wired.

If you agree, I will mark this thread as solved

mean time if you check my project if you want to make any other comment which is more then wellcome

Thanks for all your efforts
snakay

This question has already been answered. Start a new discussion instead.