C# datagridview

Store procedure :

create procedure sp_getting_tblLogin
as
begin
select * from tblLogin
end 

Application configuration file(App.config) :

If you want to create "App.config" file then you press this shortcut key "Crtl+Shift+A" then you choose the Applicatiion configuration file(App.config)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DBCS" connectionString="data source=.;database=dbSample;integrated security=SSPI" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

Note :-

Form1.cs file:

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 GridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ShowData();
        }
        private void ShowData()
        {
            //App.config name use the connectionstrings
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            //connection string sends SqlConnection class
            SqlConnection con = new SqlConnection(cs);
            //Store Procedure name and connection object send in Sqlcommand class
            SqlCommand cmd = new SqlCommand("sp_getting_tblLogin", con);
            //command text interpreted in store procedured
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                    //connection open
                    con.Open();
                     //cmd executes reader
                    SqlDataReader sdr = cmd.ExecuteReader();
                    //Creating binding source object
                    BindingSource source = new BindingSource();
                    SqlDataReader object transfer datasource into BindingSource Object
                    source.DataSource = sdr;
                    //datagrid view assign BindingSource object
                    dataGridView1.DataSource = source;
                    //Releases all resources
                    cmd.Dispose();
            }
            catch (Exception ex)
            {
                Any exception are occured then message box show the exception message
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //connection close
                con.Close();
            }
        }
    }
}

Note :

Form1.Desinger.cs file:

namespace GridView
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.Size = new System.Drawing.Size(375, 261);
            this.dataGridView1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(375, 261);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
    }
}

Output

Output of datagridview

Write a comment


Name Email Message