hi every 1. i am trying to make a search form with 1 label(surname) and textbox. i have 1 gridview on the form aswell. can you plz provide me any code so that when i type in textbox i should c the result in the gridview. wat i have done right now is. i have added data source and i than dragged the dataset.but when i load the form i already can see the data loaded on grid view .it than filters depending on the textbox. i want this gridview to be empty when it gets loaded. plz provide any idea or code.thnx

Recommended Answers

All 4 Replies

You mean you want to create a filter over data depending on just one column?

Like for example you have coustomers in gridview with columns: id, name, lastname, arress, born,...
... and now you would like to create a filter my name (for example) to show only customers with this name that`s in textBox?
Am I right?

i have already created this filter using sql query. but the problem is i done this search using short cut.i added a dataset using data sourse and i than dragged the dataset on the form. wat i wana do is without dropping dataset,i wana bind the text box with gridview. so i should only drop a grid view on the form. and rest of the things i should do by coding. so suppose i type in last name.than result should be displayed in the grid view. wat happens right now is because of gridview binding when the form is loaded i can already see my gridview filled with rows.i dont wana do that.

You cannot bind textBox to girdview, what you can do, is to do the filter over dataSet (or dataTable).
Use Select() method to create the filter string, which will fill DataRow array.
Example:

//you have one comon dataSet to fill gridview
DataSet ds; //on the class level

void DoTheFiltering()
{
    DataTable table = ds.Tables[0]; //selecting 1st DataTable from dataSet (if there is a dataSet
    stirng myFilter = "ColumnName = '" + textBox1.Text + "'"; //ColumnName is my example, change it to your column you wanna do filtering over
    DataRow[] foundRows = table.Select(myFilter);
    //fill dataTable from got rows (rows array):
    foreach (DataRow row in foundRows)
        table.ImportRow(row);
    //bind new filtered data with girdview:
    dgv.DataSource = new BinsingSource(table, null);
}

This is about it.
Hope it helps.

hello !
try this code , you have to use this code at text change event of your textbox .

Try
            Dim MyCon As New SqlConnection("connectionstring")
            Dim MyDataSet As New DataSet
            MyCon.Open()
            Dim Myquery As String
            Myquery = "select vendorid , vendorname from vendors"
            If TextBox1.Text <> "" Then
                Myquery = Myquery & " where vendorname like '" & TextBox1.Text & "%'"
            End If
            Dim da As New SqlDataAdapter(Myquery, MyCon)
            MyDataSet.Tables("Vendors").Clear()
            da.Fill(MyDataSet, "Vendors")
            DataGridView1.DataSource = MyDataSet.Tables("Vendors")
            MyCon.Close()

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try

Regards :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.