我有多个列和行的datagridview的。 第一列包含一个复选框。 我希望用户能够选择多个复选框,然后执行一个动作。 例如,如果他们选择的行1和2的复选框,从其他列中的行1和2中的数据可被选择并传递到一个消息。

我知道我需要使用checkbox_changed事件来做到这一点。但是我有麻烦工作如何为多行做到这一点?

有帮助吗?

解决方案

在按钮点击事件做:

static int SelectColumnIndex = 0;
PerformAction_Click(object sender, System.EventArgs e)
{
    string data = string.Empty;
    foreach(DataGridViewRow row in MyDataGridView.Rows)
    {
      if(row.Cells[SelectColumnIndex].Value!=null &&
             Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
      {
        foreach(DataGridViewCell cell in row.Cells)
        {
          if(cell.OwningColumn.Index!= SelectColumnIndex)
          {
            data+= (cell.Value + " ") ; // do some thing
          }
        }
        data+="\n";
      }
   }
   MessageBox.Show(data, "Data");
}

其他提示

如果您希望用户点击一个按钮来进行操作,那么你需要处理的是按钮的Click事件,而不是复选框更改事件......当单击该按钮,只需通过所有您的DataGridView的行和与选中的复选框执行上行一个动作。

static int SelectColumnIndex = 0;

PerformAction_Click(object sender, System.EventArgs e)
{
    string data = string.Empty;    

    foreach(DataGridViewRow row in MyDataGridView.Rows)     
    {
        if(row.Cells[SelectColumnIndex].Value != null 
            &&
         Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)       
        {
            foreach(DataGridViewCell cell in row.Cells)        
            {
                if (cell.OwningColumn.Index != SelectColumnIndex)                               
                {
                    data+= (cell.Value + " ") ; // do some thing           
                 }
            }

            data+="\n";
        }
    }

    MessageBox.Show(data, "Data");
}

我觉得你不需要对任何事件 i。由该代码解决了这个问题:

//In Fill DataGridViewEvent :
        DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxColumn();
        ChCol.Name = "CheckedRow";
        ChCol.HeaderText = "انتخاب";
        ChCol.Width = 50;
        ChCol.TrueValue = "1";
        ChCol.FalseValue = "0";
        dg.Columns.Insert(0, ChCol);

// In Button Event put these codes:

            try
            {
                MessageBox.Show(row.Cells[2].Value.ToString() + " --> " +
                row.Cells["CheckedRow"].Value.ToString());
            }
            catch
            {
                // Nothing Act
            }


enter code here

您输出由该参数:     条目标识号 - > 0 //对于未经检查的价值     条目标识号 - > 1 //对于检查的值

现在可以筛选返回1的值,为您的操作行 我测试这个代码,它为我工作

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top