HighTechTalks DotNet Forums  

Get object which DataGridView row is bound to

Dotnet Framework (WinForms DataBinding) microsoft.public.dotnet.framework.windowsforms.databinding


Discuss Get object which DataGridView row is bound to in the Dotnet Framework (WinForms DataBinding) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
moondaddy
 
Posts: n/a

Default Get object which DataGridView row is bound to - 04-24-2006 , 01:44 PM






VS 2005 winforms project: I have a DataGridView bound to a list of business
objects. On it's RowEnter event I want to get a reference to the business
object that row of the DataGridView is bound to. How can this be done?



// Persons_BLL obj

// obj is an instance of Persons_BLL

// Here's how Persons_BLL is defined:

// public class Persons_BLL : BindingList<Person_BLL>, IBindingList



this.bindingSource1.DataSource = obj;

this.dgvList.DataSource = this.bindingSource1;



Here's my RowEnter event. Now how can I get a reference to the business
object of Person_BLL for that row 'e'?



private void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)

{

try

{



}

catch (Exception ex)

{

ExceptionHandler.ErrLog(ex);

}

}



Thanks.


--
moondaddy (AT) noemail (DOT) noemail



Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default RE: Get object which DataGridView row is bound to - 04-24-2006 , 10:16 PM






Hi,

Thank you for posting. From your post, my understanding on this issue is:
how to get a reference to the business object bound to the current row in a
DataGridView's RowEnter event. If I'm off base, please feel free to let me
know.

When you bind a business object list to a DataGridView, each row in the
DataGridView is bound to a business object in the list. If you want to get
the reference of the object bound to row i, you could use the following
sentence:
object singleobject = dataGridView1.Rows[i].DataBoundItem;
If you know the type of the singleobject, you could make a type conversion
in the upper sentence.

For example, you can use the following sentences in your DataGridView's
RowEnter event handling method to get the reference of the business object
bound to the current row.
private void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
// get the reference of the business object bound to the current
row, and convert it to Person_BLL type
Person_BLL personInstance =
(Person_BLL)this.dataGridView1.Rows[e.RowIndex].DataBoundItem;
// add your process on the personInstance here
}

catch (Exception ex)
{
ExceptionHandler.ErrLog(ex);
}

}

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to tell me.

Sincerely,
Linda Liu
Microsoft Online Community Support

================================================== ==
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
================================================== ==


Reply With Quote
  #3  
Old   
Daniel Schoepf
 
Posts: n/a

Default Re: Get object which DataGridView row is bound to - 04-27-2006 , 03:06 AM



Hello,

well this approach seems very good to me.

I experience the same problem currently but i have doubts about this method. What happens if you change your sorting during view? I guess that the row index doesnt match the object index anymore. So what would you recommend then?

thanks
---
Posted via www.DotNetSlackers.com

Reply With Quote
  #4  
Old   
Daniel Schoepf
 
Posts: n/a

Default Re: Get object which DataGridView row is bound to - 04-27-2006 , 05:41 PM



hmm .... seems as if it is the best to use the row-index since sorting needs the iBinding interface and refreshes your datagrid ...

greets
Daniel Schöpf
---
Posted via www.DotNetSlackers.com

Reply With Quote
  #5  
Old   
AT
 
Posts: n/a

Default Re: Get object which DataGridView row is bound to - 04-28-2006 , 05:09 AM



Hi Daniel,

If you sort the rows in your DataGridView, you still can get the corrent
object bound to a row using the sentence: object singleobject =
dataGridView1.Rows[i].DataBoundItem.


Sincerely,
Linda Liu
Microsoft Online Community Support

================================================== ==
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
================================================== ==


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.