C#中的Datagridview事件 [英] Datagridview Event in C#

查看:692
本文介绍了C#中的Datagridview事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友您好,



我正在使用Windows应用程序。我正在使用datagridview。 datagridview中还有一个checkboxcolumn。我需要的是当我检查复选框时应该启用一些控件,当我取消选中控件时应该禁用。我正在使用CellContentClick事件。我应该用别的东西吗?







//代码



Hello friends,

I am working on windows application. IN which I am using datagridview . There is also a checkboxcolumn in datagridview. My need is when I checked the checkboxcell there should be some control enabled and when I unchecked control should be disabled. I am using CellContentClick Event. Should I use something else.



//code

private void dgHelpDesk_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {

           for (int i = 0; i < dgHelpDesk.RowCount; i++)
           {
               if (Convert.ToBoolean(this.dgHelpDesk.Rows[i].Cells["checkboxcolumn"].Value))
               {
                   cbAssigned.Enabled = true;
                   cbStatus.Enabled = true;
                   cbWorkFlow.Enabled = true;

               }

               else
               {
                   cbAssigned.Enabled = false;
                   cbStatus.Enabled = false;
                   cbWorkFlow.Enabled = false;
               }
           }
       }

推荐答案

不,你需要获得一个实例 CheckBox 并处理事件 CheckBox.CheckedChanged

http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox .checkedchanged.aspx [ ^ ]。



要获取控件(将事件处理程序添加到其事件实例的调用列表中,在您的情况下): http://msdn.microsoft.com/ en-us / library / system.windows.forms.datagridview.editingcontrol.aspx







好​​吧,就像一些微妙的m我会告诉你该怎么做:

No, you rather need to get an instance of a CheckBox and handle the event CheckBox.CheckedChanged:
http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checkedchanged.aspx[^].

To get a control (to add an event handler to the invocation list of its event instance, as in your case): http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrol.aspx.



Okay, as some delicate moment is involved, I''ll show you what to do:
void SetupGridViewEditEvents(DataGridView gridView) {
    gridView.CellValueChanged += (sender, eventArgs) => {
        DataGridViewCell cell = gridView.Rows[eventArgs.RowIndex].Cells[eventArgs.ColumnIndex];
        DataGridViewCheckBoxCell checkBoxCell = cell as DataGridViewCheckBoxCell;
        if (checkBoxCell != null) {
            CheckState state = (CheckState)checkBoxCell.Value;
            HandleCellCheckBoxCheckedChanged(
                eventArgs.RowIndex,
                eventArgs.ColumnIndex,
                state);
        } // if checkBoxCell
        //...
    }; // gridView.CellValueChanged
} // SetupGridViewEditEvents

void HandleCellCheckBoxCheckedChanged(int cellRow, int cellColumn, CheckState state) {
    // you handler logic goes here
} // HandleCellCheckBoxCheckedChanged









代码示例于2013年3月28日修复。很抱歉给您带来不便。



-SA


这篇关于C#中的Datagridview事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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