Datagridview单元位置 [英] Datagridview cell location

查看:166
本文介绍了Datagridview单元位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道datagridview单元的位置?...如何编辑从DB获取的gridview值... Plz建议我...

Hi, how can i know the datagridview cell location?...How can i edit the gridview values fetched from DB...Plz suggest me...

推荐答案

在数据网格视图中,您可以直接使用窗体上的控件来编辑单元格中的值,如果绑定了数据,它应该自动更新DB.

要以编程方式进行编辑,如果您知道要检索/编辑的内容的X和Y位置,则可以使用:
With data grid views you can edit the values in cells directly using the control on the form, and it should automatically update the DB if it''s data bound.

To edit programmatically, if you know the X and Y positions of what you''re trying to retrieve/edit, you can use:
using System.Windows.Forms;

// Get the value
object obj = myDataGridView[X, Y].Value

// Set the value
myDataGridView[X, Y].Value = obj;



这将允许您直接访问该值.


但是,您无法始终明确声明要检索的内容.如果用户在数据网格视图上选择一行,则可以使用:



This will allow you to access the value directly.


However, you can''t always explicitly state what you want to retrieve. If the user selects a row on the data grid view you can use:

// This retrieves the current row selected on the data grid view
DataGridViewRow row = myDataGridView.CurrentRow

// This gets the cell objects for the row
DataGridViewCellCollection cells = row.Cells;

// This gets the value stored for the column with header ''ColumnName'' on the current row
object obj = cells["ColumnName"].Value;

// Use this to get the value in one line
object obj = myDataGridView.CurrentRow.Cells["ColumnName"].Value;

// Use this to set the value in one line
myDataGridView.CurrentRow.Cells["ColumnName"].Value = obj;


这篇关于Datagridview单元位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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