Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
i Need To Implement this

changing values of other cells as soon as when a checkbox cell is ticked or unticked in a grid in c#

Please help

Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 9-Dec-11 0:01am    
Tag it: WPF, Silverlight, Forms, ASP.NET.., what?
--SA

C#
foreach (GridViewRow gr in grdMain.Rows)
            {
                CheckBox chkSelectRight = (CheckBox)gr.FindControl("chkSelectRight");
                if (chkSelectRight.Checked)
                {
                    //find controls and set or update values according to yourself...
                }
            }


Hope this will help you.
Don't forget to mark as answer if it helps. :)
 
Share this answer
 
Comments
Madhukumar N 9-Dec-11 6:13am    
hi
im talking here about the same row in the grid. the grid contains many check boxes and at a time only one checkbox must be checked so as soon as i check any checkbox others should get unchecked.
due to some reasons i dont want to use radio button for that.
so please help
Hello ,
If I understand the question correct,
when you check one of the checkBox the other checkBox
in the same DataGridView row should be unchecked.
For that you need the number of Row and Column where is the user
unchecked or checked the checkBox.
If it is the right checkBox the other should be easy to uncheck or check.

So,
all you need is to (create code for..)
process the event called CellContentClick when you will find the
number of Row and Column of the cell in
the DataGridView in wich the user have checked the checkBox.
Afther that you will easy uncheck or check all the other cells in the same row.

Example :

Create DataGridView1 with 3 columns and maximum 10 Rows,
first column initial state is checked and all the other are unchecked.
When you uncheck first column checkBox the other change state to checked,
and when you check first column checkBox the other change state to unchecked.

Program.cs

C#
/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 7.10.2011
 * Time: 12:04
 * 
 */
using System;
using System.Windows.Forms;
namespace Data_grid_view_example
{
	/// <summary>
	/// Class with program entry point.
	/// </summary>
	internal sealed class Program
	{
		/// <summary>
		/// Program entry point.
		/// </summary>
		[STAThread]
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
		
	}
}



MainForm.cs

C#
/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 7.10.2011
 * Time: 12:04
 * 
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Data_grid_view_example
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// Call sub for seting initial 
			// values for checkBox in all DataGridView
			//
			
				DataGridView1SetValue();
			
		}
		
		void DataGridView1SetValue()
		{
			//
			// Set initial values for checkBox in all DataGridView
			//
			int Row = 0;
			int Column = 0;
			
			//
			// Set number of rows to 10
			//
			dataGridView1.RowCount = 10;
			
			//
			// Set value of checkBox in first column to true (checked)
			//
			while (Row < 10)
			{
				dataGridView1[Column,Row].Value = true;
				dataGridView1[Column+1,Row].Value = false;
				dataGridView1[Column+2,Row].Value = false;
				Row = Row + 1;
			}
			dataGridView1.Refresh();
		}
		
		
		void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			int Row = 0;
			int Column = 0;
			string state = " ";
			//
			// Get  the number of Row and Column
			//
			Row = dataGridView1.CurrentRow.Index;
			Column = dataGridView1.CurrentCell.ColumnIndex;
			
			//
			// If the checkBox in first Column is checked
			// 		then uncheck all the other
			// and if the checkBox in first column is unchecked,
			// 		check all the other
			//
			if (Column == 0)
			{
				state = dataGridView1[Column,Row].Value.ToString();
				if (state == "True") 
				{
					dataGridView1[Column,Row].Value = false;
					dataGridView1[Column+1,Row].Value = true;
					dataGridView1[Column+2,Row].Value = true;
				}
				else
				{
					dataGridView1[Column,Row].Value = true;
					dataGridView1[Column+1,Row].Value = false;
					dataGridView1[Column+2,Row].Value = false;
				}
			}
			dataGridView1.Refresh();
		}
		
	}
}


MainForm.Designer.cs

C#
/*
 * Created by SharpDevelop.
 * User: Perić Željko
 * Date: 7.10.2011
 * Time: 12:04
 * 
 */
namespace Data_grid_view_example
{
	partial class MainForm
	{
		/// <summary>
		/// Designer variable used to keep track of non-visual components.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
		
		/// <summary>
		/// Disposes resources used by the form.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
			this.dataGridView1 = new System.Windows.Forms.DataGridView();
			this.Column1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.Column2 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.Column3 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGridView1
			// 
			this.dataGridView1.AllowUserToAddRows = false;
			this.dataGridView1.AllowUserToDeleteRows = false;
			this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
									this.Column1,
									this.Column2,
									this.Column3});
			this.dataGridView1.Location = new System.Drawing.Point(10, 23);
			this.dataGridView1.Name = "dataGridView1";
			this.dataGridView1.Size = new System.Drawing.Size(344, 322);
			this.dataGridView1.TabIndex = 0;
			this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataGridView1CellContentClick);
			// 
			// Column1
			// 
			dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
			dataGridViewCellStyle1.NullValue = "False";
			this.Column1.DefaultCellStyle = dataGridViewCellStyle1;
			this.Column1.HeaderText = "Column1";
			this.Column1.Name = "Column1";
			this.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.Column1.Width = 101;
			// 
			// Column2
			// 
			this.Column2.HeaderText = "Column2";
			this.Column2.Name = "Column2";
			this.Column2.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.Column2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			// 
			// Column3
			// 
			this.Column3.HeaderText = "Column3";
			this.Column3.Name = "Column3";
			this.Column3.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.Column3.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			// 
			// MainForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(455, 489);
			this.Controls.Add(this.dataGridView1);
			this.Name = "MainForm";
			this.Text = "Data grid view example";
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
			this.ResumeLayout(false);
		}
		private System.Windows.Forms.DataGridViewCheckBoxColumn Column3;
		private System.Windows.Forms.DataGridViewCheckBoxColumn Column2;
		private System.Windows.Forms.DataGridViewCheckBoxColumn Column1;
		public System.Windows.Forms.DataGridView dataGridView1;
	}
}


All the best
Perić Željko
 
Share this answer
 
v7
Try this one.

C#
CheckBox chk;
CheckBox chk2;
foreach(GridViewRow row in GridView1.Rows)
{
   chk = (CheckBox)row.Cells[0].FindControl("chkBox");
   if(chk.Checked == true)
   {
      foreach(GridViewRow row2 in GridView1.Rows)
      {
         chk2 = (CheckBox)row.Cells[0].FindControl("chkBox");
         if(row2.RowIndex != row.RowIndex)
         {
            chk2.Checked == false;
         }
      }
   }
}


Hope it helps!

Regards,
Eduard
 
Share this answer
 
Dear Madhu,

When you are ticking the checkbox cell in the grid then found the row of which the checkbox is checked by applying a foreach loop on the grid and then found other controls in the row and assign them the appropriate value.



Thanks

Varun Sareen
 
Share this answer
 

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