Tuesday, 2 August 2011

How to bind Data grid view in C#




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;




namespace Wpf
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=school;Integrated Security=True");
        SqlDataAdapter ad1 = new SqlDataAdapter();
       
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            ad1 = new SqlDataAdapter("Select * from Table1", con);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(ad1);
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            ad1.Fill(table);          
            dataGridView1.DataSource = table;


        }


        private void button1_Click(object sender, EventArgs e)
        {
            Window1 win = new Window1();
            win.Show();
        }
    }
}
Like This Screen shot