Saturday, 30 November 2013

How to bind DataGridView with Connected and Dis Connected Mode in Ado.Net

How to bind DataGridView with Connected and Dis Connected  Mode in Ado.Net

Problem : when you bind the GridView in Asp.Net with SqlDataReader dr Its binds with GridView but it doesnot happens in with DataGridView in Windows Application.Now the solution of this problem is given below….

Create table ContactDetails for binding DataGridView…………………………………….

create table ContactDetails(SrNo int identity(1,1) primary key,Name nvarchar(50) unique,Business_MobileNo nvarchar(50),Office_MobileNo nvarchar(50),Home_MobileNo nvarchar(50))

Code for From1.cs ………………………………………………..
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 DataGridViewBind
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
//bind DataGridView in Windows Application with Connected Mode..........

private void btnConnectedMode_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial Catalog=sqltopquery;User ID=sa;Password=tiwari");

SqlCommand cmd = new SqlCommand("Select * from ContactDetails", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
con.Close();
dataGridView1.DataSource = dt;
}
//bind DataGridView in Windows Application with DisConnected Mode..............

private void btnDisconnectedMode_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial Catalog=sqltopquery;User ID=sa;Password=tiwari");

SqlCommand cmd = new SqlCommand("Select * from ContactDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
}

0 comments:

Post a Comment