简体   繁体   中英

Displaying selected data in a datagridview

I have codes here that display all data in access database. Is it possible to only display specific or selected data from my database?

here is my code :

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\SISC-STRONGHOLD\MIS!\wilbert.beltran\SEEDBucksDbase.accdb";
string query = "SELECT * From TableAcct";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
    {
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        dataGridView1.Columns["UserPassword"].Visible = false;

    }
    conn.Close();
}

thanks! :)

you just need to write down where condition in you query will do work for you,

Following is example of it

string query = "SELECT col1,col2 From TableAcct where col1=value";

This will fetch data that satisfy you condition and bind with the grid control.

And instead of * in select list give name of the columns you want to display.

for getting value form control like textbox

string query = "SELECT * FROM Employee WHERE LastName='"+ textBox1.Text +"'";

Note - make use of sqlparameter to avoid SQLInjection , above is just an example.

To query for a last name (assuming the column in the table is called LastName), try this:

string query = "SELECT * FROM Employee WHERE LastName='"+ textBox1.Text +"'";

Of course, you have to be careful of SQL Injection attacks. I'm not familiar with using paramaterized queries in Access.

Is this a public form, or internal just for you (or trusted users)?

string query = "SELECT (SelectedColumnNames) From TableAcct Where LastName=(LastName)";

INstead of Searching with a * in a Query enter the desired columns for ex:- string query = "SELECT phno,address,FirstName From TableAcct where LastName ='"+txt1.text +"'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM