0

I Have a c# windows form that can save ID, Name,andBirthday into my Employee Table. I use these codes to save data in my table Using Save Button:

private void Save_Click_1(object sender, EventArgs e)
    {
        try
        {
            MainDatabaseDataSetTableAdapters.EmployeeTableAdapter employee = new MainDatabaseDataSetTableAdapters.EmployeeTableAdapter();
            employee.InsertQuery(textBox1.Text.Trim(),
                textBox2.Text.Trim(),
                textBox3.Text.Trim());


            MessageBox.Show("Saved");

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

with this Query:

INSERT INTO Employee (ID, Name, Birthday) VALUES (@ID,@Name,@Birthday)

I Use Populate Button to show my saved data from my Database into Listview using this code:

private void Populate_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

        for (int i = 0; i < dtable.Rows.Count; i++)
        {
            DataRow drow = dtable.Rows[i];

            if (drow.RowState != DataRowState.Deleted)
            {
                ListViewItem item = new ListViewItem(drow["ID"].ToString());
                item.SubItems.Add(drow["Name"].ToString());
                item.SubItems.Add(drow["Birthday"].ToString());

                listView1.Items.Add(item);
            }
        }

So my Problem now is that I don't have any Idea on how to put a search in my listview using a textbox and SearchButton.

I have a SearchQuery:

SELECT ID, Name, Birthday FROM Employee WHERE
(Name = @Name)

and I thought I can insert this in my SearchButton but I don't know how.

Please somebody Help.

Before I used this code to Populate my listview

 DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

I used this codes to Search and it worked. I set my DB Connection Using this Code:

 public SqlCeConnection connection = new SqlCeConnection(@"Data Source = C:\...\...\...");

Then I inserted this codes in my SearchButton:

 private void button2_Click(object sender, EventArgs e)
    {


        SqlCeCommand search = new SqlCeCommand("SELECT * FROM Employee Where Name like '%" + Search.Text + "%'", connection);


        try
        {
            SqlDataReader datareader = search.ExecuteReader();


            while (datareader.Read())
            {
                ListViewItem item = new ListViewItem(datareader["ID"].ToString());
                item.SubItems.Add(datareader["Name"].ToString());
                item.SubItems.Add(datareader["Birthday"].ToString());


                listView1.Items.Add(item);
            }
        }


        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

but this is not what my teacher wants. He wanted to see how I do the search using this code to populate the Listview

DataTable dtable = mainDatabaseDataSet.Tables["Employee"];

I know someone here could have an Idea on how to do this. Please help me.

Emman
  • 105
  • 1
  • 3
  • 12

3 Answers3

0

You can also filter datarow. Refer

https://msdn.microsoft.com/en-us/library/way3dy9w(v=vs.110).aspx

like dtable.Select(Name=textbox1.text);

Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
  • I used these code but I don't have any Idea where and how I will put this in my Search Button dtable.Select("Name like '%" + textBox4.Text + "%'"); – Emman Jun 16 '15 at 05:25
  • When you click search button to write textbox some text. It should be create event of this button and write code in this event. – Mukesh Kalgude Jun 16 '15 at 05:29
  • You can already filter data in query. SqlCeCommand search = new SqlCeCommand("SELECT * FROM Employee Where Name like '%" + Search.Text + "%'", connection); Do not need this code dtable.Select("Name like '%" + Search.Text + "%'"); – Mukesh Kalgude Jun 16 '15 at 05:52
0

You can set dtable.DefaultView.RowFilter property in SearchButton click handler and iterate DataTable.DefaultView instead of table rows in Populate_Click method.

cyberj0g
  • 3,707
  • 1
  • 19
  • 34
0

I just solved it on my own. Thanks for glancing in my question. Here's the Code that I used

 public MainDatabaseDataSet.EmployeeDataTable getData(string data)
    {
        MainDatabaseDataSetTableAdapters.EmployeeTableAdapter returnEmployee = new MainDatabaseDataSetTableAdapters.EmployeeTableAdapter();
        return returnEmployee.GetDataByName(this.textBox4.Text.Trim());
    }

    private void Search_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();

        try
        {

            MainDatabaseDataSet.EmployeeDataTable GetName = getData(this.Search.Text);
            MainDatabaseDataSet.EmployeeRow GetName2 = (MainDatabaseDataSet.EmployeeRow)GetName.Rows[0];


            for (int i = 0; i < GetName.Rows.Count; i++)
            {
                DataRow drow = GetName.Rows[i];

                if (drow.RowState != DataRowState.Deleted)
                {
                    ListViewItem item = new ListViewItem(drow["ID"].ToString());
                    item.SubItems.Add(drow["Name"].ToString());
                    item.SubItems.Add(drow["Birthday"].ToString());

                    listView1.Items.Add(item);
                }


            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Emman
  • 105
  • 1
  • 3
  • 12