本文介绍了如何将SQL数据加载到现有的datagridview控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有组合框和文本框元素的窗口格式datagridview。我使用这个datagridview接受来自用户的数据,然后保存在sql数据库中。



每当我试图用sql中的相同数据填充datagridview控件时,它总是创建新的控件集在同一个datagridview上显示我的sql数据,而它将我在设计视图中创建的那些控件留空。



如何在现有控件上填充我的sql数据相同的datagridview对象。



我尝试过:



Dim str作为String

str =select * from ifmis_staff_matrix k order by gl desc

cn.Open()

Dim com As New SqlCommand( str,cn)

Dim rd As SqlDataReader



rd = com.ExecuteReader(CommandBehavior.CloseConnection)

Dim dt As DataTable = New DataTable()

dt.Load(rd)



DataGridView3.DataSource = dt

I have created a window form datagridview having combobox and textbox elements. I used this datagridview to accept data from user then saved in sql database.

Whenever i attempted to populate datagridview control with same data from sql it always create new sets of controls on same datagridview to show my sql data whereas it leaves those controls i created in design view empty.

What do I do to populate my sql data on existing control on the same datagridview object.

What I have tried:

Dim str As String
str = "select * from ifmis_staff_matrix k order by gl desc"
cn.Open()
Dim com As New SqlCommand(str, cn)
Dim rd As SqlDataReader

rd = com.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As DataTable = New DataTable()
dt.Load(rd)

DataGridView3.DataSource = dt

推荐答案

MSDN写道:

如果设置DataSource属性但设置 AutoGenerateColumns false ,您必须手动添加列。 您可以通过设置将每个添加的列绑定到数据源DataGridViewColumn.DataPropertyName属性为绑定对象公开的属性的名称。

If you set the DataSource property but set AutoGenerateColumns to false, you must add columns manually. You can bind each added column to the data source by setting the DataGridViewColumn.DataPropertyName property to the name of a property exposed by the bound objects.


这篇关于如何将SQL数据加载到现有的datagridview控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:25