Home All Groups Group Topic Archive Search About

DataGrid Bind to DataSet, then Bind to DaTaview, GOT ERROR...PLS HELP

Author
11 Apr 2005 7:36 AM
A_PK
I have problem databinding the DataGrid with DataView/DataSet after the
filter...
I create the following proceudre in order for user to filter as many as they
want, but the following code is only allow user to filter the first time,
when they tried the second time, the speficied cast error message will
prompt one....

I create a mydataset1 first, and the mydataset1 data source was getting from
DataGrid.DataSource.

then I create a DataView in order for me to filter....
after I enter the criteria, then will filter successfully the first time,
but not second time.

the I bind the DataGrid to DataView together......

The first filter is always successfully, I just can not filter the same data
second time...
I suspect it the DataView and DataSet problem, because at first I bind
datagrid1 and get the data source of datagrid1 form Dataset, but at the end
I bing the Datagird to the Dataview also.....so there are some error....i
think....

I just have no idea how to bind the Datagrid with Dataview, and allow user
to filter as many time as they want.....
could someone pls guide me.... here are the CODE


Try
            Dim mydataset1 As New DataSet
            mydataset1 = DataGrid1.DataSource

            Dim mydataview As New
DataView(mydataset1.Tables(Global.strTblName))
            Dim strInput

            strInput = InputBox("test")
            mydataview.RowFilter = "Custno = " & strInput & ""

            DataGrid1.Datasource= mydataview
             '           DataGrid1.DataSource = mydataset1
             '           DataGrid1.DataMember = GlobalDBExplorer.strTblName

        Catch err As InvalidCastException
            MsgBox(err.Message)
        Catch err As Exception

            MsgBox(err.Message)
        End Try

Author
11 Apr 2005 7:54 AM
Cor Ligthert
APK,

You only have to bind the dataview one time to your datagrid.
There cannot be more datasources.

I have the idea that you are creating everytime a new dataview and try to
bind that new one against your datagrid.

I hope this helps,

Cor
Author
11 Apr 2005 9:06 AM
A_PK
Hi Cor,

my first problem is that....I dun have a fix dataset for me to bind to
datagrid1....
It is dynamically bind to datasetA or datasetB...depend on user's choice...

so I have to create anotehr local dataset1 and set to
mydataset1 = datagrid.datasource

after i get the mydataset1, then I create Dataview....
and user allow to do filter on dataview, then after the filtering, then
datagrid1 bind to dataview.....

please note that...I am using DataGrid = myDataView....
so for other feacture, i can not get the myDataSet1 = DataGrid.DataSource,
because i can not simply quote Dateset1 = DataView..

so I wish to know how the DataView can update the Dataset1.....

if I were to allow to create only one time dataview, may I know where should
I create the DataView ?




Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:e5Gh$umPFHA.2348@tk2msftngp13.phx.gbl...
> APK,
>
> You only have to bind the dataview one time to your datagrid.
> There cannot be more datasources.
>
> I have the idea that you are creating everytime a new dataview and try to
> bind that new one against your datagrid.
>
> I hope this helps,
>
> Cor
>
>
Author
11 Apr 2005 9:18 AM
Arun
Hi,
Do it like this,

dim myDataView = myDataTable.DefaultView;

If you take DefaultView from a table and filter it with your criteria,
when you change a values in the dataview the change will pass through
to the underlying datatable as well.
So no need  to bind it once again you may comment this

DataGrid1.Datasource= mydataview

Hope this helps,
Cheers,
Arun.
www.innasite.com
Author
11 Apr 2005 9:21 AM
Cor Ligthert
APK,

You can use as much dataviews as you want, however a datagrid can only one
use one datasource.

Probably the best choise for you ia uainf the defaultview what is in every
datatable which is a dataview.

Someting as setting it every time when the user have changed.

\\\
dataset.table(x).defaultview.rowfilter = "xx = " & y & "'"
///

Than you can use one time

\\\
Datagrid1.datasource = dataset.table(x).defaultview
///

I hope this helps,

Cor
Author
11 Apr 2005 1:36 PM
A_PK
hi cor, arun...

another problem.....

how could I bind the data to myDataview or myDataset ?...

