Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to do like this but i am using linq any examples how i do that?

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        BindGrid();
    }
 
    private void BindGrid()
    {
        string constring = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        dataGridView1.DataSource = dt;
                    }
                }
            }
        }
    }
}
Posted

Take a linqtosql class then go to Server Explorer and Drag n Drop Table

Then go to Page_load

DataClassesDataContext db = new DataClassesDataContext();

var hp=from p in db.tbl_test_linqs select p;
DataGridView1.DataSource = hp;
 
Share this answer
 
You have to use ADO.NET Entity Data Model to use LINQtoSQL and then ou can do like
C#
//To Select Data from Customers Table
var db = new DatabaseEntities();
var cstmrData = from c in db.Customers select c;

//To fill grid
dataGridView1.DataSource = cstmrData;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900