Advertisement
In this Article I will explain how to display data in DataGridView in C# win forms.
I created a Database name Chinnu; I created a simple table as shown below.
I created a Database name Chinnu; I created a simple table as shown below.
Create a new project in C#, select Windows in Visual c# as shown below
Click Ok.
Click on Form1 in solution explorer ,drag the data gridview in Form1 as shown,
After dragging datagridview I will looks like this.
We have to add AppConfig for Database connection, Right click on the solution explorer as shown below.
Select the Application configuration file click on add.
Right click on Datagridview ,click view code
Write the code in Form1.cs and Appconfig as shown below.
Important: Don't forget to call the DataGridView method in Form Load.
Important: Don't forget to call the DataGridView method in Form Load.
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 DatagridviewExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { displayDataGridView(); } public void displayDataGridView() { SqlConnection con = new SqlConnection("Data Source = CHINNU;Initial Catalog = dotnetdb;Uid = sa;Password = password123;"); { SqlCommand cmd; cmd = new SqlCommand("select * from Employee", con); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; dataGridView1.AutoGenerateColumns = false; dataGridView1.AllowUserToAddRows = false; } } } } // Place this code in App configOut Put
dataGridView1.DataSource = ds.Tables[0];
ReplyDeletewhat is the use of it?
Waht code to place in App.config???
ReplyDelete