Making Datagrid Searchable with MySQL database in C# WPF -
so got datagrid working in c# wpf, connects mysql database , shows first name , last name of people in database. i'm trying make search button searches last name entered in text box , display in datagrid. know code wrong don't know how this. help
this connects database first name last name , works fine
try { command = connc.createcommand(); command.commandtext = "select first_name, last_name studentinfo"; mysqldatareader reader = command.executereader(); string result = string.empty; while (reader.read()) { result += reader["first_name"].tostring() + " " + reader["last_name"].tostring() + " "; } read = result.split(' '); reader.close(); int count = 0; mysqldataadapter = new mysqldataadapter("select first_name, last_name studentinfo", connc); dataset ds = new dataset(); mysqldataadapter.fill(ds); data.setbinding(itemscontrol.itemssourceproperty, new binding { source = ds.tables[0] }); messagebox.show("s"); } catch (exception ex) { messagebox.show("something went wrong: " + ex.tostring()); }
here im trying use textbox make datagrid searchable
command = connc.createcommand(); mysqldataadapter = new mysqldataadapter("select last_name studentinfo last_name = @last_name", connc); command.parameters.addwithvalue("@last_name", txtsearch.text); dataset ds = new dataset(); mysqldataadapter.fill(ds); data.setbinding(itemscontrol.itemssourceproperty, new binding { source = ds.tables[0] }); messagebox.show("s"); } catch (exception ex) { messagebox.show("something went wrong: " + ex.tostring()); } { connc.close(); }
in piece of code using textbox, never doing while(reader.read()){}-loop.
then, make life easier: create class "student" putting in strings, gluing them space, , seperating them "split"-method. that's torturing yourself. like:
public class student { string firstname; string lastname; public student(string pfirstname, string plastname) { firstname = pfirstname; lastname = plastname; } public string firstname { { return firstname; } set { firstname = value; } } public string lastname { { return lastname; } set { lastname = value; } } }
then can use list of students fill while doing reader.read()-thing:
list<student> results = new list<student>(); // build connection while(reader.read()) { results.add(new student(reader["first_name"], reader["last_name"])); }
then can place bindingsource on form , say:
mybindingsource.datasource = results; publisherdatagridview.datasource = mybindingsource;
Comments
Post a Comment