ToolStripButton单击事件无法复选框选中状态对于WindowsForm中的DataGridView [英] ToolStripButton Click Event Could not checkbox checked status For DataGridView in WindowsForm

查看:51
本文介绍了ToolStripButton单击事件无法复选框选中状态对于WindowsForm中的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我点击datagridview中的复选框后点击toolstripbutton



windows Form形式,它总是将复选框的选中状态显示为false。 />


我附上下面的示例代码可以突出我的问题



请帮助我



谢谢



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 WindowsFormsApplication1
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();


DataTable table2 = new DataTable( 药物);
table2.Columns.Add( id);
table2.Columns.Add( 药物);
table2.Rows.Add( 1 阿替洛尔);
table2.Rows.Add( 2 阿莫西林);

// 创建一个DataSet并将两个表放入其中。
DataSet set = new DataSet( office);
set .Tables.Add(table2);

dataGridView1.AutoGenerateColumns = true ;
dataGridView1.DataSource = set ; // dataset
dataGridView1.DataMember = 药物;
}

private void button1_Click( object sender,EventArgs e)
{
List< DataGridViewRow> rows_with_checked_column = new List< DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells [ chk ]。值)== true
{
// rows_with_checked_column.Add(row);
MessageBox.Show( 经过);
}
else
{
MessageBox.Show( 未选中);
}
}
}

private void dataGridView1_CellContentClick( object sender,DataGridViewCellEventArgs e)
{
if ( e.ColumnIndex == 0
{
foreach (DataGridViewRow dr dataGridView1.Rows)
{
dr.Cells [ 0 ]。值= < span class =code-keyword> false
; // sıfırın
}
}

}

private void toolStripButton2_Click( object sende r,EventArgs e)
{
List< DataGridViewRow> rows_with_checked_column = new List< DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells [ chk ]。值)== true
{
// rows_with_checked_column.Add(row);
MessageBox.Show( 经过);
}
else
{
MessageBox.Show( 未选中);
}
}
}

