Help with c# checkbox

Discussion in 'C#' started by markyjj, Aug 8, 2009.

  1. markyjj

    markyjj New Member

    Joined:
    Dec 7, 2005
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Can someone please help me with the following problem. I currently have a datagrid linked to an sql database and in the grid I have a ckeckbox column. However, when I check the box in the current cell the checked box disappears when I click on another cell and only reappears only when I go back to the cell. I was wandering how I get the checked box to stay visible even when I click on another cell.

    I would be grateful for any advice, many thanks.
     
  2. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    what code did you add on checkbox_click event?

    Also is this the windows application or web application?

    Did you try debugging and checked what's the cause?
     
  3. Seema786

    Seema786 New Member

    Joined:
    Jul 29, 2009
    Messages:
    66
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT Professional
    Location:
    Banglore
    yup, can u plz provide source code ?
     
  4. markyjj

    markyjj New Member

    Joined:
    Dec 7, 2005
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the reply, please find below the main part of the code that I am using to create and add the checkbox column to the datagrid which is also linked to sql express database. At the moment, when I check a box in a check a box, the cell displays back to null when I click on another cell. however, when I click back in the checked cell, the checkbox is shown as checked but the cell is still classed as null when I try and update the changes to the Sql database. Thanks for any help.


    //code//
    Code:
    
    	//Define controls must be added to data grid.		
    		private ComboBox cboControl	 =	new ComboBox();
    		private ComboBox cboControl2 =  new ComboBox();
    		
    
    		//Control definishion to add to DataGrid
    		DataGridTableStyle dataGridStyle = new DataGridTableStyle();
    
    		//Create the TextBox columns for the datagrid columns
    		DataGridTextBoxColumn aCol1 = new DataGridTextBoxColumn();
    		DataGridTextBoxColumn aCol2 = new DataGridTextBoxColumn();
    		DataGridTextBoxColumn aCol3 = new DataGridTextBoxColumn();
    		DataGridTextBoxColumn aCol4 = new DataGridTextBoxColumn();
    		DataGridTextBoxColumn aCol5 = new DataGridTextBoxColumn();
    		DataGridTextBoxColumn aCol6 = new DataGridTextBoxColumn();
    		CheckBox chk1 = new CheckBox();        
    		DataView Dv;
    		public string Movstring;
    		public string Combostr;
    		public int CurRow = -1;
    		public int CurCol = -1;
    		public string CellValue;
    		public bool strValue = false;
    		private System.Windows.Forms.Button button11;
    		private System.Windows.Forms.Button button12;
    		private System.Windows.Forms.Button Findbtn;
    		public System.Windows.Forms.Button UpdateSavebtn;
    		public bool imageswitch;
    		public void Imagescrn()
    		{
    			this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\house_pg2.jpg");
    		}
    		public void ImageMain()
    		{
    			this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\bg_page.jpg");
    		}
            
    
    		
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    		
    			
    		
    		
    		public void Form1_Load(object sender, System.EventArgs e)
    		{		
    			//Add a new table style to datagrid.
    			DesignTableStyle();
    			dataGrid1.RowHeadersVisible = true;
    			dataGrid1.DataSource = Movset1;
    			MovAdapt.Fill(Movset1, "MovTable1");
    			dataGrid1.DataSource	=	Movset1;
    			label1.Show();
    			Show();	   
    			dataGrid1.Show();
    									
    		}
    
    
    		//Add a new table style to datagrid.
    		private void DesignTableStyle()
    		{
    
    			cboControl.Cursor    = Cursors.Arrow;
    			cboControl2.Cursor   = Cursors.Arrow;
    						
    			dataGridStyle.MappingName=	"MovTable1";
    			dataGridStyle.PreferredRowHeight	=	24;
    			
    			//
    		
    			// Create GridColumnStyle objects for the grid columns
    			//			
    
    			aCol1.HeaderText			        = "Item No";
    			aCol1.MappingName		            = "Movie_Index";
    			aCol1.Width				            = 50;
    			
    
    			dataGridStyle.GridColumnStyles.Add(aCol1);
    			
    
    			dataGrid1.TableStyles.Add(dataGridStyle);
    						
    
    			aCol2.HeaderText			        = "Title";
    			aCol2.MappingName		            = "Title";
    			aCol2.Width         				= 100;
    			dataGridStyle.GridColumnStyles.Add(aCol2);
    	
    
    			dataGridComboBox.HeaderText			= "Category";
    			dataGridComboBox.MappingName		= "Category";
    			dataGridComboBox.Width				= 130;
    			dataGridStyle.GridColumnStyles.Add(dataGridComboBox);
    
    			dataGridComboBox2.HeaderText			= "Features";
    			dataGridComboBox2.MappingName		    = "Features";
    			dataGridComboBox2.Width				    = 130;
    			dataGridStyle.GridColumnStyles.Add(dataGridComboBox2);
    
    			aCol6.HeaderText			        = "Boxset";
    			aCol6.MappingName		            = "Boxset";
    			aCol6.Width				            = 100;
    			dataGridStyle.GridColumnStyles.Add(aCol6);
    			aCol6.TextBox.Controls.Add(chk1);          
    
    
    		}
    
    		
    		private void label1_Click(object sender, System.EventArgs e)
    		{
    		
    		}
    
    		
    
    		private void button2_Click(object sender, System.EventArgs e)
    		{    
    			this.Close();     			     
    		}
    
    		
    		private void button4_Click(object sender, System.EventArgs e)
    		{
    			dataGrid1.Hide();
    			label1.Hide();
    			Imagescrn();
    			Dv = new DataView(Movset1.Tables[0]);
    			dataGrid1.DataSource = Dv;
    			Dv.RowFilter = "Category = 'HORROR'";
    		}
    
    		private void button6_Click(object sender, System.EventArgs e)
    		{
    			Dv = new DataView(Movset1.Tables[0]);
    			dataGrid1.DataSource = Dv;
    			Dv.RowFilter = "Category = 'Action/Thriller'";
    		}
    
    		private void button8_Click(object sender, System.EventArgs e)
    		{
    			label1.Hide();
    			this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\drama_pg.jpg");
    			Dv = new DataView(Movset1.Tables[0]);
    			dataGrid1.DataSource = Dv;
    			Dv.RowFilter = "Category = 'Drama'";
    		}
    
    		private void button5_Click(object sender, System.EventArgs e)
    		{
    			Dv = new DataView(Movset1.Tables[0]);
    			dataGrid1.DataSource = Dv;
    			Dv.RowFilter = "Category = 'Family/Entertainment'";
    		}
    
    		private void button7_Click(object sender, System.EventArgs e)
    		{
    			label1.Hide();
    			this.BackgroundImage = Image.FromFile("C:\\Documents and Settings\\Mark\\My Documents\\My Pictures\\scifi_pg.jpg");
    			Dv = new DataView(Movset1.Tables[0]);
    			dataGrid1.DataSource = Dv;
    			Dv.RowFilter = "Category = 'Sci-Fi/Fantasy'";
    		}
    
    		private void button9_Click(object sender, System.EventArgs e)
    		{
    			Dv = new DataView(Movset1.Tables[0]);
    			dataGrid1.DataSource = Dv;
    			Dv.RowFilter = "Category = 'TV/Series'";
    		}
    
    		private void button10_Click(object sender, System.EventArgs e)
    		{ 			
    			ImageMain();
    			dataGrid1.RowHeadersVisible = true;
    			dataGrid1.DataSource = Movset1;
    			label1.Show();
    			Show();	   
    		}
    
    
    		private void textBox1_TextChanged(object sender, System.EventArgs e)
    		{
    			Movstring = textBox1.ToString();
    		}
    
    		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    		{
    			int Currncol = 0;
    			Currncol = dataGrid1.CurrentCell.ColumnNumber;
    			CurCol = dataGrid1.CurrentCell.ColumnNumber; 
    			CurRow = dataGrid1.CurrentCell.RowNumber; 	
    			dataGrid1.RowHeadersVisible = false;
    			
    			Currncol = dataGrid1.CurrentCell.ColumnNumber; 
    			
    			if(Currncol == 2)			{
    				
    				
    					Combostr = this.cboControl.SelectedText.ToString();
    					cboControl.SelectedIndexChanged+=new EventHandler(cboSelectedIndexChanged);										
    				
    			}
    			
    			if(Currncol == 3)
    			{	
    				
    				
    				Combostr = this.cboControl2.SelectedText.ToString();
    				cboControl2.SelectedIndexChanged+=new EventHandler(cbo2SelectedIndexChanged);
    						
    			}		
    				
    			if(Currncol == 4)
    			{			
    				MessageBox.Show("Checked col has been clicked");	
    				chk1.CheckedChanged +=new EventHandler(chk1_CheckedChanged);
                        
    
    			}			
    					
    			
    			Currncol = 0;
    
    		}
    		
    					
    			
    		private void Form1_Enter(object sender, System.EventArgs e)
    		{
    			dataGrid1.RowHeadersVisible = true;
    			
    		}
    
    		private void button12_Click(object sender, System.EventArgs e)
    		{
    			
    			dataGrid1.Show();
    
    		}
    
    		private void Findbtn_Click(object sender, System.EventArgs e)
    		{			
    			Dv = new DataView(Movset1.Tables[0]);
    			dataGrid1.DataSource = Dv;			
    			Dv.RowFilter = "Title ='"+textBox1.Text+"'";
    			if(textBox1.Text == "")
    			{
    				MessageBox.Show("Search failed: Movie Title has not been entered!! ");
    				return;
    			}
    			
    			if(Dv.Count < 1)
    			{
    				MessageBox.Show("Movie does not exist in MovieBase, please try again");
    			}			 
    		
    		}
    
    		private void UpdateSavebtn_Click_1(object sender, System.EventArgs e)
    		{
    			Mcon.Open();
    			CurCol = dataGrid1.CurrentCell.ColumnNumber; 
    			CurRow = dataGrid1.CurrentCell.RowNumber; 	
    			CellValue = dataGrid1[CurRow, CurCol].ToString();
    			MessageBox.Show(CellValue.ToString());
    			if(CellValue == "")
    			{
    
    				MessageBox.Show("No Fields can be Blank - update failed");
    				Movset1.RejectChanges();
    				dataGrid1.Show();
    				
    
    				CurRow = -1;
    				CurCol = -1;
    				Mcon.Close();
    				return;
    				
    			}
    			CellValue = dataGrid1[CurRow, CurCol].ToString();
    			CurRow = -1;
    			CurCol = -1;
    
    			MovAdapt.UpdateCommand = cb.GetUpdateCommand();
    			MovAdapt.Update(Movset1.Tables[0]);
    			Movset1.AcceptChanges();
                    
    			MessageBox.Show("MovieBase has been updated");
    						
    			Mcon.Close();
    				
    			
    			dataGrid1.RowHeadersVisible = true;
    		}
    
    		private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    		{		  
    			
    			hitTestGrid = dataGrid1.HitTest(e.X, e.Y);
    
    			//Add combobox control to datagrid.
    			dataGridComboBox.TextBox.Controls.Add( cboControl );
    			cboControl.Items.AddRange(new string[8]{"","Horror","Action/Thriller","TV/Series","Sci-fi/Fantasy","Family","Drama","Comedy"});
    
    			dataGridComboBox2.TextBox.Controls.Add( cboControl2 );
    			cboControl2.Items.AddRange(new string[4]{"","Standard","Extended Edtn","Directors Cut"});
    
     			
    			
    		}	
    
    		//Initialize Conrols for adding in DataGrid cells.
    		private void InitializeControls()
    		{		
    			//combobox property
    			cboControl.Cursor			=	Cursors.Hand;
    			cboControl.DropDownStyle	=	ComboBoxStyle.DropDownList;
    			//combobox events.	
    		
    		}
    		private void cboSelectedIndexChanged(object sender, System.EventArgs e)
    		{			
    			
    			dataGrid1[CurRow, CurCol] = cboControl.SelectedItem.ToString();
    			CurRow = 0;
    			CurCol = 0;
    			
    		}
    
    		private void cbo2SelectedIndexChanged(object sender, System.EventArgs e)
    		{						
    			dataGrid1[CurRow, CurCol] = cboControl2.SelectedItem.ToString();
    			CurRow = 0;
    			CurCol = 0;		
    			
    			
    		}	
    	
    		private void chk1_CheckedChanged(object sender, EventArgs e)
    		{
    			if(chk1.Checked)
    			{
    				CurCol = 0;		
    				CurCol = 0;		
    				chk1.Enabled = true;
    								
      
    
    			}
    		
    		}		
    	}
    }
     
    Last edited by a moderator: Aug 11, 2009

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice