Home All Groups Group Topic Archive Search About

How to Get DataSet ot DataTable from DataGrid

Author
7 Jul 2006 4:35 PM
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.

Author
7 Jul 2006 5:29 PM
Brian Tkatch
neeraj wrote:
Show quoteHide quote
> 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.
Author
9 Jul 2006 8:43 AM
neeraj
Brian Tkatch wrote:
Show quoteHide quote
> neeraj wrote:
> > 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.
Boyishly i Click button2 after button1 click ,  but button2 click not
get datasource from datagrid. and also Datagrid1.Datasource returns
nothing .
Author
10 Jul 2006 2:27 PM
Brian Tkatch
neeraj wrote:
Show quoteHide quote
> Brian Tkatch wrote:
> > neeraj wrote:
> > > 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.
> 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.