Cor, you mentioned
dataset.table(x).defaultview.rowfilter = "xx = " & y & "'"

my problem is......my dataset is nothing at the very first beginning...
I have to based on datagrid data then only can decide my dataset1...

that is the reason that I set mydataset1 = datagrid.datasource.....
after i set the mydataset1 = datagrid.datasource, then i type the one u
mentioned, and bind it accordinly what u had mentioned....but i still
getting the error "sepcialized cast error"......

because i bind the dataset based on datagrid.datasrouce.....

I just wonder if I bind it wrongly ? or anyway way to solve that problem ?
my dataset is nothing at the beginning, I get the dataset data based on
datagird data.
mydataset = Datagrid.datasource......

after getting mydataset, then I using defauly view to bind to datagrid......

but I am still getting error when i did it repeatly....



Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:OaGkyfnPFHA.1932@tk2msftngp13.phx.gbl...
> APK,
>
> You can use as much dataviews as you want, however a datagrid can only one
> use one datasource.
>
> Probably the best choise for you ia uainf the defaultview what is in every
> datatable which is a dataview.
>
> Someting as setting it every time when the user have changed.
>
> \\\
> dataset.table(x).defaultview.rowfilter = "xx = " & y & "'"
> ///
>
> Than you can use one time
>
> \\\
> Datagrid1.datasource = dataset.table(x).defaultview
> ///
>
> I hope this helps,
>
> Cor
>
>
Author
11 Apr 2005 2:18 PM
A_PK
To simply what I want to do....

I create a Dataset with a lot of infomration, then i create Dataview to
filter out all unwanted info....so that all the info in the Dataview is all
the information that I want ......

now I want to update the dataview to a dataset...cos I might perform other
update or delete command to update dataset........

pls guide me how to update DataView to Dataset ?

1- Dataset with All Info
2- create Dataview and filter out all unwanted info
3- update back Dataview to previous Dataset...

how to do that ?
so perhaps, my dataset have 1000 records, but after I did the filter, only
50 records i want...
so i update 50 back to dataset, and delete other 950 records....

hope someone to guide me...thank you....

Show quoteHide quote
" A_PK" <pk***@hotmail.com> wrote in message
news:%23L705tpPFHA.3296@TK2MSFTNGP15.phx.gbl...
> hi cor, arun...
>
> another problem.....
>
> how could I bind the data to myDataview or myDataset ?...
>
> Cor, you mentioned
> dataset.table(x).defaultview.rowfilter = "xx = " & y & "'"
>
> my problem is......my dataset is nothing at the very first beginning...
> I have to based on datagrid data then only can decide my dataset1...
>
> that is the reason that I set mydataset1 = datagrid.datasource.....
> after i set the mydataset1 = datagrid.datasource, then i type the one u
> mentioned, and bind it accordinly what u had mentioned....but i still
> getting the error "sepcialized cast error"......
>
> because i bind the dataset based on datagrid.datasrouce.....
>
> I just wonder if I bind it wrongly ? or anyway way to solve that problem ?
> my dataset is nothing at the beginning, I get the dataset data based on
> datagird data.
> mydataset = Datagrid.datasource......
>
> after getting mydataset, then I using defauly view to bind to
> datagrid......
>
> but I am still getting error when i did it repeatly....
>
>
>
> "Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
> news:OaGkyfnPFHA.1932@tk2msftngp13.phx.gbl...
>> APK,
>>
>> You can use as much dataviews as you want, however a datagrid can only
>> one use one datasource.
>>
>> Probably the best choise for you ia uainf the defaultview what is in
>> every datatable which is a dataview.
>>
>> Someting as setting it every time when the user have changed.
>>
>> \\\
>> dataset.table(x).defaultview.rowfilter = "xx = " & y & "'"
>> ///
>>
>> Than you can use one time
>>
>> \\\
>> Datagrid1.datasource = dataset.table(x).defaultview
>> ///
>>
>> I hope this helps,
>>
>> Cor
>>
>>
>
>
Author
11 Apr 2005 5:00 PM
Cor Ligthert
APK,

I made a little sample for you.

