在C#的网格中选中或取消选中复选框单元格后立即更改其他单元格的值 [英] changing values of other cells as soon as when a checkbox cell is ticked or unticked in a grid in c#

查看:107
本文介绍了在C#的网格中选中或取消选中复选框单元格后立即更改其他单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现此

在c#中的网格中选中或取消选中复选框单元格后立即更改其他单元格的值
请帮忙

在此先感谢

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

推荐答案

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. :)


您好,
如果我理解正确的问题,
当您选中其中一个复选框时,另一个复选框
在同一DataGridView行中,应取消选中.
为此,您需要用户的行数和列数
取消选中或选中复选框.
如果它是正确的复选框,则另一个应易于取消选中或选中.

所以,
您需要做的只是(为它创建代码.)
当您找到
时,处理称为CellContentClick的事件
中单元格的行和列数 用户已选中复选框的DataGridView.
此外,您还可以轻松取消选中或选中同一行中的所有其他单元格.

例子:

创建具有3列和最多10行的DataGridView1,
第一列的初始状态已检查,其他所有状态均未选中.
当您取消选中第一列checkBox时,另一个更改状态变为选中状态,
当您选中第一列checkBox时,其他状态更改为未选中.

Program.cs

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

/*
 * 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




MainForm.cs

/*
 * 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



MainForm.Designer.cs

/*
 * 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;
	}
}



祝一切顺利
PerićŽeljko



All the best
Perić Željko


尝试一下.

Try this one.

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


这篇关于在C#的网格中选中或取消选中复选框单元格后立即更改其他单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