Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Data binding and search

I have what probably is a trivial question, but I haven't
found a definitive answer yet.

Say I have a table of customers with ID, last name, and
first name, and three text boxes bound to the dataset
based on that table. In addition to the navigation buttons,
I want a Search button that will ask the user to enter a part
of the last name and will then limit the dataset to only
those customers whose name matches.

If I had a datagrid, the answer would be simple: create a new DataView, set its RowFilter, and re-bind the grid
to that view. The question is, what's the right way to
do this with textboxes? Obviously, I could modify the
adapter's Select statement, re-fill the dataset, and re-bind
the controls, but that seems so clumsy...

Any advice would be appreciated.

Mike
Friday, March 14, 2003

ASP.NET or WinForms?

Duncan Smart
Friday, March 14, 2003

Sorry, didn't realize it wasn't clear. Win forms.

Since I posted the message, I've figured out a possible
solution - not sure it's the most elegant and/or preferred:

If I bind the controls to the default view of the dataset's
table, then setting the rowfilter of that view will filter
out the displayed data. Am I on the right track ?

Mike
Friday, March 14, 2003

Yes - filter on the DataView. How much data are you pulling from the db? If it's lots then get the db to do the filtering - otherwise you're on the right track.

Duncan Smart
Saturday, March 15, 2003

Thanks a lot!

Mike
Saturday, March 15, 2003

Is there an easy way to do this with a listview?

I've got an application that works fine with the datagrid, but I want to have a listview with checkboxes. When I say application, its more a learning toy really.

I haven't seen an obvious way of binding the .Net listitems to rows. Would iterating through the dataset be a bad thing to do? I haven't seen any obvious .MoveNext etc methods.

In VB6 I would do this with a Do While Not eof Loop, adding a listitem on every iteration.

Justin
Monday, March 17, 2003

There isn't any equivalent to .MoveNext in a DataSet, because the DataSet doesn't have a current record pointer. Instead, you use For Each loops:

    ' Create a SqlConnection
    Dim cnn As SqlConnection = _
    New SqlConnection("Data Source=(local);" & _
    "Initial Catalog=Northwind;Integrated Security=SSPI")
    ' Create a SqlCommand
    Dim cmd As SqlCommand = cnn.CreateCommand()
    cmd.CommandType = CommandType.Text
    cmd.CommandText = _
    "SELECT * FROM Customers WHERE Country = 'France'"
    ' Set up the DataAdapter and fill the DataSet
    Dim da As SqlDataAdapter = New SqlDataAdapter()
    da.SelectCommand = cmd
    Dim ds As DataSet = New DataSet()
    da.Fill(ds, "Customers")
    ' Dump the contents of the DataSet
    Dim dt As DataTable
    Dim dr As DataRow
    Dim dc As DataColumn
    lbData.Items.Add("DataSet: " & ds.DataSetName)
    For Each dt In ds.Tables
        lbData.Items.Add("  DataTable: " & dt.TableName)
        For Each dr In dt.Rows
            lbData.Items.Add("    DataRow")
            For Each dc In dt.Columns
                lbData.Items.Add("      " & dr(dc))
            Next
        Next
    Next

Mike Gunderloy
Monday, March 17, 2003

Bind a CheckedListBox? Sure:

string sql = "select * from titles";
SqlDataAdapter adapter = new SqlDataAdapter(sql, "Server=.;Database=pubs;Integrated Security=sspi;");
DataTable table = new DataTable("Titles");
adapter.Fill(table);

myCheckedListBox.DataSource = table;
myCheckedListBox.DisplayMember = "title";
myCheckedListBox.ValueMember = "title_id";

Duncan Smart
Tuesday, March 18, 2003

*  Recent Topics

*  Fog Creek Home