Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a data grid view which contain two columns.
e.g.

date day
1/1/2005 Monday
7/1/2005 Monday
14/1/2005 Monday
21/1/2005 Monday
28/1/2005 Monday
5/2/2005 Monday
12/2/2005 Monday
19/2/2005 Monday
26/2/2005 Monday

i want to validate user in such way that he can change date(let 7/1/2005 )but not less or = than 1/1/2005 (upper cell) and not more than or =14/1/2005 (lowercell)
please friend if u have any idea please help me.
can anybody help me how to get upper and lower cell value of current cell ?

thanks in advanced
lakhan
Posted
Updated 27-Dec-11 19:18pm
v2
Comments
Sunasara Imdadhusen 28-Dec-11 1:22am    
Not clear!!

SQL
Hi,

You need to write a logic for validating Date like.

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DateTime StartDate = dataGridView1.Rows[0].Cells["Date"];
DateTime EndDate = dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells["Date"]
DateTime enetedDate = dataGridView1.Rows[e.RowIndex].Cells["Date"];
 
if( enetedDate >=  StartDate &&  enetedDate<= EndDate)
{
//Your logic should be here..
} 
else
{
 MessageBox.Show("Invalid Date!");
  e.Cancel = true;
}
}


Note:
Please use DataType conversion before use. because i haven't implemented it.
 
Share this answer
 
Comments
Sunasara Imdadhusen 28-Dec-11 8:23am    
Nice answer. please keep it up.
Savalia Manoj M 28-Dec-11 8:25am    
Thanks.
Yes. you can do it.
you need to take help of Gridview1_cellLeave event and write code under it.
e.g.
C#
gridview1_CellLeave()
{
 // collect all cell values in datetime object to compare
 DataTime dtUpperCell = e.gridview1.Rows[RowIndex - 1].Text;
 DataTime dtLowerCell = e.gridview1.Rows[RowIndex + 1].Text;
 DataTime dtCurrentCell = e.gridview1.Rows[RowIndex + 1].Text;

  if(dtUpperCell > dtCurrentCell && dtLowerCell < dtCurrentCell) //check condition
  {
     MessageBox.Show("Not allowed"); //show message
  }
}
 
Share this answer
 
Comments
[no name] 28-Dec-11 2:07am    
thanks koolprasad
Sunasara Imdadhusen 28-Dec-11 8:24am    
good one!
1st u have to fire gridviewrowdatabound event and inside that.
C#
Label date= (Label)e.Row.FindControl("Date");
 DateTime r =    Convert.ToDateTime(date.Text)            
DateTime r1=Convert.ToDateTime("1/1/2005");
DateTime r1=Convert.ToDateTime("14/1/2005");
                if (DateTime.Compare(r, r2) <= 0)
                {
                    can't change
                }
                else
                {
                    you can change
                }
               if (DateTime.Compare(r, r2) >= 0)
                {
                    can't change
                }
                else
                {
                    you can change
                }
 
Share this answer
 
v2
Comments
RaisKazi 28-Dec-11 4:47am    
Edited: Formatted using "pre" and "code" tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900