Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How do i access BindingSource current row ?
 

How do i access BindingSource current row ?

How do i access BindingSource current row and change value of specific field ?

I have client table with 4 elements

client_guid
code
name
surname

My form is binded through BindingSource to all fields except client_guid

I want to create new client. So i enter values in all fields and hit save. But i get error that client_guid should be supplied.

I use BindingSource.AddNew() and then i use BindingSource.EndEdit() to save new clients- this works fine and new row is created, but it works only if i also bind client_guid to some textbox... sure it is not what i want. I wan to assign client_guid value in code not from UI textbox

I can't get it how do i access this client_guid field to set it's value. I would do it before calling EndEdit()

i thought i can access it like BindingSource.Current, but there are no methods for that. Then i thought maybe i should set value directly in DataSet but that does not work either - it says there are no rows at position 0.

This is code in short:

physicalPersonBindingSource.DataSource = CommonData.MainDataManager.DataSetCR.PhysicalPerson;

physicalPersonBindingSource.AddNew();

textBoxCode.DataBindings.Add("Text", physicalPersonBindingSource, "code", true, DataSourceUpdateMode.OnPropertyChanged, String.Empty);

textBoxName.DataBindings.Add("Text", physicalPersonBindingSource, "name", true, DataSourceUpdateMode.OnPropertyChanged, String.Empty);

textBoxSurname.DataBindings.Add("Text", physicalPersonBindingSource, "surname", true, DataSourceUpdateMode.OnPropertyChanged, String.Empty);

physicalPersonBindingSource.EndEdit();

ambidexterous  Sunday, July 30, 2006 6:30 PM

Ok, no need to answer i got it. I could do it also this way:

((DataSetCR.PhysicalPersonRow)physicalPersonBindingSource.Current).client_guid = client.ClientGuid

ambidexterous  Monday, July 31, 2006 8:34 PM
The datatable has an TableNewRow event. I would assign the client_guid a new guid there.
Ken Tucker  Sunday, July 30, 2006 9:50 PM

1. That would be a solution i think, but it does not answer my question... is it realy true, that i can't set this value without using event ?

2. How do i access this TableNewRow event ... is it possible through designer or, i have generate DataSetCR.cs class and do it there ?

I guess you meant something like this:

public MainDataManager()

{

_dataSetCR.PhysicalPerson.TableNewRow += new System.Data.DataTableNewRowEventHandler(PhysicalPerson_TableNewRow);

}

private void PhysicalPerson_TableNewRow(object sender, EventArgs e)

{

DataSetCR.PhysicalPersonRow physicalPersonRow = (DataSetCR.PhysicalPersonRow)sender;

DataSetCR.ClientRow clientRow =

(DataSetCR.ClientRow)(physicalPersonRow.GetParentRow("FK_PhysicalPerson_Client"));

physicalPersonRow.client_guid = clientRow.client_guid;

}

ambidexterous  Monday, July 31, 2006 7:07 PM

Ok, no need to answer i got it. I could do it also this way:

((DataSetCR.PhysicalPersonRow)physicalPersonBindingSource.Current).client_guid = client.ClientGuid

ambidexterous  Monday, July 31, 2006 8:34 PM

I tried the posted codepatternwithout success. The reason is: BindingSource.Current is of DataRowViewtype. But this one worked for me:

int id = Convert.ToInt32(((dtsMy.TblRow)((DataRowView)tblBindingSource.Current).Row).ID);

I hope it will help somebody! Wink

Alexander Yarushin  Monday, April 23, 2007 12:46 PM

You can use google to search for other answers

Custom Search

More Threads

• Database is changing when cell is editing
• Grid control - copy cell contents to clipboard without making read/write
• Scrolling datagridview 1px at a time from code
• GetSelectedRow from Datagrid
• Checked Items in DataGridViewCheckBoxColumn
• Custom Object in DataGrid
• Insert Statement fails , but works in MS access
• DataGridViewCell Painting Problem
• Dataset Precision and Scale for Decimal datatype
• Binding a Collection to a Listbox