Problem with Form.BindContext().Position

M

Mauricio

I have a form with a DataGrid with AllowSorting property
sets true.
I am usually using the form.DataBinding
(DataSet,DataMember).Position as the index when I need get
the match data from the DataSet, but when I click on the
header column to sort the rows, the Position porperty is
changed to a value different from index.
The question is, how can I get the corret row in dataset
using the Form.BindingContext's properties with the
DataGrid's AllowSorting set True?
Best regards,
Mauricio (Brazil)
 
Y

Ying-Shen Yu[MSFT]

Hi Mauricio,

I found a similiar issue and another Support Engineer from Microsoft has
given an solution to it.
I past the solution here, please be free to reply to this thread if you
feel it is not helpful to the problem.

For full thread, you may find it at this link:
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&threadm=8#e
GfszPCHA.1900%40cpmsftngxa07&rnum=1&prev=/groups%3Fhl%3Dzh-CN%26lr%3D%26ie%3
DUTF-8%26oe%3DUTF-8%26q%3DMSFT%2Bsort%2Bdatagrid

<cite>
Alastair,

If your datagrid is bound to a DataTable then it will be bound to the
Default DataView. Clicking on a column header will change the sort order
for that dataview. Reading a row index from that datagrid will get you the
index of the row as it exist in the sorted dataview, hence, it will not
match the index of the same row in the unsorted atatable.

SO if you create an explicit dataview object from the datatable and then
bind the datagrid to it, you should have no problem. Here is sample code
that binds dataview to a datagrid:

da.Fill(ds, "customers")
dv = ds.Tables("customers").DefaultView
dv.Sort = "CustomerID"
DataGrid1.DataSource = dv
cm = CType(Me.BindingContext(dv), CurrencyManager)
SortColumnName = dv.Sort

now in the mousedown event, need to know the column name if the use clicks
on a column header then you do:

Dim myGrid As DataGrid = CType(sender, DataGrid)
Dim hti As DataGrid.HitTestInfo
hti = myGrid.HitTest(e.X, e.Y)
Select Case hti.Type
Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
SortColumnName = dv.Table.Columns(hti.Column).ColumnName
..

and then in the search button, set the sort order of the dataview then
search the sorted dataview:

Dim i As Integer
dv.Sort = SortColumnName
i = dv.Find(TextBox1.Text)
DataGrid1.Select(i)
cm.Position = i


I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support
</cite>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!
 
M

Mauricio

Hi Ying-Shen Yu [MSFT],
This solution didn't resolve, because I have other tables
related and this solution treat only one table.
My application has a main form with 4 datagrids. Each one
is bound with the same dataset, but different related
tables. I select a row of anyone datagrids and open a
second form to edit the selec row. This second form has
some controls (textboxes and comboboxes) bound with the
same main form's dataset. In the second form load event
it's initiated its BindContext().Position with the same
values of the main form. If I don't sort the main fom's
datagrids, it's ok, otherwise the second form shows a
different row.
I don't know what to do. For the time being I disable the
datagrid sort property, but I hope that you can help me.

Best Regards,
Mauricio
-----Mensagem original-----
Hi Mauricio,

I found a similiar issue and another Support Engineer from Microsoft has
given an solution to it.
I past the solution here, please be free to reply to this thread if you
feel it is not helpful to the problem.

For full thread, you may find it at this link:
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF- 8&oe=UTF-8&threadm=8%23e
GfszPCHA.1900%40cpmsftngxa07&rnum=1&prev=/groups%3Fhl%
3Dzh-CN%26lr%3D%26ie%3
DUTF-8%26oe%3DUTF-8%26q%3DMSFT%2Bsort%2Bdatagrid

<cite>
Alastair,

If your datagrid is bound to a DataTable then it will be bound to the
Default DataView. Clicking on a column header will change the sort order
for that dataview. Reading a row index from that datagrid will get you the
index of the row as it exist in the sorted dataview, hence, it will not
match the index of the same row in the unsorted atatable.

SO if you create an explicit dataview object from the datatable and then
bind the datagrid to it, you should have no problem. Here is sample code
that binds dataview to a datagrid:

da.Fill(ds, "customers")
dv = ds.Tables("customers").DefaultView
dv.Sort = "CustomerID"
DataGrid1.DataSource = dv
cm = CType(Me.BindingContext(dv), CurrencyManager)
SortColumnName = dv.Sort

now in the mousedown event, need to know the column name if the use clicks
on a column header then you do:

Dim myGrid As DataGrid = CType(sender, DataGrid)
Dim hti As DataGrid.HitTestInfo
hti = myGrid.HitTest(e.X, e.Y)
Select Case hti.Type
Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
SortColumnName = dv.Table.Columns (hti.Column).ColumnName
..

and then in the search button, set the sort order of the dataview then
search the sorted dataview:

Dim i As Integer
dv.Sort = SortColumnName
i = dv.Find(TextBox1.Text)
DataGrid1.Select(i)
cm.Position = i


I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support
</cite>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!


.
 
Y

Ying-Shen Yu[MSFT]

Hi Mauricio,

Thanks for your clarification!

You now want to sync the binding context between the two forms. You may
pass the BindingContext of your main form as well as the dataset into your
second form. probably this will solve your problem.
Here is the code snippet to show the idea:
<code>
public Form2(BindingContext bc, DataSet1 ds)
{
InitializeComponent();
//BindingContext[ds,"Categories"].Position = bc[ds,"Categories"].Position;
BindingContext = bc;
textBox1.DataBindings.Add("Text",ds,"Categories.CategoryID");
textBox2.DataBindings.Add("Text",ds,"Categories.CategoryName");
textBox3.DataBindings.Add("Text",ds,"Categories.Description");
}
</code>

Does this solve your problem?
If you still have problem on this issue, please be free to reply me in this
thread.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
M

Mauricio

Hi Ying-Shen Yu,

Thanks for your help!

Now it's ok.

Best Regards,

Mauricio (Brazil)
-----Mensagem original-----
Hi Mauricio,

Thanks for your clarification!

You now want to sync the binding context between the two forms. You may
pass the BindingContext of your main form as well as the dataset into your
second form. probably this will solve your problem.
Here is the code snippet to show the idea:
<code>
public Form2(BindingContext bc, DataSet1 ds)
{
InitializeComponent();
//BindingContext[ds,"Categories"].Position = bc [ds,"Categories"].Position;
BindingContext = bc;
textBox1.DataBindings.Add ("Text",ds,"Categories.CategoryID");
textBox2.DataBindings.Add
("Text",ds,"Categories.CategoryName");
textBox3.DataBindings.Add
("Text",ds,"Categories.Description");
}
</code>

Does this solve your problem?
If you still have problem on this issue, please be free to reply me in this
thread.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top