user3189440
user3189440

Reputation: 11

how to show only my query result in datagridview c#

i am trying to get the result of my query into datagridview i managed to make add,update and delete but i have a little trouble in "find" query, i used querybuilder and wrote the query and execute it as well but with the same code that showing tables data in gridview i get nothing at all,i want the datagridview only show searched result i use sql express 2008

thanks in advance :)

this is my update query:

ownersTBLTableAdapter1.UpdateQuery(name_txtbox.Text,phone_txtbox.Text);
this.ownersTBLTableAdapter1.Fill(this.rtmS_DS1.OwnersTBL);

and this is a the useless code:

 private void search_tlstb_txtbox_TextChanged(object sender, EventArgs e)
        {
           ownersTBLTableAdapter1.FindQuery(search_tlstb_txtbox.Text);
           this.ownersTBLTableAdapter1.Fill(this.rtmS_DS1.OwnersTBL);
        }

Upvotes: 1

Views: 965

Answers (2)

user3189440
user3189440

Reputation: 11

finally it worked

 private void search_tlstb_txtbox_TextChanged(object sender, EventArgs e)
    {
        if (search_tlstb_txtbox.Text != string.Empty && owners_dgv.RowCount > 0)
        {
            for (int i = 0; i < owners_dgv.Rows.Count - 1; i++)
            {
                for (int j = 0; j < owners_dgv.Rows[i].Cells.Count; j++)
                {
                    if (owners_dgv.Rows[i].Cells[j].Value.ToString().Contains(search_tlstb_txtbox.Text))
                    {
                        owners_dgv.Rows[i].Visible = true;
                        break;
                    }
                    else
                    {
                        owners_dgv.CurrentCell = null;
                        owners_dgv.Rows[i].Visible = false;
                    }
                }
            }
        }
        else
            this.OwnersTBLTableAdapter1.Fill(this.rtmS_DS1.OwnersTBL);
    }

Upvotes: 0

JohnyHarkness
JohnyHarkness

Reputation: 88

First off I assume you are using a DataSet

So you could use something like

ownersTBLTableAdapter1.Fill(rtmS_DS1.OwnersTBL);
dataGridView1.DataSource = rtmS_DS1.OwnersTBL;

In your text changed event then you could apply a filter to the defaultview

so if the DataSet type is DS1 then it would be

(dataGridView1.DataSource as DS1.OwnersTBL).DefaultView.RowFilter = string.Format("Field = '{0}'", search_tlstb_txtbox.Text);

and "Field" should be replaced with your column name

after reading again replacing the string.Format("Field = '{0}'", search_tlstb_txtbox.Text)

with string.Format("Field LIKE '%{0}%'", search_tlstb_txtbox.Text)

Will probably get you what your after though

Any questions let me know

Upvotes: 1

Related Questions