Re: How to sort a DataGrid bound to a DataRelation?

From: Josh (joshualangemann_at_gmail.com)
Date: 12/09/04


Date: 8 Dec 2004 20:56:56 -0800

I just had a similar problem, where I was attempting to sort the
contents of a combobox that was bound to a datarelation. Eventually I
found that I could indeed retrieve the DataView from the currency
manager and set the sort on it. However, it appears that the DataView
is re-created whenever the position of the parent bindingmanager
changes, requiring the sort to be re-done repeatedly. Here is what I
ended up implementing successfully. Note that Payment is my parent
table, and PaymentAudit is the child table. PaymentAudit records need
to be appear reverse sorted in the combobox:

// In the load event of the form, bind to the CurrentChanged and
PositionChanged events of the parent datatable, Payment:
this.BindingContext[this.paymentListData, "Payment"].CurrentChanged +=
new EventHandler(PaymentListTabs_PaymentChanged);
this.BindingContext[this.paymentListData, "Payment"].PositionChanged +=
new EventHandler(PaymentListTabs_PaymentChanged);

// Event handler that will run with the Payment Position or Current
item changes
private void PaymentListTabs_PaymentChanged(object sender, EventArgs e)
{
// Sort the Payment Audit records if there are any
if (this.BindingContext[this.paymentListData,
"Payment.PaymentPaymentAudit"].Count > 0)
((DataRowView)this.BindingContext[this.paymentListData,
"Payment.PaymentPaymentAudit"].Current).DataView.Sort = "PaymentAuditId
DESC";
}

Hopefully you can interpret the code despite the text wrapping!

Thanks,

Josh

Kevin P. Fleming wrote:
> I've got a strongly typed DataSet containing two tables with a
> DataRelation defining the parent-child relationship between them. On
my
> form, there is a DropDownList ComboBox bound to the parent table,
> allowing the user to select a row from that table.
>
> Also I have a DataGrid bound to the DataRelation between the parent
and
> child tables (DataSource is set to the DataSet, DataMember is set to
the
> relation name). This works fine, as the user can use the ComboBox to
> select a parent table row and the DataGrid properly displays the
related
> rows from the child table.
>
> However, I cannot seem to find a way get the DataGrid to
automatically
> sort the child rows by their Name column. I can't use a DataView
because
> DataViews can't be bound to DataRelations. I have set the
> DefaultView.Sort property of the child table, but that has no effect,

> presumably because the DataGrid's internal DataView is not being
built
> directly from the child table, but from the DataRelation. I _can_
> manually sort the rows in the grid, of course, but this is not very
> elegant :-)
>
> I even tried using the CurrencyManager returned from getting the
> DataGrid's BindingContext, but the .List property for that
> CurrencyManager is not a DataView, it's a DataViewManager, which
doesn't
> have a .Sort property.
>
> Anyone have any ideas?



Relevant Pages

  • Re: How to sort a DataGrid bound to a DataRelation?
    ... I just had a similar problem, where I was attempting to sort the ... contents of a combobox that was bound to a datarelation. ... Note that Payment is my parent ... > DataRelation defining the parent-child relationship between them. ...
    (microsoft.public.dotnet.framework.windowsforms.databinding)
  • Re: Langhams disgrace
    ... Sometimes brave, sometimes mentally unbalanced, sometimes vengeful, sometimes manipulated by a parent who is indignant about an older person having sex with her child. ... sort of stuff up - it would be the rare exception when people do that. ...
    (uk.legal)
  • Re: ConversationTopic question
    ... If you want to sort messages that have the same parent in the conversation ... OutlookSpy - Outlook, CDO ... Dmitry,I always thought it wud be a tree.. ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: Sort order via excel userform combobox
    ... It sounds like you have one column for each combobox. ... Is there a reason why you ... just can't sort just column A in ascending order, then sort just column B, then ... that Column C is now sorted in ascending order. ...
    (microsoft.public.excel.programming)
  • Re: you will be a god if you know how....
    ... C and Data | Sort - select column C as the sort key and click on ... we have a register of data which use a parent / child relationship. ...
    (microsoft.public.excel.misc)