\\\Needs a datagrid and a button on a form
Private ds As New DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("APK")
        dt.Columns.Add("A")
        dt.LoadDataRow(New Object() {"1"}, True)
        dt.LoadDataRow(New Object() {"2"}, True)
        dt.LoadDataRow(New Object() {"3"}, True)
        ds.Tables.Add(dt)
        DataGrid1.DataSource = ds.Tables("APK").DefaultView
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        If ds.Tables("APK").DefaultView.RowFilter = "A = '2'" Then
            ds.Tables("APK").DefaultView.RowFilter = ""
        Else
            ds.Tables("APK").DefaultView.RowFilter = "A = '2'"
        End If
End Sub
///

I hope this helps,

Cor
Author
12 Apr 2005 12:49 AM
A_PK
Hi Cor, i understand what u mean....

but after I have done the filter, and set the DataGrid1.DataSource =
ds.Tables("APK").DefaultView
....I can perform filter feature actually. just that when I do know how to
retierve the filtered datasource and bind them to a new dataset to perform
other feature....

When I click other buttton, not the button u mentioned.....I need to know
what dataset is binding that datagrid. so in other button, i type myDataset
= Datagrid1.datasource. then i will get the specialied cast error...cos my
Datagrid.datasource is binding to a dataview, not dataset.... after I filter
all infomration liao, i have problem binding those information to other
dataset1...

u tried the following sure got problem

Declare a global dataset as dsFilter....

Try
            Dim strInput
            strInput = InputBox("test")

                dsFilter.Tables(GlobalDBExplorer.strTblName).DefaultView.RowFilter
= "ColumnName = ' & strInput & '"

            DataGrid1.DataSource =
dsFilter.Tables(GlobalDBExplorer.strTblName).DefaultView


        Catch err As InvalidCastException
            MsgBox(err.Message)
        Catch err As Exception

            MsgBox(err.Message)

End Try

I have no problem up to here.....but I want to
get the Datasource from DataGrid, then I will have problem

can i do anything like
mydataset = Datagird.datasource ?

mydataset.Tables(TableName) = DataGrid.Datasource ?

I need to get the Datasource from DataGrid from my other features....
I just no idea how to do that .....

but I have no problem if it is dataset.....
I can simply type mydataset = DataGrid.Datasource
Since I am binding to a View, i can no longer get the datasource from
datagrid1 by just typing mydataset = datagrid.datasource....

pls guide


Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:OgaYQgrPFHA.3076@TK2MSFTNGP12.phx.gbl...
> APK,
>
> I made a little sample for you.
>
> \\\Needs a datagrid and a button on a form
> Private ds As New DataSet
>    Private Sub Form1_Load(ByVal sender As System.Object, _
>    ByVal e As System.EventArgs) Handles MyBase.Load
>        Dim dt As New DataTable("APK")
>        dt.Columns.Add("A")
>        dt.LoadDataRow(New Object() {"1"}, True)
>        dt.LoadDataRow(New Object() {"2"}, True)
>        dt.LoadDataRow(New Object() {"3"}, True)
>        ds.Tables.Add(dt)
>        DataGrid1.DataSource = ds.Tables("APK").DefaultView
>    End Sub
>    Private Sub Button1_Click(ByVal sender As System.Object, _
>    ByVal e As System.EventArgs) Handles Button1.Click
>        If ds.Tables("APK").DefaultView.RowFilter = "A = '2'" Then
>            ds.Tables("APK").DefaultView.RowFilter = ""
>        Else
>            ds.Tables("APK").DefaultView.RowFilter = "A = '2'"
>        End If
> End Sub
> ///
>
> I hope this helps,
>
> Cor
>
Author
12 Apr 2005 5:25 AM
Cor Ligthert
APK,

When you want to set another datasource to a datagrid than you first have to
do.

datagrid.datasource = nothing

I hope this helps,

Cor
Author
12 Apr 2005 12:57 AM
A_PK
i still have a sub Call WriteXML...using the filtered data to write to XML

Private Sub WriteXML()
        Dim mydataset As New DataSet
        mydataset = DataGrid1.DataSource
        mydataset.WriteXml(Application.StartupPath & "\Demo.xml")

End Sub

I can simply use mydataset = DataGrid.Datasource, and get the reference, and
export as XML file.
but after I set to defauly view or use dataview, i can no longer the the
datasource references.

