连接两个标签控件 [英] connecting two tab control

查看:63
本文介绍了连接两个标签控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生!
我想将一个选项卡控件连接到另一个选项卡控件,因为第一个选项卡控件包括数据添加&一个选项卡控件包含datagrid视图.
我想在要保存的datagridview中显示更新的数据.
我该怎么做.

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.

推荐答案

使用此代码来学习如何更新表单上的datagrid视图

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;

            // 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();
        }
    }
}



要使用上述代码,请使用C#创建一个Windows应用程序,并将其命名为
CP_324051_connecting_two_tab_control

然后创建数据库并将其命名为"TST"
使用以下脚本创建表



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
/****** 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

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



对于UI设计
您必须将其放置在选项卡控件上,并在第一个选项卡中放置四个文本框和一个命令按钮

删除form1.cs中的所有代码,并将其替换为上述代码

测试
1)运行程序.
2)在所有文本框中分别输入1,``ABC'',``DEF'',GHI''.
3)点击保存,在第二个标签页中查看datagridview.它将随您的
更新 值
4)然后分别在所有文本框中分别输入1,"ABC_Updated","DEF_Updated",GHI_Updated.
5)点击保存,在第二个选项卡中查看datagridview.它将随您的
更新 值.


希望这可以帮助 .如果是,则接受答案并投票,否则选择
返回 您的查询.

--Rahul D.



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.


这篇关于连接两个标签控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