Skip to content

Set data binding position through item selected in Data Grid View

Ranhiru Cooray

Asked May 13, 2009, 4:31 AM EDT1 answer
Share
LinkedInX

I have a form with data bindings.   and i use a BindingManagerBase to travel through the records

           

Java
SqlConnection con = Global.Con;
            con.Open();
            da = new SqlDataAdapter("SELECT * FROM Customer", con);
            mcb = new SqlCommandBuilder(da);

            da.Fill(ds, "Customer");

            txtCustomerFName.DataBindings.Add(new Binding("Text", ds.Tables["Customer"], "Cus_FName"));
            txtCustomerLName.DataBindings.Add(new Binding("Text", ds.Tables["Customer"], "Cus_LName"));
            txtCustomerAddress.DataBindings.Add(new Binding("Text", ds.Tables["Customer"], "Cus_Address"));
            txtCustomerEmail.DataBindings.Add(new Binding("Text", ds.Tables["Customer"], "Cus_Email"));
            txtCustomerRegDate.DataBindings.Add(new Binding("Value", ds.Tables["Customer"], "Reg_Date"));
            txtCustomerLoyalty.DataBindings.Add(new Binding("Text", ds.Tables["Customer"], "Loyalty"));
    
           bm = this.BindingContext[ds.Tables["Customer"]];
And then i use bm.Position +=1 and bm.Position -=1 to travel through it.

TypeScript
private void cmdBack_Click(object sender, EventArgs e)
        {
            

            if (bm.Position == 0)
                MessageBox.Show("Reached beginning of list!", "Cannot go further!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                bm.Position -= 1;
            
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            
           
            if (bm.Position == (bm.Count - 1))
                MessageBox.Show("Reached end of list!", "Cannot go further!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            bm.Position += 1;
        }
Anyway  i use a DataView and DataView.RowFilter to search and display the results in the DataGridView which is in a seperate form. And once the search results are returned i want to set the current position in the before form to whatever row is selected in the Data Grid View.

C#
private void CustomerSearch_Load(object sender, EventArgs e)
        {

            SqlConnection con = Global.Con;
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customer", con);
            da.Fill(ds, "Customer");
            dv.Table = ds.Tables[0];


            DGV1.DataSource = dv;



            
            
            


        }

        private void txtSearchString_TextChanged(object sender, EventArgs e)
        {

            dv.RowFilter = "Cus_FName LIKE '*" + txtSearchString.Text + "*' OR Cus_LName LIKE '*" + txtSearchString.Text+ "*' OR Cus_Address LIKE '*" + txtSearchString.Text + "*' OR Cus_Email LIKE '*" + txtSearchString.Text + "*'";



        }
Now if the user selects a select a row returned from the searching, i want set the bm.Position (which is in another form and if you want i can take the searching the same form as well) to whatever the row selected on the DataGridView.

I hope you get the problem. Please ask me if this is not clear.  How can i do that?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 1 short reply