I just wonder how should I get the above mydataset bind to the filtered
datagrid datasource.

Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:OgaYQgrPFHA.3076@TK2MSFTNGP12.phx.gbl...
> APK,
>
> I made a little sample for you.
>
> \\\Needs a datagrid and a button on a form
> Private ds As New DataSet
>    Private Sub Form1_Load(ByVal sender As System.Object, _
>    ByVal e As System.EventArgs) Handles MyBase.Load
>        Dim dt As New DataTable("APK")
>        dt.Columns.Add("A")
>        dt.LoadDataRow(New Object() {"1"}, True)
>        dt.LoadDataRow(New Object() {"2"}, True)
>        dt.LoadDataRow(New Object() {"3"}, True)
>        ds.Tables.Add(dt)
>        DataGrid1.DataSource = ds.Tables("APK").DefaultView
>    End Sub
>    Private Sub Button1_Click(ByVal sender As System.Object, _
>    ByVal e As System.EventArgs) Handles Button1.Click
>        If ds.Tables("APK").DefaultView.RowFilter = "A = '2'" Then
>            ds.Tables("APK").DefaultView.RowFilter = ""
>        Else
>            ds.Tables("APK").DefaultView.RowFilter = "A = '2'"
>        End If
> End Sub
> ///
>
> I hope this helps,
>
> Cor
>
Author
12 Apr 2005 5:45 AM
Cor Ligthert
APK

This is a complet new question and reading in the ADONET newsgroup I now
understand what you are after and have even for that a sample. However I
think that it becomes to much confusing for people who reads those threads
in future when the questions are placed in more newsgroups and they don't
see the relation between question and answers, so I stop with answering in
this thread.

Cor
Author
12 Apr 2005 6:50 AM
A_PK
I apologize for doing that....

It is because that after I post my query, then I only realized that there is
still one adonet newgroup which might be related to my topic, so I post my
question to that newsgroup...

I dun mean do that that....

If you do know the solution, pls kindly let me know.

THank you very much


Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:uO%23oXLyPFHA.2876@TK2MSFTNGP10.phx.gbl...
> APK
>
> This is a complet new question and reading in the ADONET newsgroup I now
> understand what you are after and have even for that a sample. However I
> think that it becomes to much confusing for people who reads those threads
> in future when the questions are placed in more newsgroups and they don't
> see the relation between question and answers, so I stop with answering in
> this thread.
>
> Cor
>
Author
12 Apr 2005 7:04 AM
Arun
Hi APK,

Here is a sample code snippet, Thanks Cor for the code and I have
changed a little to work for your requirement,

Try this code.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Try
            Dim dt As New DataTable("APK")
            dt.Columns.Add("A")
            dt.LoadDataRow(New Object() {"1"}, True)
            dt.LoadDataRow(New Object() {"2"}, True)
            dt.LoadDataRow(New Object() {"3"}, True)
            ds.Tables.Add(dt)
            DataGrid1.DataSource = dt
        Catch err As InvalidCastException
            MsgBox(err.Message)
        Catch err As Exception
            MsgBox(err.Message)
        End Try


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim ds1 As New DataSet
        Dim dp As New DataTable
        Dim i As Int32
        dp = ds.Tables("APK").Clone
        Dim dt() As DataRow = ds.Tables("APK").Select("A = '2'")
        'Use ImportRow method to copy from Products table to its clone.
        For i = 0 To dt.Length - 1
            dp.ImportRow(dt(i))
        Next
        ds1.Tables.Add(dp)
        DataGrid1.DataSource = dp
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        Dim i As Int32
        Dim dt1 As New DataTable
        dt1 = CType(DataGrid1.DataSource, DataTable)
        dt1.DataSet.WriteXml("\Demo.xml")
    End Sub

Hope this helps,

Cheers,
Arun.
www.innasite.com
Author
12 Apr 2005 8:00 AM
Cor Ligthert
Arun,

I readed in the ADONET newsgroup that this is probably what APX is needed.

However as you did, is this kind of answering confusing on questions in more
newsgroups.

(Changed a lot in this message so watch typos)

