PB之取下来列修改后的值(AcceptText)

在PowerBuilder中,我们可以使用DataWindow对象来显示数据库中的数据,并对表格中的数据进行修改、删除和添加等操作。当用户修改完某一行的数据之后,我们需要获取该行数据的新值,以便进行后续的处理。本篇文章将介绍如何通过PowerBuilder的数据窗口代码,获取修改后的表格数据,并提供配有案例说明的详细操作流程。

使用DataWindow获取修改后的数据

在PowerBuilder中,DataWindow使用一个数据源来显示数据库的数据,在用户对表格数据进行修改后,我们可以通过Datawindow对象获取修改后的数据值。接下来让我们通过下面的步骤进行操作:

1. 首先,我们需要获取DataWindow对象。这个对象通常在界面布局中已经定义好了。假设我们的DataWindow对象名为d_my_table(如果你使用的是PB2019以前的版本,可能需要手工创建DataWindow对象)。

2. 接着,我们需要定义一个DataStore对象,用于和DataWindow对象进行交互。定义DataStore对象的代码如下:

datastore ds_my_table

ds_my_table=create datastore

ds_my_table.dataobject='d_my_table'

3. 现在,我们需要连接到数据库并获取数据。为此,我们使用SQLCA对象的连接功能进行连接。建立连接后,我们使用Retrieve方法获取数据:

SQLCA.DBMS='oracle'

SQLCA.database='my_db'

SQLCA.servername='my_server'

SQLCA.userid='my_user'

SQLCA.password='my_password'

if SQLCA.Connect() = -1 then

MessageBox("Error",SQLCA.SQLERRTEXT)

else

ds_my_table.Retrieve()

end if

4. 获取数据之后,我们可以对数据进行修改。修改数据的代码可以使用DataStore对象的SetItem方法,在其中指定要修改的行、列以及新值。例如,我们可以使用以下代码将第1行、第2列的值改为"New Value":

ds_my_table.SetItem(1,2,"New Value")

5. 在修改数据之后,我们需要获取新的数据值。可以使用Datawindow对象的Describe方法获取Datawindow的结构,包括每一列的名称和数据类型等信息。之后,我们就可以使用GetItem方法获取每一列的新值了:

datastore ds_structure

ds_structure = CREATE datastore

ds_structure.dataobject = 'd_my_table'

ds_structure.Describe("DataWindow.ColumnCount")

li_columns = Integer(ds_structure.Describe("DataWindow.ColumnCount"))

for li_column = 1 to li_columns

ls_column_name = ds_structure.Describe("#" + string(li_column) + ".Name")

ls_column_type = ds_structure.Describe("#" + string(li_column) + ".ColType")

ls_column_edit = ds_structure.Describe("#" + string(li_column) + ".EditStyle")

// get the new value

ls_new_value = String(ds_my_table.GetItem(ls_column_name,ds_my_table.Row), ls_column_type)

next

6. 最后,我们将获取的修改后的表格数据值用于后续的处理。

案例说明

以下是一个简单的案例,通过PowerBuilder获取修改后的表格数据值:

假设我们的数据库中有一个表格名为db_user,其中包含以下列:user_id,user_name,user_email和user_phone。

我们创建一个DataWindow对象d_user,并在其中添加这4列,其中user_id列设置为只读。

接着,我们添加一个按钮,并在其单击事件中添加以下代码:

datastore ds_user

ds_user=create datastore

// connect to database

SQLCA.DBMS='oracle'

SQLCA.database='my_db'

SQLCA.servername='my_server'

SQLCA.userid='my_user'

SQLCA.password='my_password'

if SQLCA.Connect() = -1 then

MessageBox("Error",SQLCA.SQLERRTEXT)

else

ds_user.dataobject='d_user'

ds_user.Retrieve()

// modify data

ds_user.SetItem(1,'user_name','New User Name')

// get the new value

datastore ds_structure

ds_structure = CREATE datastore

ds_structure.dataobject = 'd_user'

ds_structure.Describe("DataWindow.ColumnCount")

li_columns = Integer(ds_structure.Describe("DataWindow.ColumnCount"))

for li_column = 1 to li_columns

ls_column_name = ds_structure.Describe("#" + string(li_column) + ".Name")

ls_column_type = ds_structure.Describe("#" + string(li_column) + ".ColType")

ls_column_edit = ds_structure.Describe("#" + string(li_column) + ".EditStyle")

// get the new value

ls_new_value = String(ds_user.GetItem(ls_column_name,ds_user.Row), ls_column_type)

MessageBox("New value for " + ls_column_name, ls_new_value)

next

end if

这个例子会首先连接到数据库,并获取db_user表格的所有数据。然后,他会将第一行第二列(即user_name)的值改为"New User Name"。接着,它会获取修改后每一列的值,并弹出一个消息框显示这些值。 通过这个案例,我们可以看到如何使用PowerBuilder获取修改后的表格数据值。

壹涵网络我们是一家专注于网站建设、企业营销、网站关键词排名、AI内容生成、新媒体营销和短视频营销等业务的公司。我们拥有一支优秀的团队,专门致力于为客户提供优质的服务。

我们致力于为客户提供一站式的互联网营销服务,帮助客户在激烈的市场竞争中获得更大的优势和发展机会!

点赞(54) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部