In this article I will explain how to display contents of DataTable in DataGridView control in Windows Forms Application using C# and VB.Net.
Namespaces
You will need to import the following namespace.
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlClient;
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;
using System.Configuration;
namespace Pavan_Fabrics
{
public partial class AllCompany : Form
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["admin"].ConnectionString);
int ID=0;
public static string SetValueForText1 = "";
public AllCompany()
{
InitializeComponent();
}
private void AllCompany_Load(object sender, EventArgs e)
{
kaushik();
}
public string acompany = "";
private void kaushik()
{
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
label1.Text = acompany.ToString();
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Company_Info", con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
if (dt.Rows.Count > 0)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
string selectstr = "select * from dbo.Company_Info";
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da = new SqlDataAdapter(selectstr, con);
ds = new DataSet("dbo.Company_Info");
da.SelectCommand = new SqlCommand(selectstr, con);
ds.Clear();
da.Fill(ds, "dbo.Company_Info");
dataGridView1.DataSource = ds.Tables["dbo.Company_Info"];
dataGridView1.Columns[1].HeaderText = "Code";
dataGridView1.Columns[2].HeaderText = "Name";
dataGridView1.Columns[3].HeaderText = "Address";
dataGridView1.Columns[4].HeaderText = "Date";
dataGridView1.Columns[5].HeaderText = "Mobileno";
dataGridView1.Columns[6].HeaderText = "Email Id";
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Visible = false;
dataGridView1.Columns[2].Width = 140;
dataGridView1.Columns[3].Width = 140;
dataGridView1.Columns[4].Width = 200;
dataGridView1.Columns[5].Width = 175;
dataGridView1.Columns[6].Width = 175;
dataGridView1.Visible = true;
con.Close();
}
else
{
}
con.Close();
}
catch
{
}
}
private void button4_Click(object sender, EventArgs e)
{
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = new SqlCommand("delete Company_Info where Id=@Id", con);
cmd.Parameters.AddWithValue("@Id", ID);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
cmd.ExecuteNonQuery();
kaushik();
Delete_Successfully ds = new Delete_Successfully();
ds.ShowDialog();
}
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
label2.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
label3.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
SetValueForText1 = ID.ToString();
this.Hide();
Company ac = new Company();
ac.ShowDialog();
}
}
}