Recent

6/recent/ticker-posts

Header Ads Widget

Show data in DataGridView

The DataGridView Control in dot net(c sharp) is the very useful data control. The DataGridView control makes it easy to define the basic appearance of cells and the display formatting of cell values. DataGridView Control has many properties and events we .
DataGridView control is the most useful control for data control. Also most used control for showing report and other details.

First, we are going to show data in gridview 
1. database : i have two table with billing and servicecost



Show data in DataGridView
 Now i have a windows form . which shows the patient total expenses.

Add DataGridView Control.
Show data in DataGridView
Add caption
create connectionstring 

SqlConnection con = new SqlConnection("Data Source=servername;Min Pool Size=20;Max Pool Size=500; Initial Catalog=databasename;Integrated Security=False;User ID=databaseusername;Password=serverpassword;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
create a function to load data
 DataTable dt = new DataTable();            SqlDataAdapter sda = new SqlDataAdapter("patientgetAllexpenses", con);
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;            sda.Fill(dt);            dataGridView1.DataSource = dt

Show data in DataGridView

Complete Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 namespace HMS
{
    public partial class frmpatientExpenses : Form
    {        public frmpatientExpenses()
        {            InitializeComponent();        }SqlConnection con = new SqlConnection("Data Source=servername;Min Pool Size=20;Max Pool Size=500; Initial Catalog=databasename;Integrated Security=False;User ID=databaseusername;Password=serverpassword;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
        private void button1_Click(object sender, EventArgs e)
        {            loaddata();        }         private void loaddata()
        {            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter("patientgetAllexpenses", con);
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;            sda.Fill(dt);            dataGridView1.DataSource = dt;         
        }    }}  

Post a Comment

0 Comments