C# DataGridView - disabling rows?

Soldato
Joined
26 Aug 2006
Posts
9,726
Location
62.156684,-49.781113
Not had much experience of the DGV, but think I'm using it outside of it's intended scope and pushing it too far, so open to suggestions for alternatives if someone can think of one...

I populate it with a set of items of a known maximum size, which worked fine when the description for each item belonged to the same row in the view. This meant I could just hide the rows when not applicable.

However, I now want to pull the descriptions out into a static list and have a number of DGVs showing applicable items in multiple lists based on other conditions. This creates the problem of not being able to hide rows in the variable columns, as they then wouldn't correspond with the description in the static list.

Soooo, is there a way of disabling a row (as in greying it out) or preventing the checkbox in the row from being selected? I can't seem to seem to find a nice method for either of these solutions...?

Let me know if I'm not making any sense!
 
I'm not 100% sure what the complications are with the method you are using now but for filtering a DGV to only show what you want you can get it to filter rows by using a DataTable and DataTable.select(expression, sortOrder) then set the DGV.DataSource to the filtered DataTable

Hope this is close to what you were after. If you have a screenshot I may be able see what you're after.
 
Last edited:
However, I now want to pull the descriptions out into a static list and have a number of DGVs showing applicable items in multiple lists based on other conditions. This creates the problem of not being able to hide rows in the variable columns, as they then wouldn't correspond with the description in the static list.

Can you explain this in more detail, preferably with a screen as mentioned above.

But it does sound like you are going about it in the wrong way by hiding rows (why add them in the first place if you're just going to hide them!?). I second using the DataTable.select method

So something like (pseudo code)

DGV1.DataSource = getMyDataRows(expression);
DGV1.DataBind();

private List<MyItemList> getMyDataRows(string _expression)
{
return m_globalDGV.select(_expression, sortOrder);
}


If you do that for each of the new DGV's (and pass it a dif expression for each one, you should be sorted).
 
Currently, this is what I have - the description field is part of the same DGV, so in cases where certain rows do not appear everything still lines up:

current.gif


However, I'd like to extend it to have multiple DGVs containing variations of the same data but with a static description field. Obviously here, removing rows causes them to shift up and therefore not line up with the description:

static.gif


So I'm looking at some way of just 'disabling' the rows rather than removing them...
 
so hang on. You are putting 2 DGV's 'next' to each other on the page to give the illusion of 1? So the deletion of a row from one DVG scews the grid data?

If that's the case the it would make far more sense to create a single DGV with extra columns and then do some logic in the background to populate.

Any chance of seeing the code which populates the DVG(s)?
 
so hang on. You are putting 2 DGV's 'next' to each other on the page to give the illusion of 1? So the deletion of a row from one DVG scews the grid data?

In a sense, yes. I'll want more DGVs, some are only applicable under certain conditions... But it's far easier to hide a whole DGV obviously.

If that's the case the it would make far more sense to create a single DGV with extra columns and then do some logic in the background to populate.

This is the slightly roundabout way I was looking to avoid, yet obviously feasible. If it's the best option I'll have to go for that.

Any chance of seeing the code which populates the DVG(s)?

I might confuse the situation more with that. Basically, it's not dynamic, I create the DGV with a list of known values, then 'hide' certain rows under certain conditions. There's no database links or such, the entries have fixed values.
 
Ok, things are a lot clearer now. But why you are doing things this way still has me a little confused - are you trying to hack a quick demo together, or are you just starting out programming?

Have you thought about the logic needed to find which rows to hide? I would think if you have 3 or 4 DGV's next to each other the logic needed to line the rows up would be a little intense to say the least.

From a programming pov it makes far more sense to use a single DGV and do the logic before binding the data to the DGV, preferably using a custom data type.
 
Ok, things are a lot clearer now. But why you are doing things this way still has me a little confused - are you trying to hack a quick demo together, or are you just starting out programming?

I was testing the water as to whether it would be easy enough to morph from my previously working method into using separate grids. I know the grid perhaps isn't the best tool for the situation anyway. So in a sense, a demo.

Have you thought about the logic needed to find which rows to hide? I would think if you have 3 or 4 DGV's next to each other the logic needed to line the rows up would be a little intense to say the least.

The logic to find which rows to hide already exists, I'd just be extending it under certain conditions.

From a programming pov it makes far more sense to use a single DGV and do the logic before binding the data to the DGV, preferably using a custom data type.

This does look like the best way it seems. That's fine. I suspected as much, just needed someone to make me see sense I suppose. I'll start making some bigger changes.
 
Thinking outside the box is always a good thing and should be encouraged in programming.

But a far more important skill, which you have demonstrated, is knowing when to cut your loses.

Good luck and I hope you get everything working as required.
 
Back
Top Bottom