private void toolStrip1_ItemClicked( object sender,ToolStripItemClickedEventArgs e)
{

}
}
}



 命名空间 WindowsFormsApplication1 
{
partial class Form1
{
/// < 摘要 >
/// 必需的设计变量。
/// < / summary >
private System.ComponentModel.IContainer components = null ;

/// < 摘要 >
/// 清理正在使用的所有资源。
/// < / summary >
/// < param < span class =code-summarycomment> name =disposing > 如果管理资源应予以处理;否则,false。< / param >
protected 覆盖 void Dispose( bool 处理)
{
if (处置&&(components!= null ))
{
components.Dispose();
}
base .Dispose(disposing);
}

#region Windows窗体设计器生成的代码

/// < 摘要 >
/// Designer支持所需的方法 - 不要修改
/// 使用代码编辑器的方法的内容。
/// < / summary >
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = System.ComponentModel.ComponentResourceManager( typeof (Form1));
this .dataGridView1 = new System.Windows.Forms.DataGridView();
this .chk = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this .Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this .button1 = new System.Windows.Forms.Button();
this .tabControl1 = new System.Windows.Forms.TabControl();
this .tabPage1 = new System.Windows.Forms.TabPage();
this .tabPage2 = new System.Windows.Forms.TabPage();
this .toolStrip1 = new System.Windows.Forms.ToolStrip();
this .toolStripButton2 = new System.Windows.Forms.ToolStripButton();
((System.ComponentModel.ISupportInitialize)( this .dataGridView1))。BeginInit();
this .tabControl1.SuspendLayout();
this .tabPage1.SuspendLayout();
this .toolStrip1.SuspendLayout();
this .SuspendLayout();
//
// dataGridView1
//
this .dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this .dataGridView1.Columns.AddRange( new System.Windows.Forms.DataGridViewColumn [] {
this .chk,
this .Column2});
.dataGridView1.Location = new System.Drawing.Point( 9 37 );
.dataGridView1.Name = dataGridView1 ;
.dataGridView1.Size = new System.Drawing.Size( 240 150 );
.dataGridView1.TabIndex = 0 ;
this .dataGridView1.CellContentClick + = new System.Windows.Forms.DataGridViewCellEventHandler( .dataGridView1_CellContentClick);
//
// chk
//
this .chk.HeaderText = column1的;
.chk.Name = chk ;
//
// Column2
//
this .Column2.HeaderText = 列2;
this .Column2.Name = Column2 ;
//
// button1
//
this .button1.Location = new System.Drawing.Point(-1 , 227 );
this .button1.Name = button1 ;
this .button1.Size = new System.Drawing.Size( 75 23 );
this .button1.TabIndex = 1 ;
this .button1.Text = button1 ;
this .button1.UseVisualStyleBackColor = true ;
this .button1.Click + = new System.EventHandler( .button1_Click);
//
// tabControl1
//
this .tabControl1.Controls.Add( this .tabPage1);
this .tabControl1.Controls.Add( this .tabPage2);
this .tabControl1.Location = new System.Drawing.Point( 12 12 );
this .tabControl1.Name = tabControl1 ;
this .tabControl1.SelectedIndex = 0 ;
this .tabControl1.Size = new System.Drawing.Size( 260 238 );
this .tabControl1.TabIndex = 2 ;
//
// tabPage1
//
this .tabPage1.Controls.Add( this .toolStrip1);
this .tabPage1.Controls.Add( this .dataGridView1);
this .tabPage1.Location = new System.Drawing.Point( 4 22 );
this .tabPage1.Name = tabPage1 ;
this .tabPage1.Padding = new System.Windows.Forms.Padding( 3 );
this .tabPage1.Size = new System.Drawing.Size( 252 212 );
this .tabPage1.TabIndex = 0 ;
this .tabPage1.Text = tabPage1 ;
this .tabPage1.UseVisualStyleBackColor = true ;
//
// tabPage2
//
this .tabPage2.Location = new System.Drawing.Point( 4 22 );
this .tabPage2.Name = tabPage2 ;
this .tabPage2.Padding = new System.Windows.Forms.Padding( 3 );
this .tabPage2.Size = new System.Drawing.Size( 252 212 );
.tabPage2.TabIndex = 1 ;
this .tabPage2.Text = tabPage2 ;
this .tabPage2.UseVisualStyleBackColor = true ;
//
// toolStrip1
//
this .toolStrip1.Items.AddRange( new System.Windows.Forms。 ToolStripItem [] {
this .toolStripButton2});
this .toolStrip1.Location = new System.Drawing.Point( 3 3 );
this .toolStrip1.Name = toolStrip1 ;
this .toolStrip1.Size = new System.Drawing.Size( 246 25 );
this .toolStrip1.TabIndex = 1 ;
this .toolStrip1.Text = toolStrip1 ;
this .toolStrip1.ItemClicked + = new System.Windows.Forms.ToolStripItemClickedEventHandler( .toolStrip1_ItemClicked);
//
// toolStripButton2
//
this .toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this .toolStripButton2.Image =((System.Drawing.Image)(resources.GetObject( toolStripButton2.Image)));
this .toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
this .toolStripButton2.Name = toolStripButton2 ;
this .toolStripButton2.Size = new System.Drawing.Size( 31 22 );
this .toolStripButton2.Text = 编辑;
this .toolStripButton2.Click + = new System.EventHandler( .toolStripButton2_Click);
//
// Form1
//
this .AutoScaleDimensions = new System.Drawing.SizeF(6F,13F) ;
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size( 284 262 );
this .Controls.Add( this .tabControl1);
this .Controls.Add( this .button1);
this .Name = Form1< /跨度>;
this .Text = Form1< /跨度>;
((System.ComponentModel.ISupportInitialize)( this .dataGridView1))。EndInit();
this .tabControl1.ResumeLayout( false );
this .tabPage1.ResumeLayout( false );
this .tabPage1.PerformLayout();
this .toolStrip1.ResumeLayout( false );
this .toolStrip1.PerformLayout();
this .ResumeLayout( false );

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridViewCheckBoxColumn chk;
private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButton2;
}
}

