Insert ,Update ,Delete and Display in Datagridview using LINQ To SQL in C#.Net
Create tables in SQL server
Go To Server Explore
Add Data Connection
Drag Tables into LIQToSQL Class
Create user Control
Add User Control in your Project.
Add Datagridview and Two buttons on the User Control
Declare Variables ,Data Context and Events
Create tables in SQL server
tbl_department_master
depId int -- Primary Key -- Set Identify is true
DepName -- varchar(Max)
depId int -- Primary Key -- Set Identify is true
DepName -- varchar(Max)
tbl_Position_Master
PatId int -- Primary key
Patname-- varchar(Max)
Add LINQ To SQL in the your projectPatId int -- Primary key
Patname-- varchar(Max)
Go To Server Explore
Add Data Connection
Drag Tables into LIQToSQL Class
Create user Control
Add User Control in your Project.
Add Datagridview and Two buttons on the User Control
Declare Variables ,Data Context and Events
DataClasses1DataContext objDC = null;
tbl_Department_Master objDM = null;
tbl_Position_Master objPM = null;
public event EventHandler SaveClicked;
public event EventHandler CloseClicked;
/// To Display Datatbl_Department_Master objDM = null;
tbl_Position_Master objPM = null;
public event EventHandler SaveClicked;
public event EventHandler CloseClicked;
public void Displaydata(string mode)
{
switch (mode)
{
case "Department":
DisplayData("Department");
break;
case "Position":
DisplayData("Position");
break;
}
}
private void DisplayData(string mode)
{
objDC = new DataClasses1DataContext();
switch (mode)
{
case "Department":
var query = from q in objDC.tbl_Department_Masters
select q;
dataGridView1.DataSource = query;
break;
case "Position":
var Posquery = from Pq in objDC.tbl_Position_Masters
select Pq;
dataGridView1.DataSource = Posquery;
break;
}
}
/// Delete Data{
switch (mode)
{
case "Department":
DisplayData("Department");
break;
case "Position":
DisplayData("Position");
break;
}
}
private void DisplayData(string mode)
{
objDC = new DataClasses1DataContext();
switch (mode)
{
case "Department":
var query = from q in objDC.tbl_Department_Masters
select q;
dataGridView1.DataSource = query;
break;
case "Position":
var Posquery = from Pq in objDC.tbl_Position_Masters
select Pq;
dataGridView1.DataSource = Posquery;
break;
}
}
private void DeleteData(string mode)
{
objDC = new DataClasses1DataContext();
switch (mode)
{
case "Department":
objDM = new tbl_Department_Master();
if (objDM != null)
{
var Delet = from d in objDC.tbl_Department_Masters
select d;
foreach (var details in Delet)
{
objDC.tbl_Department_Masters.DeleteOnSubmit(details);
}
objDC.SubmitChanges();
}
break;
case "Position":
objPM = new tbl_Position_Master();
if (objPM != null)
{
var Delet = from d in objDC.tbl_Position_Masters
select d;
foreach (var details in Delet)
{
objDC.tbl_Position_Masters .DeleteOnSubmit(details);
}
objDC.SubmitChanges();
}
break;
}
}
/// Save Data{
objDC = new DataClasses1DataContext();
switch (mode)
{
case "Department":
objDM = new tbl_Department_Master();
if (objDM != null)
{
var Delet = from d in objDC.tbl_Department_Masters
select d;
foreach (var details in Delet)
{
objDC.tbl_Department_Masters.DeleteOnSubmit(details);
}
objDC.SubmitChanges();
}
break;
case "Position":
objPM = new tbl_Position_Master();
if (objPM != null)
{
var Delet = from d in objDC.tbl_Position_Masters
select d;
foreach (var details in Delet)
{
objDC.tbl_Position_Masters .DeleteOnSubmit(details);
}
objDC.SubmitChanges();
}
break;
}
}
public void SaveData(string mode)
{
switch (mode)
{
case "Department":
DeleteData("Department");
objDC = new DataClasses1DataContext();
for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)
{
objDM = new tbl_Department_Master();
objDM.depId = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
objDM.DepName = Convert.ToString(dataGridView1.Rows[index].Cells[1].Value);
objDC.tbl_Department_Masters.InsertOnSubmit(objDM);
objDC.SubmitChanges();
}
DisplayData("Department");
break;
case "Position":
DeleteData("Position");
objDC = new DataClasses1DataContext();
for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)
{
DataGridViewRow objRowIndex = new DataGridViewRow();
objPM = new tbl_Position_Master();
objPM.PatId = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
objPM.Patname = Convert.ToString(dataGridView1.Rows[index].Cells[1].Value);
objDC.tbl_Position_Masters.InsertOnSubmit(objPM);
objDC.SubmitChanges();
}
DisplayData("Position");
break;
}
}
Call this User Control in your form:
Add the code in your code Page
{
switch (mode)
{
case "Department":
DeleteData("Department");
objDC = new DataClasses1DataContext();
for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)
{
objDM = new tbl_Department_Master();
objDM.depId = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
objDM.DepName = Convert.ToString(dataGridView1.Rows[index].Cells[1].Value);
objDC.tbl_Department_Masters.InsertOnSubmit(objDM);
objDC.SubmitChanges();
}
DisplayData("Department");
break;
case "Position":
DeleteData("Position");
objDC = new DataClasses1DataContext();
for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)
{
DataGridViewRow objRowIndex = new DataGridViewRow();
objPM = new tbl_Position_Master();
objPM.PatId = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
objPM.Patname = Convert.ToString(dataGridView1.Rows[index].Cells[1].Value);
objDC.tbl_Position_Masters.InsertOnSubmit(objPM);
objDC.SubmitChanges();
}
DisplayData("Position");
break;
}
}
Call this User Control in your form:
Add the code in your code Page
private void DisplayData(string mode)
{
switch (mode)
{
case "Department":
ucDepartment1.Displaydata("Department");
break;
case "Position":
ucDepartment1.Displaydata("Position");
break;
}
}
private void FrmDepartment_Load(object sender, EventArgs e)
{
DisplayData("Position");
}
private void ucDepartment1_Load(object sender, EventArgs e)
{
}
private void ucDepartment1_CloseClicked(object sender, EventArgs e)
{
((Form)this.TopLevelControl).Close();
}
private void ucDepartment1_SaveClicked(object sender, EventArgs e)
{
ucDepartment1.SaveData("Position");
}
{
switch (mode)
{
case "Department":
ucDepartment1.Displaydata("Department");
break;
case "Position":
ucDepartment1.Displaydata("Position");
break;
}
}
private void FrmDepartment_Load(object sender, EventArgs e)
{
DisplayData("Position");
}
private void ucDepartment1_Load(object sender, EventArgs e)
{
}
private void ucDepartment1_CloseClicked(object sender, EventArgs e)
{
((Form)this.TopLevelControl).Close();
}
private void ucDepartment1_SaveClicked(object sender, EventArgs e)
{
ucDepartment1.SaveData("Position");
}