c#windows form使用RadioButton过滤

时间:2017-08-22 17:02:24

标签: c# windows-forms-designer

任何人都可以告诉我如何根据选定的单选按钮过滤Datagridview,我附上了我创建的表单的截图,需要根据存款和取款进行过滤。我把TransactionType作为枚举。

Screenshot of form

3 个答案:

答案 0 :(得分:2)

循环遍历DGV中的行并检查第一个单元格值=值(存款/取款/两者取决于选中的RadioButton),然后设置

DataGridView1.Rows[rowIndex].Visible = false;

答案 1 :(得分:0)

您可以根据所选的单选按钮更改DataSource

BindingSource bs = new BindingSource();
grid.DataSource = bs;

通过更改单选按钮选择

bs.DataSource = q;
bs.ResetBindings(false);

用于q:

  1. var q = Transactions
  2. var q = Transactions.Where(t=>t.TransactionType==Deposit)
  3. var q = Transactions.Where(t=>t.TransactionType==Withdrawal)

答案 2 :(得分:0)

执行所需操作的代码如下所示:(可能不是正确的变量名称)

  1. 检查选中了哪个单选按钮
  2. 根据哪一个被检查,读取列" transactionType"每行,
  3. 呈现或不显示您想要的行。
  4. Foreach(DataGridViewRow row in DataGridView.Rows)
    {
      if(radioButtonDeposit.isChecked())
      {
        if(row["TransactionType"].Value == Enum.Deposit)
        {
           row.Visible = true;
        }
        else
        {
           row.Visible = false;
        }
      }
      else if(radioButtonWithdrawal.isChecked())
      {
        if(row["TransactionType"].Value == Enum.Withdrawal)
        {
           row.Visible = true;
        }
        else
        {
          row.Visible = false;
        }
      }
      else 
        row.Visible = true;            
    }