How to Get DataSet ot DataTable from DataGrid

N

neeraj

Hi
Every body
I m not able to get dataset or datatable from datagrid.
Now I m trying this code but this is always returns nothing.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim tbl As New DataTable
Dim ds As New DataSet
Dim dr As DataRow

tbl.Columns.Add("Col1")
tbl.Columns.Add("Col2")
dr = tbl.NewRow

dr("Col1") = "Neeraj"
dr("Col2") = "Pankaj"

tbl.Rows.Add(dr)
ds.Tables.Add(tbl)

DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim ds As DataSet
ds = CType(DataGrid1.DataSource, DataSet)
DataGrid2.DataSource = ds
DataGrid2.DataBind()
End Sub

Command button1 working successfully but command button2 not working
I got ds = nothing when I execute command button2 code
Please help me.
 
B

Brian Tkatch

neeraj said:
Hi
Every body
I m not able to get dataset or datatable from datagrid.
Now I m trying this code but this is always returns nothing.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim tbl As New DataTable
Dim ds As New DataSet
Dim dr As DataRow

tbl.Columns.Add("Col1")
tbl.Columns.Add("Col2")
dr = tbl.NewRow

dr("Col1") = "Neeraj"
dr("Col2") = "Pankaj"

tbl.Rows.Add(dr)
ds.Tables.Add(tbl)

DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim ds As DataSet
ds = CType(DataGrid1.DataSource, DataSet)
DataGrid2.DataSource = ds
DataGrid2.DataBind()
End Sub

Command button1 working successfully but command button2 not working
I got ds = nothing when I execute command button2 code
Please help me.

Did you click button two after button one? Since the first button sets
DataGrid1's DataSource, the second button canot refer to it until after
the first button is clicked.

Check right after "ds = CType(DataGrid1.DataSource, DataSet)".

Also, the DataBind may be gratuitous. So far, i have found no need for
them in my project.

B.
 
N

neeraj

Brian said:
Did you click button two after button one? Since the first button sets
DataGrid1's DataSource, the second button canot refer to it until after
the first button is clicked.

Check right after "ds = CType(DataGrid1.DataSource, DataSet)".

Also, the DataBind may be gratuitous. So far, i have found no need for
them in my project.

B.
Boyishly i Click button2 after button1 click , but button2 click not
get datasource from datagrid. and also Datagrid1.Datasource returns
nothing .
 
B

Brian Tkatch

neeraj said:
Boyishly i Click button2 after button1 click , but button2 click not
get datasource from datagrid. and also Datagrid1.Datasource returns
nothing .

That is odd. It looks like somehow Datagrid1.Datasource is being reset.

Add a breakpoint to you code, and watch what DataGrid1.DataSource is
set to. The one line of code

DataGrid1.DataSource = ds

sets it, but maybe it gets reset before the end of the sub?

B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top