Hi I am trying to insert a row in a databound grid at a specific index. I tried this in the following way:
private void HierarchyGrid_Load(object sender, EventArgs e)
{ BindingList<Pool> data = DummyDataBuilder.BuildDummyData(); TestGridMain.DataSource = data; TestGridMain.DataBind(); InsertSpecificRow(); } private void InsertSpecificRow() { var position =0; var row = TestGridMain.DisplayLayout.Bands[1].AddNew(); row.Cells["Name"].SetValue("Bla bla bla", false); row.Appearance.BackColor = Color.Pink; TestGridMain.Rows.Move(row, position); }
{
BindingList<Pool> data = DummyDataBuilder.BuildDummyData();
TestGridMain.DataSource = data;
TestGridMain.DataBind();
InsertSpecificRow();
}
private void InsertSpecificRow()
var position =0;
var row = TestGridMain.DisplayLayout.Bands[1].AddNew();
row.Cells["Name"].SetValue("Bla bla bla", false);
row.Appearance.BackColor = Color.Pink;
TestGridMain.Rows.Move(row, position);
Now whatever value I give position, the row is always at the bottom. I even added a button to the form invoking the Rows.Move on a specific row and I checked the row's index before and after the move instruction: no change.
Is there a setting in the grid that is preventing my rows from moving? Did I miss something elsewhere?
Thanks,
Teun
P.S. Sorry for ugly code: What is the correct way for mark up?
Hi,
I tried this out and it worked fine for me on the root level. Your code seems to be doing this on a child band, but I can't see why that would make any difference.
I am, of course, using the latest service release, so maybe this is a bug in the version you are using that has already been fixed.Try getting the latest service release and see if that helps.
How to get the latest service release - Infragistics Community
If not, perhaps you could post a small sample project demonstrating the issue so we can check it out.
Teun said:P.S. Sorry for ugly code: What is the correct way for mark up?
You can use a code tag.
I have the version from 17-09-2010 (NetAdvantage 2010 vol2). I am currently evaluating. Acceptance depends on me being able to get the UltraGrid to comply with certain unusual requirements.
I have quickly made a sample project which illustrates the problem: Check out the button at the bottom of the form, AFAIK it should move the new row up 2 rows every time it is pressed... on my computer it doesn't. I've tried several values for the target index.
I hope you can help me out.
About code tag: It looks worse now lol
There are several things wrong with your code here.
You are adding a row to Band 2. The row you have colored Pink is the current active row in Band 1, so the new row is being added under this row.
There are currently 0 rows under this row, so when you add a row, that brings the total number of rows to 1. But for some reason, your code is trying to move this row to index 5. This is, of course, impossible.
The second problem is that you are calling the Move method on the root-level rows collection of the grid. The row you are trying to move does not belong to that collection. So either this is a mistake or you are trying to move a row from one collection to another, which is also impossible.
Teun said:AFAIK it should move the new row up 2 rows every time it is pressed
This baffles me. Why would it do that? You are hard-coding an index of 5, so even if this worked, it would not move the row up 2 places, it would always move the row to index 5.
Oh, I see, now.. I missed the 'if' block there the first time.
Okay let's forget about the pink one (that was my first attempt I kinda forgot it was still there).
You indicate I am targeting bands incorrectly. To fix this I changed the following lines:
Line 99: TestGridMain.DisplayLayout.Bands[2].Layout.Rows.Move(row, 5);
Line 106: TestGridMain.DisplayLayout.Bands[2].Layout.Rows.Move(row, targetIndex);
This has had no apparent effect.
You also said that the index I am trying to move to and index that does not exist (please keep in mind I have tried a ton of different indexes just to get it to move anywhere). To illustrate my use of the sample please have a look at this:
Highlighted Alpha05 and pressed button1:
Then pressed button1 again:
Now I believe I am calling Move on the correct band, there are plenty of indexes to play around with. The textual feedback is accurate. The row just doesn't move. I also tried using the Band object obtained from the row instance itself to make sure I had the correct band, same effect.
Now it surely possible I am missing some obvious point, but I would appreciate if you could give me a code snippet that would make the row move... anywhere but the last position in any band that is not the top band.
P.S. I also tried messing around with Line 37 (Move Pink row) again, now calling the correct band: no difference.
Teun said:Now I believe I am calling Move on the correct band
Nope. You are still calling Move on the root-level rows collection.There is no such thing as a rows collection for a band - except for the root band where all of the rows are in a single collection. For any other band, there are multiple collections of rows - one collection for each parent row.
The code you have here is going on in circles.
TestGridMain.DisplayLayout.Bands[2] returns a Band object to you. The Layout property is a backward pointed to the grid's DipsplayLayout. So:
TestGridMain.DisplayLayout.Bands[2].Layout == TestGridMain.DisplayLayout
And, of course, the DisplayLayout returns the root-level rows collection. So:
TestGridMain.DisplayLayout.Bands[X].Layout.Rows == TestGridMain.Rows
Anyway, you are making this far too complicated. If you have a row and you want to move that row to a new position within the rows collection that the row belongs to, all you have to do is:
row.ParentCollection.Move
Ah I thought there was a RowCollection in each band. Thanks for setting me straight (explains a few other things I have been having problems with as well!)
I have changed my code accordingly and now everything works fine. Thank you very much.
Suggestion: Why not put the Move method (or SetIndex) on the GridRow class so a row can move itself?
Teun said:Suggestion: Why not put the Move method (or SetIndex) on the GridRow class so a row can move itself?
Why not? Hm.. no reason I can think of.
You should Submit a feature request to Infragistics