【问题标题】:Dynamically hide a row in a DataGrid动态隐藏 DataGrid 中的一行
【发布时间】:2016-01-28 11:51:15
【问题描述】:

当用户为另一行选择某些内容时,我想隐藏 DataGrid 的 1 行。

我该怎么做?

 private void DataGridCommands_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     for (int i = 0; i < dataGridData.Items.Count; i++)
     {
         if ((dataGridData.Items[i] as DataForTable).MsgType ==   _qf.ElementAt(DataGridCommands.SelectedIndex).Mcode)
         {
             //need to hide 1 row from datagriddata
         }
     }
 }

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    我不知道你想用什么逻辑从你的数据网格中隐藏行,所以我将向你展示一个简单的示例。

    关键是您需要检索该行的容器并将其隐藏。让我们看看如何。这是 XAML:

    <StackPanel>
        <DataGrid AutoGenerateColumns="True" CanUserAddRows="False"
                    SelectionChanged="DataGrid_SelectionChanged" Name="dataGrid" />
    </StackPanel>
    

    现在是代码隐藏:

    public partial class Window3 : Window
    {
        private ObservableCollection<Person> people = new ObservableCollection<Person>();
    
        public Window3()
        {
            InitializeComponent();
    
            people.Add(new Person() { Name = "Paul", Surname = "Green" });
            people.Add(new Person() { Name = "Mike", Surname = "Gray" });
            people.Add(new Person() { Name = "John", Surname = "Black" });
    
            dataGrid.ItemsSource = people;
        }
    
        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGridRow dataGridRow;
            foreach (Person p in e.AddedItems)
            {
                if (p.Name == "Mike")
                {
                    dataGridRow = dataGrid.ItemContainerGenerator.ContainerFromItem(people[2]) as DataGridRow;
                    dataGridRow.Visibility = System.Windows.Visibility.Collapsed;
                    return;
                }
            }
    
            dataGridRow = dataGrid.ItemContainerGenerator.ContainerFromItem(people[2]) as DataGridRow;
            dataGridRow.Visibility = System.Windows.Visibility.Visible;
        }
    }
    

    我的逻辑是,如果用户选择一个名为“Mike”的人,我想隐藏我的集合中的最后一个人对象。因此,我检索了与我要隐藏/显示的行相对应的容器(在本例中为 DataGridRow)。然后将其 Visibility 设置为正确的值。

    我希望我的示例可以为您的问题提供提示。

    【讨论】:

      【解决方案2】:

      尝试以下解决方案

      public partial class frmTestGirdBinding : Form
      {
          CustomDataCollection cdata = new CustomDataCollection();
          Random rnd = new Random();
          public frmTestGirdBinding()
          {
              InitializeComponent();
              this.dataGridView1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView1_DataBindingComplete);
          }
      
          private void frmTestGirdBinding_Load(object sender, EventArgs e)
          {
              BindingSource bindingSource1 = new BindingSource();
              bindingSource1.DataSource = cdata;
              dataGridView1.DataSource = bindingSource1;            
      
          }
      
          private void button1_Click(object sender, EventArgs e)
          {
              for (int i = 0; i < cdata.Count; i++)
              {
                  cdata[i].Reading = (float)rnd.NextDouble();
              }
              dataGridView1.Refresh(); //without this all rows are not updating
          }
      
          private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
          {
              //InVisible the rows
              dataGridView1.Rows[2].Visible = false;
              dataGridView1.Rows[3].Visible = false;
          }
      }
      

      另一个我想发布的sn-p

      foreach (DataGridViewRow dr in taggGrid.Rows)
              {
                  if (dr.Cells[4].Value.ToString() == "False")
                  {
                      dr.Visible = false;
                  }
              }
      

      【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多