Access the bound object of a row

TeeGrid .NET for Microsoft Visual Studio .NET.
Post Reply
Curt K.
Newbie
Newbie
Posts: 6
Joined: Wed Jan 22, 2020 12:00 am

Access the bound object of a row

Post by Curt K. » Tue Jan 28, 2020 4:21 pm

Apologies if what I'm about to talk about available and I just haven't found it yet.

I have a "RemakeProgress" object, and I'm binding it to the grid's data using the method below.


GridMain.Data = new VirtualListData<RemakeProgress>(remakeClasses);

I know the grid supports binding to all types of things, not necessarily full objects, but, it would be nice if the selection events would give easy to use event args. Like e.AData.Data = object instead of e.AData.Data string.

A simple work around would be to use this field/property: e.AData.Row

Is there a method somewhere to do something like this somewhere?

Object myObj = MainGrid.Rows[e.AData.Row].BoundObject;
RemakeProgress rp = (RemakeProgress)myObj;

The http://www.teechart.net/docs/TeeGridNETReference.htm would be MUCH better if it was searchable.

thanks,
Curt

Christopher
Guru
Posts: 1218
Joined: Fri Nov 15, 2002 12:00 am

Re: Access the bound object of a row

Post by Christopher » Thu Jan 30, 2020 4:14 pm

Hello Curt,

would Column and Row from a Selection event be sufficient? If so, the ButtonColumn example on Steema's GitHub has an example of how to obtain this, e.g.

Code: Select all

            tGrid1.Data = new VirtualArrayData<MyButtonData>(myData);

            tGrid1.Selected.Changed += Selected_Changed;

            ChangeRender(tGrid1.Columns[0]);
        }

        private void Selected_Changed(object sender, EventArgs e)
        {

            var selection = sender as GridSelection;

            if(selection.Column == tGrid1.Columns[0] && selection.Row != 5)
            {
                MessageBox.Show($"You've clicked a button on row {selection.Row}");
            }
        }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Curt K.
Newbie
Newbie
Posts: 6
Joined: Wed Jan 22, 2020 12:00 am

Re: Access the bound object of a row

Post by Curt K. » Thu Jan 30, 2020 4:33 pm

Hi Christopher,

Knowing the grid row/column isn't helpful if I can't determine what that row is bound to. Getting access to the object the row is bound to lets the programmer decide what they want to do next (no need to use row column indexes attempting to figure out how to get to what the user selected). Getting the value/contents of a row/column (cell) is more helpful, but assumes the value in the row/cell can identify the underlying object so that some type of "work" can be done.

One very useful item in the .net's data grid is the Row.BoundObject. If I can get access to the grids row object, and then the row's bound object, then I can do what ever I please. The grid can sort any way it likes, and my underlying List<> is irrelevant (indexes into the list doesn't matter). If the SelectionChanged handed the selection of row(s), I could then grab my bound objects and do what I need with them. I don't have to attempt to look them up with row indexes. In other words, I'm not finding the connection from the grid display, to the user clicking on something in the grid, and getting back to the object(s) the grid is bound to (not just a value of a cell or property) .

Hope that makes sense.
Curt

Christopher
Guru
Posts: 1218
Joined: Fri Nov 15, 2002 12:00 am

Re: Access the bound object of a row

Post by Christopher » Thu Jan 30, 2020 4:38 pm

Hello Curt,

I think I have a clearer idea of what you're after now, thank you. I'm afraid it's getting a little late now in my timezone, so if it's okay with you I'll try to get a clear answer back to you first thing tomorrow morning.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Christopher
Guru
Posts: 1218
Joined: Fri Nov 15, 2002 12:00 am

Re: Access the bound object of a row

Post by Christopher » Fri Jan 31, 2020 9:35 am

Good morning Curt,

I've uploaded an example we can use in this conversation to one of our servers, and you can find it in *.zip file format here. This example is a modified version of one of the examples on GitHub, and puts Visual Studio's DataGridView side-by-side with TeeGrid.

The section relevant to this thread is here:

Code: Select all

		private void DataGridView1_SelectionChanged(object sender, EventArgs e)
		{
			var rows = dataGridView1.CurrentRow.DataBoundItem as Car;
			MessageBox.Show($"DataGridView: This is {rows.Manufacturer}");
		}

		private void Selected_Changed(object sender, EventArgs e)
		{
			var selection = sender as GridSelection;
			MessageBox.Show($"TeeGrid: This is {myCars[selection.Row].Manufacturer}");
		}
As you say, the DataGridView gives you access to the underlying data structures while TeeGrid does not, as things stand. This means, as per this example, accessing the data structures which were passed to TeeGrid (and DataGridView ) intially. I think this may seem counterintiutive, that in this case TeeGrid does not give access directly to the underlying data, and I also think that access to the original data is guaranteed by the fact it has to be passed to the two grid objects.

What do you think?
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Curt K.
Newbie
Newbie
Posts: 6
Joined: Wed Jan 22, 2020 12:00 am

Re: Access the bound object of a row

Post by Curt K. » Fri Jan 31, 2020 9:20 pm

Christopher,

Does the selection.Row always work into the original List<Cars> even if the TeeGrid is sorted when someone clicks on any column? In other words, is the List<Cars> resorted so that the Row int always points to the correct underlying object in the original bound to List<Cars> (I'm using List<T> obviously).

I'm juggling 4 different projects at the moment, and only one uses this grid, and I'm not in a position where I can easily try this out, at least today anyway.

The other things is that the columns (header text) can't be created a head of time, and in my case, every time I do a refresh I bind to my list, and I have to make sure I refresh the column headers. That's obviously different too.

Overriding the column paint method is also a pain to use, and I have to continue to use the DataGrid that remain in other projects, so that means I have to know both "styles" of programming using grids (which I'm not going to do). My hope was that the TeeGrid took the pain points of the DataGrid and opened it up more so that the programmer could jump in and override functionality, add new functionality like more rich embedded column editors (fix the issues with the DataGrid's embedded controls, like focusing, and clicking), due to having access to source (recent option), but the two are so different, it would be a huge undertaking.

My mistake, I didn't take the time to really dig deep into the available demo projects before I purchased.

Unfortunately, when I have a chance, I'm going to back out the use of the TeeGrid and put the DataGrid back in. Where the TeeGrid probably really shines (lots of data), I'll never use.

Thanks,
Curt

Post Reply