Datagridview自动刷新未完成 [英] Datagridview auto refresh incompleted

查看:76
本文介绍了Datagridview自动刷新未完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


数据插入到sql中不断更改数据网格显示。但它并没有完全显现出来。例如,我在这张表中有四列。在第一次运行时显示4列数据。但是当我将数据插入到表中时,两个列数据只有它的
来了。



Column1,第4列未出现在datagridview中而按钮点击。请注意。第1列(ID)自动增量和列(时间)是自动时间戳。我的sql代码是

 USE [d] 
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo]。[data](
[ID] [int] IDENTITY(1,1)NOT NULL,
[no] [nvarchar] (255)NULL,
[name] [nvarchar](255)NULL,
[time] [datetime] NULL,
PRIMARY KEY CLUSTERED

[ID ] ASC
)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]
)ON [PRIMARY]
GO

我的vb代码是 

 protected void button1_Click_1(object sender,EventArgs e)

{
SqlConnection con = new SqlConnection();
con.ConnectionString =(" Data Source = PC; Initial Catalog = d; Integrated Security = True");
con.Open();
String st =" INSERT INTO data(no,name,time)values(@ no,@ name,GETDATE())" ;;
SqlCommand cmd = new SqlCommand(st,con);
cmd.Parameters.AddWithValue(" @ no",textBox5.Text);
cmd.Parameters.AddWithValue(" @ name",textBox6.Text);
cmd.ExecuteNonQuery();
MessageBox.Show(" Saved");
con.Close();
textBox5.Text ="" ;;
textBox6.Text ="" ;;

dataBindingSource5.DataSource = dataTableAdapter.GetData();
dataBindingSource5.ResetBindings(false);
//// dataGridView2.Refresh();

}



解决方案

< blockquote>

您好,


不确定您是使用TableAdapter还是DataAdapter,但如果这是使用Insert方法添加记录的TableAdapter,则之后执行Fill方法。



使用变量而不是文本框控件的概念性示例。

 private void button1_Click(object sender,EventArgs e)
{
var companyName =" Test" ;;
var contactName =" Karen Payne" ;;
var contactTitle =" Owner" ;;

if(customersTableAdapter.Insert(companyName,contactName,contactTitle,","",1)== 1)
{
this.customersTableAdapter.Fill (northWindDataSet.Customers);
}
}



Data insert in to sql is keep changing in datagrid display. But its not appearing fully. example i have got four column in this table. While run first time its displaying 4 columns data. But while i inserting the data in to table then two column data only its coming.

Column1,Column 4 is not appearing in datagridview while button click . please note that. Column 1(ID) auto-increment and Column(time) is autotimestamp. my sql code is

USE [d]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[data](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[no] [nvarchar](255) NULL,
	[name] [nvarchar](255) NULL,
	[time] [datetime] NULL,
PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

my vb code is 

 protected void button1_Click_1(object sender, EventArgs e)

        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ("Data Source=PC;Initial Catalog=d;Integrated Security=True");
            con.Open();
            String st = "INSERT INTO data(no,name,time)values (@no, @name, GETDATE())"; 
            SqlCommand cmd = new SqlCommand(st, con);
            cmd.Parameters.AddWithValue("@no", textBox5.Text);
            cmd.Parameters.AddWithValue("@name", textBox6.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Saved");
            con.Close();
            textBox5.Text = "";
            textBox6.Text = "";
    
            dataBindingSource5.DataSource = dataTableAdapter.GetData();
             dataBindingSource5.ResetBindings(false);
            //// dataGridView2.Refresh();
            
        }


解决方案

Hello,

Not sure if you are using a TableAdapter or a DataAdapter but if this is a TableAdapter using the Insert method to add the record then do the Fill method afterwards.

A conceptual example where variables are used instead of text box controls.

private void button1_Click(object sender, EventArgs e)
{
    var companyName = "Test";
    var contactName = "Karen Payne";
    var contactTitle = "Owner";

    if (customersTableAdapter.Insert(companyName, contactName, contactTitle, "", "", 1) == 1)
    {
        this.customersTableAdapter.Fill(northWindDataSet.Customers);
    }
}


这篇关于Datagridview自动刷新未完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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