Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a code where if the combo box selection is changed, the relating row hides, but nothing is changing when I select options in the combo box

private void ViewOrdersCB_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (DataGridViewRow dr in CustOrdersDGV.Rows)
    {
        if (dr.Cells.Count > 9)  // validate the index
            if (dr.Cells[9].Value.ToString() == "Pending Orders" && ViewOrdersCB.SelectedText == "Processing Orders")
            {
                dr.Visible = false;
            }
        else if (dr.Cells[9].Value.ToString() == "Processing Orders" && ViewOrdersCB.SelectedText == "Pending Orders")
                {
                    dr.Visible = false;
                }
    }


What I have tried:

    int targetColumnIndex = 9;
    if (dr.Cells.Count > targetColumnIndex)  // validate the index
        if (dr.Cells[targetColumnIndex].Value.ToString() == "Pending Orders" && ViewOrdersCB.SelectedText == "Processing Orders")
        {
            dr.Visible = false;
        }
    else if (dr.Cells[targetColumnIndex].Value.ToString() == "Processing Orders" && ViewOrdersCB.SelectedText == "Pending Orders")
            {
                dr.Visible = false;
            }

}
Posted
Updated 21-Feb-18 4:41am
Comments
Maciej Los 21-Feb-18 11:15am    
How the datagridview is bound with data?

1 solution

You can do it by changing the datagriditem data template in the Xaml. You may have to add a property to your object or your form code-behind that can return the Visibility of the object. In my own code, I have a base notifiable object that has properties for IsSelected, IsModified, IsVisible, etc. In your OnSelectionChanged event, all you would have to do is set an IsVisible property to the approiate value, and bind your gridview item's visibility property to the IsVisible property in your object.

Go here - Downloads: SQLXAgent - Jobs for SQL Express - Part 1 of 6[^]

Look for the file Notifiable.cs in the WPFCommon assembly. That should get you started on modifying your viewmodel to accommodate what I suggested above.
 
Share this answer
 
v2
Comments
Maciej Los 21-Feb-18 11:22am    
WPF? DataGridView is WinForm control.
#realJSOP 21-Feb-18 12:15pm    
It's also available in WPF. The OP didn't specify winforms or wpf.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900