Use this code to learn how to update datagrid view on form
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.Data.OleDb;
namespace CP_324051_connecting_two_tab_control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = null;
SqlDataReader rdr = null;
conn = new
SqlConnection(@"Server=MDT765\MDT765;DataBase=TST;user=user;password=user@123;Integrated Security=SSPI");
conn.Open();
SqlCommand cmd = new SqlCommand(
"sp_SaveEmployeeDetail", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("@Id", textBox1.Text));
cmd.Parameters.Add(
new SqlParameter("@Name", textBox2.Text));
cmd.Parameters.Add(
new SqlParameter("@Position", textBox3.Text));
cmd.Parameters.Add(
new SqlParameter("@Location", textBox4.Text));
rdr = cmd.ExecuteReader();
string myConnection = conn.ConnectionString.ToString();
SqlDataAdapter sqlcom0 = new SqlDataAdapter("SELECT * FROM Employee", myConnection);
DataSet ds0 = new DataSet();
sqlcom0.Fill(ds0);
dataGridView1.DataSource = ds0.Tables[0].DefaultView;
dataGridView1.Refresh();
}
}
}
To use above code create one windows application in C# and name it as
CP_324051_connecting_two_tab_control
Then Create database and name it as "TST"
Create table using below script
USE [TST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employee](
[Id] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Name] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Position] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Location] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
Create below procedures
USE [TST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_FindEmployeeDetail]
AS
BEGIN
SELECT * FROM Employee
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_SaveEmployeeDetail]
( @Id NVARCHAR(10),
@Name NVARCHAR(255),
@Position NVARCHAR(255),
@Location NVARCHAR(255)
)
AS
BEGIN
If EXISTS (SELECT * FROM Employee WHERE Id=@Id)
Begin
Update Employee
SET
Name=@Name ,
Position=@Position ,
Location=@Location
WHERE Id=@Id
End
Else
Begin
Insert Into Employee SELECT @Id,@Name,@Position,@Location
End
END
For UI Design
You have to place on tab control and in first tab place four text boxes and one command button
Remove all code in form1.cs and replace it with above code
To test
1) run the program.
2) enter 1 , 'ABC','DEF',GHI' in all textboxes respectively.
3) Click on save and see the datagridview in second tab.It will get updated with your
values
4) Then again enter 1 , 'ABC_Updated','DEF_Updated',GHI_Updated' in all textboxes respectively.
5) Click on save and see the datagridview in second tab.It will get updated with your
values.
Hope this helps . If yes then accept the answer and vote it other wise revert back with
your queries.
--Rahul D.