rj tubera
rj tubera

Reputation: 757

DatagridView Search Winform - C#

I'm here again asking questions. I hope somebody would put some effort in answering this question.

So I have a datagrid view which had values from my database. I have a search button in my form which opens a new form and then there, there is a textbox which you can write the string that you want to search. The question is, How can do that to search and update my datagridview.

for ex. My datagrid view has the following values:

name: adam lewis, adam sandler, justin bieber, rebecca black

if i search only "adam" after I clicked ok and that 2nd form gets closed my datagrid view will update and will only show names which has adam on it.

name: adam lewis, adam sandler

**My Datagrid's values is bound by a datatable.

Just comment below if you don't understand the question and I will put pictures of my gui if that will help. THANK YOU VERY MUCH STACKOVERFLOW!!

it looks like this... enter image description here enter image description here

Here's a video example: http://www.youtube.com/watch?v=1OjZwBqVSVI

Upvotes: 2

Views: 3802

Answers (2)

gavin
gavin

Reputation: 1346

What part of it do you need help though? The general idea is this, you want to be able to control your main form from your search form. You can do this:

In your main form you do 2 things implement UpdateDatatable and add an event handler on search button click:

private void Search_Click(object sender, EventArgs e)
{
       SearchForm mySearchForm = new SearchForm();
       mySearchForm.SetMainForm(this);
       mySearchForm.Show();
}
public void UpdateDatatable(string searchWord)
{
//write your own code to update your datagridview by updating the datatable, filtering the datatable or creating a new datatable by using the parameter searchWord.  I am saying datatable because I assume your datagridview is bound to a datatable.
}

In your search form:

public partial class SearchForm : Form
{
    private Form mainForm;
    public SearchForm()
    {
        InitializeComponent();
    }
    public void SetMainForm(Form fromMainForm)
    {
        mainForm = fromMainForm;
    }

    private void txtSearchWord_TextChanged(object sender, EventArgs e)
    {
        mainForm.UpdateDatatable(txtSearchWord.text);
    }
}

Hope this helps

Upvotes: 3

Tathagat Verma
Tathagat Verma

Reputation: 558

Here is one way of doing this:

  • Make a public property on the parent form
  • This property should have the set-accessor as public
  • It should be the data-source for your data-grid
  • All operations to search and manage should be on this property only
  • For example:- if you are binding the grid with a data-table, then in the child form you can access this public property of the parent-form and filter it (maybe using LinQ or the DataTable Select & Filter methods)
  • By doing so the data-grid will update automatically; if the source is modified
  • But ensure to have a reset option or original-source available, as applicable

This is a crude way of doing things and not the optimal way, but it is a feasible one.

Hope this helps.

Upvotes: 0

Related Questions