解决方案

嗨Ralf Meier,



道歉,因为我没有添加c#.net标签,因为我很难添加c#.net标签。



我想要选中复选框状态toolstripbutton点击事件但是我选择了复选框,它总是返回false。



但是如果我使用普通按钮它正常工作。



以上代码示例将突出显示我的问题。





< pre lang =c#> private void toolStripButton2_Click( object sender,EventArgs e)
{
List< datagridviewrow> rows_with_checked_column = new List< datagridviewrow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{

if (Convert.ToBoolean(row.Cells [ chk]。值)== true
{
// rows_with_checked_column.Add(row);
MessageBox.Show( 选中);
}
else
{
MessageBox.Show( 未选中);
}
}
}
< / datagridviewrow > < / datagridviewrow < span class =code-keyword>>


Whenever i clicked toolstripbutton after checking the checkbox inside datagridview in

"windows Form" form its always showing the checked status of checkbox as "false" .

I have attached the sample code below which can highlight my issue

Kindly assist me

Thanks

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           

            DataTable table2 = new DataTable("medications");
            table2.Columns.Add("id");
            table2.Columns.Add("medication");
            table2.Rows.Add(1, "atenolol");
            table2.Rows.Add(2, "amoxicillin");

            // Create a DataSet and put both tables in it.
            DataSet set = new DataSet("office");
            set.Tables.Add(table2);

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = set; // dataset
            dataGridView1.DataMember = "medications";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells["chk"].Value) == true)
                {
                    //rows_with_checked_column.Add(row);
                    MessageBox.Show("Checked");
                }
                else
                {
                    MessageBox.Show("not Checked");
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    dr.Cells[0].Value = false;//sıfırın
                }
            }

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells["chk"].Value) == true)
                {
                    //rows_with_checked_column.Add(row);
                    MessageBox.Show("Checked");
                }
                else
                {
                    MessageBox.Show("not Checked");
                }
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}


namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.chk = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.button1 = new System.Windows.Forms.Button();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.chk,
            this.Column2});
            this.dataGridView1.Location = new System.Drawing.Point(9, 37);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(240, 150);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
            // 
            // chk
            // 
            this.chk.HeaderText = "Column1";
            this.chk.Name = "chk";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Column2";
            this.Column2.Name = "Column2";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(-1, 227);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(12, 12);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(260, 238);
            this.tabControl1.TabIndex = 2;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.toolStrip1);
            this.tabPage1.Controls.Add(this.dataGridView1);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(252, 212);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "tabPage1";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(252, 212);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "tabPage2";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButton2});
            this.toolStrip1.Location = new System.Drawing.Point(3, 3);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(246, 25);
            this.toolStrip1.TabIndex = 1;
            this.toolStrip1.Text = "toolStrip1";
            this.toolStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.toolStrip1_ItemClicked);
            // 
            // toolStripButton2
            // 
            this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));
            this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton2.Name = "toolStripButton2";
            this.toolStripButton2.Size = new System.Drawing.Size(31, 22);
            this.toolStripButton2.Text = "Edit";
            this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.tabControl1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.DataGridViewCheckBoxColumn chk;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.ToolStripButton toolStripButton2;
    }
}

解决方案

Hi Ralf Meier,

Apologies, for not adding c#.net tag , as i had difficulty in adding c#.net tag.

I want to get the checkbox selected status in the toolstripbutton click event but eventhough i selected the checkbox its always returns false .

But if i use normal button its working properly .

The Above code sample will highlight my issue.


private void toolStripButton2_Click(object sender, EventArgs e)
        {
            List<datagridviewrow> rows_with_checked_column = new List<datagridviewrow>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {

                if (Convert.ToBoolean(row.Cells["chk"].Value) == true)
                {
                    //rows_with_checked_column.Add(row);
                    MessageBox.Show("Checked");
                }
                else
                {
                    MessageBox.Show("not Checked");
                }
            }
        }
</datagridviewrow></datagridviewrow>


这篇关于ToolStripButton单击事件无法复选框选中状态对于WindowsForm中的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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