Re: DataView.Sort Dilemma - A bit long

From: runningdog (runningdog_at_reply.to.newsgroup)
Date: 08/25/04


Date: Wed, 25 Aug 2004 10:42:36 +0930

Kevin,

The problem here is not with a DataGrid.
I have a form with a number of TextBoxes bound to a DataSet and an find
method that selects the row to bind to.
This works fine until I programatically add a row to the dataset.
I have got around this problem by binding the Textboxes to a DataView then
adding the rows using the DataViews NewRow method.

The following example works

    Me.tbCompanyName.DataBindings.Add(New
System.Windows.Forms.Binding("Text", Me.DV, "CompanyName"))
    ...
    Private Sub btAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btAdd.Click
        Dim row As DataRow
        row = DV.Table.NewRow()
'DV is a dataview
        row.Item("CustomerId") = CustomerId.Text
        row.Item("CompanyName") = tbCompanyName.Text
        DV.Table.Rows.Add(row)
        Me.BindingContext(DV).EndCurrentEdit()
    End Sub

But the following does not

    Me.tbCompanyName.DataBindings.Add(New
System.Windows.Forms.Binding("Text", Me.DS, "Customers.CompanyName"))
    ...
    Private Sub btAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btAdd.Click
        Dim row As DataRow
        row = DS.tables("Customers").NewRow()
        row.Item("CustomerId") = CustomerId.Text
        row.Item("CompanyName") = tbCompanyName.Text
        DS.tables("Customers").Rows.Add(row)
        Me.BindingContext(DS, "Customers").EndCurrentEdit()
    End Sub

It will add the row and a Find will return the expected position but

    Me.BindingContext(DS, "Customers").Position =
DS.Tables("Customers").DefaultView.Find(key)

binds to the wrong row. It binds original row occupying the postion returned
by the find.
I am out of trouble with by using DataViews but would be interested to know
what I am doing wrong.

Thanks Steve

"Kevin Yu [MSFT]" <v-kevy@online.microsoft.com> wrote in message
news:j1dM1ObiEHA.2228@cpmsftngxa10.phx.gbl...
> Hi Steve,
>
> The following is my first reply to the issue. I have tried to change the
> DefaultView on my machine, however, this worked perfect.
>
> First of all, I would like to confirm my understanding of your issue. From
> your description, I understand that the positioning doesn't work properly
> after you add a row to the DataSet. If there is any misunderstanding,
> please feel free to let me know.
>
> I have written a test program according to your description. However, this
> one works fine on my machine whether there is row added or not. Could you
> please try this code on your machine? It gets data from Employees table in
> Northwind database.
>
> private void Form1_Load(object sender, System.EventArgs e)
> {
> SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Employees",
> this.sqlConnection1);
> this.ds = new DataSet();
> sda.Fill(ds, "Employees");
> ds.Tables["Employees"].DefaultView.Sort = "LastName";
>
> this.dataGrid1.DataSource = ds;
> this.dataGrid1.DataMember = "Employees";
> }
>
> private void button1_Click(object sender, System.EventArgs e)
> {
> int pos = this.ds.Tables[0].DefaultView.Find(this.textBox1.Text);
> BindingContext[ds,"Employees"].Position = pos;
> }
>
> If that still doens't work, could you please show me the code that you are
> binding to data source?
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>


Loading