动态隐藏DataGrid中的行

时间:2016-01-28 11:51:16

标签: c# wpf datagrid

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

我该怎么做?

 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
         }
     }
 }

2 个答案:

答案 0 :(得分:1)

尝试以下解决方案

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;
    }
}

我想发布的另一个片段

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

答案 1 :(得分: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;
    }
}

我的逻辑是,如果用户选择一个名为&#34; Mike&#34;的人,我想隐藏我馆藏中的最后一个人物。所以我检索容器(在这种情况下是一个DataGridRow),它对应于我想要隐藏/显示的行。然后将其可见性设置为正确的值。

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