Hi PSowmiya,
To display scrollbar in DataGridView set the AutoSize property to false and ScrollBars property to Both.
Refer below example.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = GetData();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 8;
dataGridView1.DataSource = dt;
dataGridView1.AutoSize = false;
dataGridView1.ScrollBars = ScrollBars.Both;
dataGridView1.Columns[0].Name = "Id";
dataGridView1.Columns[0].HeaderText = "Id";
dataGridView1.Columns[0].DataPropertyName = "CustomerID";
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[1].DataPropertyName = "ContactName";
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.Columns[2].Name = "City";
dataGridView1.Columns[2].HeaderText = "City";
dataGridView1.Columns[2].DataPropertyName = "City";
dataGridView1.Columns[2].ReadOnly = true;
dataGridView1.Columns[3].Name = "Country";
dataGridView1.Columns[3].HeaderText = "Country";
dataGridView1.Columns[3].DataPropertyName = "Country";
dataGridView1.Columns[3].ReadOnly = true;
dataGridView1.Columns[4].Name = "Address";
dataGridView1.Columns[4].HeaderText = "Address";
dataGridView1.Columns[4].DataPropertyName = "Address";
dataGridView1.Columns[4].ReadOnly = true;
DataGridViewComboBoxColumn returnstatus = new DataGridViewComboBoxColumn();
returnstatus.Name = "ReturnStatus";
returnstatus.Items.Add("Yes");
returnstatus.Items.Add("No");
dataGridView1.Columns.Insert(5, returnstatus);
dataGridView1.Columns[6].Name = "PostalCode";
dataGridView1.Columns[6].HeaderText = "PostalCode";
dataGridView1.Columns[6].DataPropertyName = "PostalCode";
dataGridView1.Columns[6].ReadOnly = true;
dataGridView1.Columns[7].Name = "Phone";
dataGridView1.Columns[7].HeaderText = "Phone";
dataGridView1.Columns[7].DataPropertyName = "Phone";
dataGridView1.Columns[7].ReadOnly = true;
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 20 * FROM Customers";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = GetData()
dataGridView1.AutoGenerateColumns = False
dataGridView1.ColumnCount = 8
dataGridView1.DataSource = dt
dataGridView1.AutoSize = False
dataGridView1.ScrollBars = ScrollBars.Both
dataGridView1.Columns(0).Name = "Id"
dataGridView1.Columns(0).HeaderText = "Id"
dataGridView1.Columns(0).DataPropertyName = "CustomerID"
dataGridView1.Columns(0).ReadOnly = True
dataGridView1.Columns(1).Name = "Name"
dataGridView1.Columns(1).HeaderText = "Name"
dataGridView1.Columns(1).DataPropertyName = "ContactName"
dataGridView1.Columns(1).ReadOnly = True
dataGridView1.Columns(2).Name = "City"
dataGridView1.Columns(2).HeaderText = "City"
dataGridView1.Columns(2).DataPropertyName = "City"
dataGridView1.Columns(2).ReadOnly = True
dataGridView1.Columns(3).Name = "Country"
dataGridView1.Columns(3).HeaderText = "Country"
dataGridView1.Columns(3).DataPropertyName = "Country"
dataGridView1.Columns(3).ReadOnly = True
dataGridView1.Columns(4).Name = "Address"
dataGridView1.Columns(4).HeaderText = "Address"
dataGridView1.Columns(4).DataPropertyName = "Address"
dataGridView1.Columns(4).ReadOnly = True
Dim returnstatus As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn()
returnstatus.Name = "ReturnStatus"
returnstatus.Items.Add("Yes")
returnstatus.Items.Add("No")
dataGridView1.Columns.Insert(5, returnstatus)
dataGridView1.Columns(6).Name = "PostalCode"
dataGridView1.Columns(6).HeaderText = "PostalCode"
dataGridView1.Columns(6).DataPropertyName = "PostalCode"
dataGridView1.Columns(6).ReadOnly = True
dataGridView1.Columns(7).Name = "Phone"
dataGridView1.Columns(7).HeaderText = "Phone"
dataGridView1.Columns(7).DataPropertyName = "Phone"
dataGridView1.Columns(7).ReadOnly = True
End Sub
Private Function GetData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT TOP 20 * FROM Customers"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot