Gridview数据源 [英] Gridview Datasource

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

问题描述

你好!

我的页面上有一个gridview,它绑定到asp.net页面的page_load上的数据集.我在网格中也有一个编辑按钮.

在遇到这种情况之前,单击更新"按钮,我会将更改保存在数据库本身中,然后再次从db中检索数据集,然后绑定到gridview.效果很好.

我想做的是:

我不想直接在数据库中进行更改.我想将更改保存在数据集中.然后,经过一些检查,然后将其保存在数据库中.我无法在gridview数据源中进行更改:

如果我写:

Hi there!

I have a gridview on my page, which I''m binding to a dataset on page_load of the asp.net page. I have an edit button in the grid as well.

Prior to my situation, on clicking the "update" button, I would save the changes in the database itself and then retrieve the dataset again from the db and then bind to the gridview. This works fine.

What I want to do is:

I do not want to make the changes in the database directly. I want to save the changes in a dataset. Then later, after some checks, save it the db. I am not able to make changes in the gridview datasource:

If I write:

Dim ds as dataset = ctype(gridview1.datasource,dataset)



它引发空引用异常.

我进行了调试,并注意到数据源始终设置为"Nothing".如何从gridview提取数据集,对其进行更改,然后再次将其绑定到gridview?

感谢



It throws an exception of null reference.

I did the debugging, and noticed that the datasource is always set to "Nothing". How do i extract the dataset from the gridview, make changes to it and then bind it again to the gridview?

Thanks

推荐答案

对于每个项目,您必须从网格中检索数据并将其保存在数据表中...

昏暗的dtGridContent作为DataTable
dtGridContent =新数据表
将drGridContent变暗为DataRow


试试

dtGridContent.Columns.Add(0)
dtGridContent.Columns.Add(1)
dtGridContent.Columns.Add(2)


对于gridview1.Items
中的每一行 drGridContent = dtGridContent.NewRow()
drGridContent(0)= CType(row.Cells(0).FindControl("cntrl1"),HtmlInputText).Value
drGridContent(1)= CType(row.Cells(1).FindControl("cntrl2"),HtmlInputText).Value
drGridContent(2)= CType(row.Cells(2).FindControl("cntrl3"),HtmlInputText).Value
dtGridContent.Rows.Add(drGridContent)
下一个

//在此处将dtGridContent保存在您的数据集中
返回True

异常捕获
返回False
结束尝试
for each item u have to retrieve the data from the grid and save it in the data table........

Dim dtGridContent As DataTable
dtGridContent = New DataTable
Dim drGridContent As DataRow


Try

dtGridContent.Columns.Add(0)
dtGridContent.Columns.Add(1)
dtGridContent.Columns.Add(2)


For Each row In gridview1.Items
drGridContent = dtGridContent.NewRow()
drGridContent(0) = CType(row.Cells(0).FindControl("cntrl1"), HtmlInputText).Value
drGridContent(1) = CType(row.Cells(1).FindControl("cntrl2"), HtmlInputText).Value
drGridContent(2) = CType(row.Cells(2).FindControl("cntrl3"), HtmlInputText).Value
dtGridContent.Rows.Add(drGridContent)
Next

// save dtGridContent in ur dataset here
Return True

Catch ex As Exception
Return False
End Try


Dim str As String ="Driver = {Microsoft Access驱动程序(* .mdb)}; Dbq = E:\\ mohan \\ birthday_reminder \\ Send_Bitrhday_emails.mdb;"
昏暗的状态作为新的OdbcConnection()
con.ConnectionString = str
con.Open()
昏暗的cmd作为新的OdbcCommand"select * from Emp_Info",con)
将Dim广告作为新的OdbcDataAdapter(cmd)
将Dim ds用作新数据集
ad.Fill(ds)
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()``直到这里我首先显示实际数据(在ur DB中可用的数据)
ds.Tables(0).Rows.Add("6787","yourName","yourEmail","9")''将您的行添加到ds中可用的表
''现在按照您的要求进行验证(我还没有完成)
''验证完成后,将代码推送到数据库中(下面两行对u有用)
Dim bl As New OdbcCommandBuilder(ad)
ad.Update(ds.Tables(0))
Dim str As String = "Driver={Microsoft Access Driver (*.mdb)};Dbq=E:\\mohan\\birthday_reminder\\Send_Bitrhday_emails.mdb;"
Dim con As New OdbcConnection()
con.ConnectionString = str
con.Open()
Dim cmd As New OdbcCommand"select * from Emp_Info",con)
Dim ad As New OdbcDataAdapter(cmd)
Dim ds As New DataSet
ad.Fill(ds)
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind() ''Till here i m jst displying the actual data(Data which is avail in ur DB)
ds.Tables(0).Rows.Add("6787","yourName","yourEmail","9")''add ur row to the table avail in ds
''Now do the validation as per ur requirement(Which i have nt done)
''Once validation is done , Push the code into the DB(Following 2 lines ll do for u)
Dim bl As New OdbcCommandBuilder(ad)
ad.Update(ds.Tables(0))


这篇关于Gridview数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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