Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir!
I want to connect one tab Control to another tab control because 1st tab control included data addition & one tab control contains the datagrid view.
I want to show the updated data in the datagridview which is being saved.
how i can do that.
Posted
Updated 2-Feb-12 14:57pm
v2
Comments
Rajesh Anuhya 2-Feb-12 20:57pm    
Edited: Spelling, readabulity
--RA
_Maxxx_ 2-Feb-12 21:25pm    
You need to give a little more information. Like is this windows forms you're talking about? Are you using data binding for one or both tabs?
If the answer to both is Yes then just bind both to the same datasource
BillWoodruff 3-Feb-12 0:29am    
Please clarify exactly the nature of the "views:" what is seen within each TabPage, within the two TabControls.

You click on TabPage1 of TabControl1, and then, in TabControl2, you see its TabPage1 view of the DataGridView that matches whatever information is displayed in TabControl1, TabPage1.

Or ?
tusharkaushik 3-Feb-12 11:17am    
insertion in the one tab, accessing data in another tab!
BillWoodruff 3-Feb-12 11:27am    
Insertion of "what" in one Tab. Do you mean "selection" by clicking a TabPage header ? ... or ... ?

You really need to clarify this question: please use the full terms for the control, TabControl, and the tab pages, TabPage.

1 solution

Use this code to learn how to update datagrid view on form

C#
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;

            // create and open a connection object
            conn = new
                SqlConnection(@"Server=MDT765\MDT765;DataBase=TST;user=user;password=user@123;Integrated Security=SSPI");
            conn.Open();

            // 1. create a command object identifying
            // the stored procedure
            SqlCommand cmd = new SqlCommand(
                "sp_SaveEmployeeDetail", conn);

            // 2. set the command object so it knows
            // to execute a stored procedure
            cmd.CommandType = CommandType.StoredProcedure;

            // 3. add parameter to command, which
            // will be passed to the stored procedure
            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));

            // execute the command
            rdr = cmd.ExecuteReader();

            string myConnection = conn.ConnectionString.ToString();//"Data Source=MDT765;Initial Catalog=TST;User Id=user;Password=user@123;"; 
            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

SQL
USE [TST]
GO
/****** Object:  Table [dbo].[Employee]    Script Date: 02/04/2012 15:44:33 ******/
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
SQL
USE [TST]
GO
/****** Object:  StoredProcedure [dbo].[sp_FindEmployeeDetail]    Script Date: 02/04/2012 15:45:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sp_FindEmployeeDetail]
 AS 
 BEGIN
  SELECT * FROM Employee 
  
END



GO
/****** Object:  StoredProcedure [dbo].[sp_SaveEmployeeDetail]    Script Date: 02/04/2012 15:45:36 ******/
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900