[英]how to bind datagridview to database at page load time in vb.net
I use below code in try catch block but it gives exception" 'table' argument cannot be null. Parameter name: table " .My table name is caste and that table two columns are there srno and castename .But it say that my table has no data.Memory table is a datatable. 我在try catch块中使用下面的代码,但它给出了例外情况:“ 'table'参数不能为null。参数名称:table “。我的表名称为caste,该表的两列为srno和castename。但是它说我的表具有无数据。内存表是数据表。
Dim Dset As New DataSet()
Dset = New DataSet()
Dset.Tables.Add(MemoryTable)
DataGridView1.DataSource = Dset.Tables("caste")
I tried data connect with database with using datasource but it gives service pack 1 error 'One is to use data binding on your TextBox controls and assigning the same DataSource.but its gives error 我尝试使用数据源与数据库连接数据,但它给Service Pack 1错误'一个是在TextBox控件上使用数据绑定并分配相同的DataSource。但是它给错误
You don't show the definition of MemoryTable
, but you do say it's a data table. 您没有显示MemoryTable
的定义,但您确实说它是一个数据表。 If it's an object of type System.Data.DataTable
, then it will have a property called TableName
. 如果它是System.Data.DataTable
类型的对象,则它将具有一个名为TableName
的属性。
When you access a DataTable
in a DataSet
with a string index value, the value you are passing is the table's TableName
property. 当您使用字符串索引值访问DataSet
的DataTable
时,您传递的值是表的TableName
属性。 So Dset.Tables("caste")
is looking for a DataTable
whose TableName
property is set to "caste". 所以Dset.Tables("caste")
正在寻找一个DataTable
,其TableName
属性设置为“种姓”。 If it can't find one, it will return Null
. 如果找不到,它将返回Null
。 That looks like what's happening. 看起来好像发生了什么。
So set MemoryTable.TableName
to "caste" and the error may go away. 因此,将MemoryTable.TableName
设置为“ caste”,错误可能消失。
I assume that MemoryTable
actually has rows in it? 我认为MemoryTable
实际上有行吗? If not, that may be a reason why you're getting the message about your table having no data. 如果不是,那可能就是为什么您收到有关表没有数据的消息的原因。
So your code should look something like this: 因此,您的代码应如下所示:
Dim Dset As New DataSet() ' You don't have to do a separate assignment to Dset
' if you use New in the declaration, so we can omit that line.'
MemoryTable.TableName = "caste"
Dset.Tables.Add(MemoryTable)
DataGridView1.DataSource = Dset.Tables("caste")
And, actually, you can use MemoryTable
as your data source without having to add it to a DataSet
, unless you need to for some other reason. 而且,实际上,您可以将MemoryTable
用作数据源,而不必将其添加到DataSet
,除非出于其他原因需MemoryTable
。
DataGridView1.DataSource = MemoryTable
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.