简体   繁体   中英

How to a display the data from database to the databound datagridview?

So how to I display data from the database to datagridview??

I am using Visual Studio 2012

I am using Window Form Application

Here are some code that is related to make the code

using System.Data.SqlServerCe;
SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Co-op\Contact\Contact\ContactDataBase.sdf;Password=********");

This column is on my table:

Id , Name , Adress , Phone , Email

This is an example of how to do it

using (SqlDataAdapter sa = CreateCustomAdapter(OrderIDInt))
        {
            sa.Fill(OrderTable);
            dataGridView1.DataSource = OrderTable;
        }

Creat your sql data adapter

 public static SqlDataAdapter CreateCustomAdapter(int OrderIDInt)
    {

        SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Co-op\Contact\Contact\ContactDataBase.sdf;Password=********");
        con.Open();
        SqlDataAdapter adapter = new SqlDataAdapter();

Thes is a good place to create sql parameters if you need it

SqlParameter param1 = new SqlParameter("@CURSTAT", record.curstat);

Write the select command

        adapter.SelectCommand = new SqlCommand("select Id, Name, Adress, Phone, Email from [The name of the table]", coמ);

If you are using sql parameters you should include thes line

        adapter.SelectCommand.Parameters.Add(param1 );

Then you should return the adapter

return adapter;
}

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