WordSide
WordSide

Reputation: 11

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

Upvotes: 1

Views: 52

Answers (1)

user5993035
user5993035

Reputation:

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;
}

Upvotes: 2

Related Questions