如何在不更改标题名称的情况下填充数据网格。 [英] How to fill datagrid without changing the header name.

查看:111
本文介绍了如何在不更改标题名称的情况下填充数据网格。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重复使用具有固定标题名称和属性的数据网格来减少我的窗体,因为该功能几乎相同。



应该怎样我添加显示数据?

或者有没有办法显示数据而不更改标题名称?




I want to reuse a datagrid with fixed header name and properties to lessen my windows forms since the function will be almost the same.

What should I add to display the data?
Or is there a way to display the data without changing the header names?


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

	sql = "Select col1,col2,col3 from tbl_detailrec WHERE tc_no ='" & txtgetData.Text & "' order by col1"
	OpenConnection()
	Dim sqlReader As SqlDataReader = command.ExecuteReader()

        If sqlReader.HasRows Then
		Dim dt = New DataTable()
            	dt.Load(sqlReader)
            	DataGridView1.AutoGenerateColumns = False
            	DataGridView1.DataSource = dt
		DataGridView1.Refresh()
	End If

        sqlReader.Close()
 	connection.Close()
End Sub





我是什么尝试过:



我试图删除列然后将 AutoGenerateColumns 设置为 True 但是它也将删除属性。



What I have tried:

I tried to remove the columns then set the AutoGenerateColumns to True but it will remove the properties also.

推荐答案

不是那样的!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。您是否定期进行备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


正如MSDN文档所述 DataGridView.AutoGenerateColumns属性 [ ^ ]获取或设置一个值,指示是否自动创建列设置DataSource或DataMember属性时。



因此,如果要保留列的自定义集合及其属性,则必须使用自定义方法来完全填充一个DataGridView组件。

您可以使用: Rows.Add [ ^ ]方法。



关于你的评论to OriginalGrif的回答:

As MSDN documentation states DataGridView.AutoGenerateColumns Property[^] gets or sets a value indicating whether columns are created automatically when the DataSource or DataMember properties are set.

So, if you want to keep your custom collection of columns with their properties, you have to use custom method to full fill a DataGridView component.
You can use: Rows.Add[^] method.

As to your comment to OriginalGrif's answer:
引用:

是的,我知道这个,该查询仅供样本使用。从SQL注入开始,我的原始查询更长且更安全。但我的问题是将查询中的数据显示到datagrid而不更改标题名称/属性。好吧,谢谢你,无论如何也会帮助别人。

Hi, yes I know this, that query is for sample only. My original query is longer and safe from SQL injection. But my problem here is about displaying the data from my query to datagrid without changing the header names/properties. Well, thanks for this anyway, will help others also.



不,你的查询不安全,容易受到SqlInjection攻击。请仔细阅读OG回答。





正确的方法是使用参数!请参阅:


No, your query is not safe and is vulnerable to SqlInjection attacks. Please, read OG answer carefully.


A proper way is to use parameters! See:

Function GetMyData(sql As String, parameters As List<SqlParameter>)
dt As DataTable = New DataTable()

Using connection As SqlConnection = New SqlConnection(connectionstring)
    connection.Open()
    Using command As SqlCommand = New SqlCommand(sql, connection)
        For Each p As SqlParameter
            command.Parameters.Add(p);
        Next 'p
        Using sqlReader As SqlDataReader = command.ExecuteReader()
            dt.Load(sqlReader)
        End Using 'reader
    End Using 'command
End Using 'connection





用法:



Usage:

sql = "Select col1,col2,col3 from tbl_detailrec WHERE tc_no =@tc_no order by col1"
Dim parameters As List<SqlParameter> = New List<SqlParameter>()
parameters.Add(New SqlParameter("@tc_no", SqlDbType.VarChar).Value="some_value")
'or: Dim parameters As List<SqlParameter> = New List<SqlParameter>(){New SqlParameter("@tc_no", SqlDbType.VarChar).Value="some_value"}
 

'clear rows
DataGridView1.Rows.Clear()

Dim dt As DataTable = GetMyData(sql, parameters)
For Each r As DataRow in dt.Rows
    DataGridView1.Rows.Add(r)
Next


你说两个数据行集是兼容的。



在这种情况下,只需添加/替换/合并/无论第一个数据中的行表(绑定到网格)与第二个数据表/查询中的行。
You're saying the two "data row" sets are compatible.

In that case, just add / replace / merge / whatever the rows in the first "data table" (bound to the grid) with the rows from the second data table / query.


这篇关于如何在不更改标题名称的情况下填充数据网格。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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