Jerry Welliver
Jerry Welliver

Reputation: 377

Show specific row on a bound form

I have a form that is bound directly with a table adapter created with the designer. I also created a list box with the key value from each record. My thought is to allow the user to click on a specific key to load the desired record in the edit screen. I can access the list entry on click, but I don't know what command to use to move to the correct row.

I am wondering if it is really worth using the bound forms like this or just do it in code, similar to how I created the list box. Any suggestions? See my code below:

public partial class Customer : Form
{
    public string dbConString = "Data Source=localhost\\BALLMILL;Initial Catalog=Ballmill;Integrated Security=True";
    public SqlConnection dbCon = null;
    public SqlDataReader dbRdr = null;
    public SqlCommand dbCommand = null;

    public Customer()
    {
        InitializeComponent();
    }
    private void cUSTOMERBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.cUSTOMERBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.bML_WMS245GDataSet);
    }

    private void Customer_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'bML_WMS245GDataSet.CUSTOMER' table. You can move, or remove it, as needed.
        this.cUSTOMERTableAdapter.Fill(this.bML_WMS245GDataSet.CUSTOMER);

        SqlConnection dbCon = new SqlConnection(dbConString);
        SqlDataReader rdrCustomers = null;
        try
        {
            dbCon.Open();
            SqlCommand sqlCustomers = new SqlCommand("SELECT CustomerCode FROM Customer", dbCon);
            rdrCustomers = sqlCustomers.ExecuteReader();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Error accessing database");
            return;
        }

        while (rdrCustomers.Read())
        {
            listCustomers.Items.Add(rdrCustomers["CustomerCode"].ToString());
        }
    }

    private void listCustomers_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show(listCustomers.SelectedItem.ToString(), listCustomers.SelectedIndex.ToString());
    }
}    

Upvotes: 0

Views: 85

Answers (1)

Jerry Welliver
Jerry Welliver

Reputation: 377

I found the answer after searching through the designer code.

cUSTOMERBindingSource.Position = listCustomers.SelectedIndex;

This automatically moves the navigator to the selected row.

Upvotes: 0

Related Questions