Click here to Skip to main content
15,920,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)


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
Posted
Updated 18-Aug-10 23:32pm
v2

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
 
Share this answer
 
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))
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900