如何获取数据表中数据行的列值
我有一个 DataTable ,它在我的代码中绑定到 dataGridview 。
当我从数据库获取数据时,网格视图工作正常。它充满了数据。 我想要做的是获取指定行的主键值。
例如:
PK Address Phone
1 xxxyyyzzz... 1234567
2 aaabbbccc... 2345678
我想要的是这个。用户将单击任意行,然后单击按钮添加一些内容。 然后我就得到PK值,然后做其他的事情。
我该怎么做呢?
I have a DataTable
which is bind to dataGridview in my code.
When I get datas from database, the gridview works fine. it fills with data.
What I want to do is getting the primary key value of the specified row.
Ex:
PK Address Phone
1 xxxyyyzzz... 1234567
2 aaabbbccc... 2345678
What I want is this. User will click on any row, then click on a button to add some stuff.
Then I will get PK value, and do other stuffs.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,你能具体说明如何以及何时获取这个值吗?
你可以这样做:
但我不确定这样做的意义是什么,如果你已经有了指定的行,为什么不能通过
row[columnName]
获得PK?但这比仅使用 for 循环循环更耗费资源。
您在绑定到 dataGridView 之前还是之后需要这个?如果您需要从 dataGridView 行获取不同的值。
对于您的编辑:
您可以像这样获取选定的行:
如果您通过其他方法(例如使用复选框选择行)来执行此操作,则可以执行以下操作:
Hmm can you specify how and when you are talking about getting this value?
You can do this:
But I am not sure what the point of that would be, if you already have the specified row, why can't you just get the PK via
row[columnName]
?But this is more resource intensive than just looping through with a for loop.
Do you need this before or after binding to the dataGridView? If you need to get the value from the dataGridView row that is different.
For your edit:
You would can get the selected row like so:
If you are doing it by some other method like using a checkbox to select a row, you would do this:
DataGridView 控件使用 DataView 类绑定到 DataTable。您可以使用类似的方法来获取 DataGridViewRow 的 DataRow,
其中 12 是您要查找的索引。有时,将 DataSource 属性显式设置为 DataView 并在表单上保留对其的引用会有所帮助。视图也会知道数据的排序
The DataGridView control binds to a DataTable using the DataView class. You can use something like this to get the DataRow of a DataGridViewRow
where 12 is the index you are looking for. Sometimes it helps to set the DataSource property to a DataView explicitly and keep a reference to it on your form. The view will know the sorting of the data too