\\\
Dim dsNew as new dataset
Dim dv As New DataView(OriginalDataset.Tables("whatever"))
dv.RowFilter = "A = '1"
Dim dtnew As DataTable = OriginalDataset.Tables("whatever").Clone
For Each dvr As DataRowView In dv
dtnew.ImportRow(dvr.Row)
Next
dt.Clear()
dsNew.Tables.Add(dtnew)
dsNew.WriteXML("C:\mydataset.xml")
///

:-)

Cor
Author
12 Apr 2005 9:07 AM
A_PK
Hi Arun,

thank for the code...

another question...just wonder if there is a way to bind the datagrid back
to dataset instead of dataview and datatable ?

because if I do filter, then have to bind my datagrid to datatable or
dataview,
if I dun do filter, then my datagrid will bind to dataset....

Without Filter....following is my WriteXML code

                Dim mydataset As New DataSet
               mydataset = DataGrid1.DataSource
               mydataset.WriteXmlSchema(Application.StartupPath &
"\DBexplorer\Demo.xsd")
               mydataset.WriteXml(Application.StartupPath &
"\DBexplorer\Demo.xml")

WithFilter...following is my WriteXML code....

        Dim dt1 As New DataTable
        dt1 = CType(DataGrid1.DataSource, DataTable)
        dt1.DataSet.WriteXml(Application.StartupPath &
"\DBexplorer\\Demo.xml")


so if I have done filter, then the code gave u will be working,
if i have not done filter, then the code u gave me will not be wokring....
so they all get datagrid.datasource from different source. one from Dataset,
one from either Dataview and DataTable...

how could I make all consistent ?

Show quoteHide quote
"Arun" <arunkumarselva***@gmail.com> wrote in message
news:1113289477.751943.106900@z14g2000cwz.googlegroups.com...
> Hi APK,
>
> Here is a sample code snippet, Thanks Cor for the code and I have
> changed a little to work for your requirement,
>
> Try this code.
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>        Try
>            Dim dt As New DataTable("APK")
>            dt.Columns.Add("A")
>            dt.LoadDataRow(New Object() {"1"}, True)
>            dt.LoadDataRow(New Object() {"2"}, True)
>            dt.LoadDataRow(New Object() {"3"}, True)
>            ds.Tables.Add(dt)
>            DataGrid1.DataSource = dt
>        Catch err As InvalidCastException
>            MsgBox(err.Message)
>        Catch err As Exception
>            MsgBox(err.Message)
>        End Try
>
>
>    End Sub
>
>    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>        Dim ds1 As New DataSet
>        Dim dp As New DataTable
>        Dim i As Int32
>        dp = ds.Tables("APK").Clone
>        Dim dt() As DataRow = ds.Tables("APK").Select("A = '2'")
>        'Use ImportRow method to copy from Products table to its clone.
>        For i = 0 To dt.Length - 1
>            dp.ImportRow(dt(i))
>        Next
>        ds1.Tables.Add(dp)
>        DataGrid1.DataSource = dp
>    End Sub
>
>    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button2.Click
>        Dim i As Int32
>        Dim dt1 As New DataTable
>        dt1 = CType(DataGrid1.DataSource, DataTable)
>        dt1.DataSet.WriteXml("\Demo.xml")
>    End Sub
>
> Hope this helps,
>
> Cheers,
> Arun.
> www.innasite.com
>
Author
12 Apr 2005 11:52 AM
Arun
APK,

With or without filter

Dim dt1 As New DataTable
dt1 = CType(DataGrid1.DataSource, DataTable)
dt1.DataSet.WriteXml (Application.StartupPath &
"\DBexplorer\\Demo.xml")

This should have to work, Have you tried it!!!

Cheers,
Arun.
Author
13 Apr 2005 3:27 AM
A_PK
Well,,, thank for Arun and Cor...

Show quoteHide quote
"Arun" <arunkumarselva***@gmail.com> wrote in message
news:1113306732.265672.90320@l41g2000cwc.googlegroups.com...
> APK,
>
> With or without filter
>
> Dim dt1 As New DataTable
> dt1 = CType(DataGrid1.DataSource, DataTable)
> dt1.DataSet.WriteXml (Application.StartupPath &
> "\DBexplorer\\Demo.xml")
>
> This should have to work, Have you tried it!!!
>
> Cheers,
> Arun.
>