以窗体形式加载数据而不关闭表单 [英] Load data in windows form without closing form

查看:60
本文介绍了以窗体形式加载数据而不关闭表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我遇到了问题。我使用SQL Server 2008作为我的数据库。一切都可以加载,添加,数据。



问题是我在数据库中插入记录。插入记录后,它不会在表单中正确显示,除非我关闭表单并重新加载它。在C#中是否有任何方法可以刷新表格,并且可以在不重新启动表格的情况下同时加载表格?



谢谢

//这是我的代码//



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;

使用System.Data.SqlClient;

使用System。 Data.Sql;



命名空间MyProject

{



公共部分类表格1:表格

{

DataTable tbl = new DataTable();

int i = 0;



public Form1()

{

InitializeComponent();

}



private void Form1_Load(对象发送者,EventArgs e)

{

SqlConnection conn = new SqlConnection(@Data Source = .\SQLEXPRESS; AttachDbFilename = C:\ Users \ manish\Documents\ HRM.mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True);

conn.Open();

SqlCommand command = new SqlCommand(select * from emp_detail,conn);

SqlDataAdapter adp = new SqlDataAdapter(command);

adp.Fill(tbl);

display(tbl);





}

私有无效显示(DataTable tbl)

{

txtempid.Text = tbl.Rows [i] [0] .ToString();

txtname.Text = tbl.Rows [i] [1] .ToString();

txtsurname.Text = tbl.Rows [i] [2] .ToString();

txtfathername.Text = tbl.Rows [i] [3] .ToString();

dtdob.Text = tbl.Rows [i] [4] .ToString();

cbgender.Text = tbl.Rows [i] [5] .ToString();

cbcity.Text = tbl.Rows [i] [6] .ToString();

txtcontactno.Text = tbl.Rows [i] [7] .ToString();

dtdoj.Text = tbl.Rows [i] [8] .ToString();

txtdept.Text = tbl.Rows [i] [9] .ToString();

txtdesig.Text = tbl.Rows [i] [10] .ToString();

txtqualification.Text = tbl.Rows [i] [11] .ToString();

rtaddress.Text = tbl.Rows [i] [12] .ToString();

rtremarks.Text = tbl.Rows [i] [13] .ToString();



}

private void button4_Click(object sender,EventArgs e)



{

SqlConnection conn = new SqlConnection(@Data Source = .\ SQLEXPRESS; AttachDbFilename = C:\Users\manish \Documents\ HRM.mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True);

conn.Open();

SqlCommand command = new SqlCommand();

command.CommandText =insert into emp_detail(empid,name,surname,fathername,dob,gender ,city,contactno,doj,department,designation,qualification,address,remarks)values('+ txtempid.Text +','+ txtname.Text +','+ txtsurname.Text +',' + txtfathername.Text +','+ dtdob.Text +','+ cbgender.Text +','+ cbcity.Text +','+ txtcontactno.Text +',' + dtdoj.Text +','+ txtdept.Text +','+ txtdesig.Text +','+ txtqualification.Text +','+ rtaddress.Text +',' + rtremarks.Text +');

command.Connection = conn;

command.ExecuteNonQuery();

MessageBox.Show (保存完成!);

}

解决方案

保存后你必须刷新你的桌子。



private void Form1_Load( object sender,EventArgs e)
{
RefreshData();
}

private void RefreshData()
{
SqlConnection conn = new SqlConnection( @ 数据源= .\SQLEXPRESS; AttachDbFilename = C:\Users\manish \Documents\ HRM.mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True);
conn.Open();
SqlCommand command = new SqlCommand( select *来自emp_detail,conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
adp.Fill(tbl);
display(tbl);
}

private void button4_Click( object sender,EventArgs e)
{
SqlConnection conn = new SqlConnection( @ 数据源=。\SQLEXPRESS; AttachDbFilename = C:\ Users \ manish \Documents\ HRM.mdf; Integrated Security = True;连接超时= 30;用户实例=真);
conn.Open();
SqlCommand command = new SqlCommand();
command.CommandText = 插入emp_detail(empid,name,surname,fathername,dob,gender,city ,contactno,doj,department,designation,qualification,addresses,remarks)values(' + txtempid.Text + ',' + txtname.Text + ',' + txtsurname.Text + ',' + txtfathername.Text + ',' + dtdob.Text + ',' + cbgender.Text + ',' + cbcity.Text + ',' + txtcontactno.Text + ',' + dtdoj.Text + ',' + txtdept.Text + ',' + txtdesig.Text + ',' + txtqualification.Text + ',' + rtaddress.Text + ',' + rtremarks.Text + ');
command.Connection = conn;
command.ExecuteNonQuery();
MessageBox.Show( 保存完成!);

// 在这里你可以刷新你的表
RefreshData( );
}


Hi,

I am having a problem. I am using SQL Server 2008 as my DB. Everything is okay loading, adding, data.

Problem is when i insert a record in database. After inserting a record, it does not show properly in the form unless i close the form and reload it again. Is there any method in C# by which we can refresh the table and can load it in the form same time without restarting the form?

Thanks
//This is My code//

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.Sql;

namespace MyProject
{

public partial class Form1 : Form
{
DataTable tbl = new DataTable();
int i = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
conn.Open();
SqlCommand command = new SqlCommand("select * from emp_detail", conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
adp.Fill(tbl);
display(tbl);


}
private void display(DataTable tbl)
{
txtempid.Text = tbl.Rows[i][0].ToString();
txtname.Text = tbl.Rows[i][1].ToString();
txtsurname.Text = tbl.Rows[i][2].ToString();
txtfathername.Text = tbl.Rows[i][3].ToString();
dtdob.Text = tbl.Rows[i][4].ToString();
cbgender.Text = tbl.Rows[i][5].ToString();
cbcity.Text = tbl.Rows[i][6].ToString();
txtcontactno.Text = tbl.Rows[i][7].ToString();
dtdoj.Text = tbl.Rows[i][8].ToString();
txtdept.Text = tbl.Rows[i][9].ToString();
txtdesig.Text = tbl.Rows[i][10].ToString();
txtqualification.Text = tbl.Rows[i][11].ToString();
rtaddress.Text = tbl.Rows[i][12].ToString();
rtremarks.Text = tbl.Rows[i][13].ToString();

}
private void button4_Click(object sender, EventArgs e)

{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
conn.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "insert into emp_detail(empid,name,surname,fathername,dob,gender,city,contactno,doj,department,designation,qualification,address,remarks) values('" + txtempid.Text + "','" + txtname.Text + "','" + txtsurname.Text + "','" + txtfathername.Text + "','" + dtdob.Text + "','" + cbgender.Text + "','" + cbcity.Text + "','" + txtcontactno.Text + "','" + dtdoj.Text + "','" + txtdept.Text + "','" + txtdesig.Text + "','" + txtqualification.Text + "','" + rtaddress.Text + "','" + rtremarks.Text + "')";
command.Connection = conn;
command.ExecuteNonQuery();
MessageBox.Show("Saving is done!");
}

解决方案

After saving you have to refresh your table.

private void Form1_Load(object sender, EventArgs e)
{
    RefreshData();
}

private void RefreshData() 
{
    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    conn.Open();
    SqlCommand command = new SqlCommand("select * from emp_detail", conn);
    SqlDataAdapter adp = new SqlDataAdapter(command);
    adp.Fill(tbl);
    display(tbl);
}

private void button4_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data  Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    conn.Open();
    SqlCommand command = new SqlCommand();
    command.CommandText = "insert into emp_detail(empid,name,surname,fathername,dob,gender,city,contactno,doj,department,designation,qualification,address,remarks) values('" + txtempid.Text + "','" + txtname.Text + "','" + txtsurname.Text + "','" + txtfathername.Text + "','" + dtdob.Text + "','" + cbgender.Text + "','" + cbcity.Text + "','" + txtcontactno.Text + "','" + dtdoj.Text + "','" + txtdept.Text + "','" + txtdesig.Text + "','" + txtqualification.Text + "','" + rtaddress.Text + "','" + rtremarks.Text + "')";
    command.Connection = conn;
    command.ExecuteNonQuery();
    MessageBox.Show("Saving is done!");

    //here you can refresh your table
    RefreshData();
} 


这篇关于以窗体形式加载数据而不关闭表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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