DataView as DataSource

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm using VS2003. I am trying to use the FindRows method to create something
with the returned rows that I can use as a datasource for a list box. I am
storing the returned rows of the FindRows in a DataRowView array. What can i
do to be able to change this into something I can use as a DataSource. (A
dataView for instance).

Here is the code:
DataView dv = new DataView(dsMaster.Customers);

string filterString = "CompanyName LIKE 'a%'";


DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Create something to use as a Datasource// This doesn't work

DataView newView = new DataView();

// Iterate thru the array of rowViews
foreach (DataRowView rowView in drv)
{
newView.Table.Rows.Add(rowView); // must convert it somehow
filteredTable.Rows.Add( );
}

lstBox.DataSource = whateverKindOfDataSource;

Thanks for your help in advance
Ant
 
You can use that LIKE expression eith DataView's RowFilter property that is:
DataView dv = new DataView(dsMaster.Customers);
dv.RowFilter = "CompanyName LIKE a%";
lstBox.DataSource = dv;
 
HI Kerem, Thanks but...

I'm actually trying to accustomize myself to the use of the FindRows method.
I understand this can be done with RowFilter, but I believe it is more
efficient with the FindRows as it doesn't rebuild the index. That aside, I'd
just like to know how I could do this. Any ideas. (Thanks kindly so far)

Ant

Kerem OZMAN said:
You can use that LIKE expression eith DataView's RowFilter property that is:
DataView dv = new DataView(dsMaster.Customers);
dv.RowFilter = "CompanyName LIKE a%";
lstBox.DataSource = dv;

Ant said:
Hi,
I'm using VS2003. I am trying to use the FindRows method to create
something
with the returned rows that I can use as a datasource for a list box. I am
storing the returned rows of the FindRows in a DataRowView array. What can
i
do to be able to change this into something I can use as a DataSource. (A
dataView for instance).

Here is the code:
DataView dv = new DataView(dsMaster.Customers);

string filterString = "CompanyName LIKE 'a%'";


DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Create something to use as a Datasource// This doesn't work

DataView newView = new DataView();

// Iterate thru the array of rowViews
foreach (DataRowView rowView in drv)
{
newView.Table.Rows.Add(rowView); // must convert it somehow
filteredTable.Rows.Add( );
}

lstBox.DataSource = whateverKindOfDataSource;

Thanks for your help in advance
Ant
 
Ok this works butI'm not so sure about performance gains and losses.
How to create a table from DataRowView[]:

//..
DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Assume the DataTable on which you created dv is named myTable.
// We are going to copy the drv rows into copyTable and copyTable
// should have the same structure as the source table i.e myTable
DataTable copyTable = myTable.Clone();
foreach(DataRowView rowView in drv)
{
copyTable.ImportRow(rowView.Row);
}

So now you have a DataTable named copyTable which has the found rows of drv
and you can use it as a data source. As a side note you can actually use
arrays as datasources but Array items should expose some (public) properties
to use as display and value members but in this case I was not able to
display drv's individual item names on a listbox in anyway. Oh could you
please tell me what exactly you meant by DataView.RowFilter and rebuilding
the index?
Ant said:
HI Kerem, Thanks but...

I'm actually trying to accustomize myself to the use of the FindRows
method.
I understand this can be done with RowFilter, but I believe it is more
efficient with the FindRows as it doesn't rebuild the index. That aside,
I'd
just like to know how I could do this. Any ideas. (Thanks kindly so far)

Ant

Kerem OZMAN said:
You can use that LIKE expression eith DataView's RowFilter property that
is:
DataView dv = new DataView(dsMaster.Customers);
dv.RowFilter = "CompanyName LIKE a%";
lstBox.DataSource = dv;

Ant said:
Hi,
I'm using VS2003. I am trying to use the FindRows method to create
something
with the returned rows that I can use as a datasource for a list box. I
am
storing the returned rows of the FindRows in a DataRowView array. What
can
i
do to be able to change this into something I can use as a DataSource.
(A
dataView for instance).

Here is the code:
DataView dv = new DataView(dsMaster.Customers);

string filterString = "CompanyName LIKE 'a%'";


DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Create something to use as a Datasource// This doesn't work

DataView newView = new DataView();

// Iterate thru the array of rowViews
foreach (DataRowView rowView in drv)
{
newView.Table.Rows.Add(rowView); // must convert it somehow
filteredTable.Rows.Add( );
}

lstBox.DataSource = whateverKindOfDataSource;

Thanks for your help in advance
Ant
 
Hello Kerem,
Thanks for your answer. I appreciate it. As for the performance gains, I'm
not sure. I just read it & thought it mght be a good thing to be aware of. I
don't know the legitimacy of it though. Here is the information quoted:
"
It is more efficient to use the FindRows method over the RowFilter. The
RowFilter property returns multiple rows if more than one row matches the
search critera, but it is not the most efficient manner because it requires
the DataViews index to be rebuilt.
"
Don't know. The part about the RowFilter "returning" multiple rows sounds
strange as it doesn't *return* anything; it restricts the rows of the
Dataview. But anyway...
It's good to know how to turn the array into a Datatable. Thank you.

Kerem OZMAN said:
Ok this works butI'm not so sure about performance gains and losses.
How to create a table from DataRowView[]:

//..
DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Assume the DataTable on which you created dv is named myTable.
// We are going to copy the drv rows into copyTable and copyTable
// should have the same structure as the source table i.e myTable
DataTable copyTable = myTable.Clone();
foreach(DataRowView rowView in drv)
{
copyTable.ImportRow(rowView.Row);
}

So now you have a DataTable named copyTable which has the found rows of drv
and you can use it as a data source. As a side note you can actually use
arrays as datasources but Array items should expose some (public) properties
to use as display and value members but in this case I was not able to
display drv's individual item names on a listbox in anyway. Oh could you
please tell me what exactly you meant by DataView.RowFilter and rebuilding
the index?
Ant said:
HI Kerem, Thanks but...

I'm actually trying to accustomize myself to the use of the FindRows
method.
I understand this can be done with RowFilter, but I believe it is more
efficient with the FindRows as it doesn't rebuild the index. That aside,
I'd
just like to know how I could do this. Any ideas. (Thanks kindly so far)

Ant

Kerem OZMAN said:
You can use that LIKE expression eith DataView's RowFilter property that
is:
DataView dv = new DataView(dsMaster.Customers);
dv.RowFilter = "CompanyName LIKE a%";
lstBox.DataSource = dv;

Hi,
I'm using VS2003. I am trying to use the FindRows method to create
something
with the returned rows that I can use as a datasource for a list box. I
am
storing the returned rows of the FindRows in a DataRowView array. What
can
i
do to be able to change this into something I can use as a DataSource.
(A
dataView for instance).

Here is the code:
DataView dv = new DataView(dsMaster.Customers);

string filterString = "CompanyName LIKE 'a%'";


DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Create something to use as a Datasource// This doesn't work

DataView newView = new DataView();

// Iterate thru the array of rowViews
foreach (DataRowView rowView in drv)
{
newView.Table.Rows.Add(rowView); // must convert it somehow
filteredTable.Rows.Add( );
}

lstBox.DataSource = whateverKindOfDataSource;

Thanks for your help in advance
Ant
 
Ant,

Kerem shows you that the dataview findrows returns a collection of
unformated datarows. Those you can not direct bind to a control. You have
first to add those to a datatable and if you with again to a dataview.

In my opinion do you want to use a very inefficient method.

(The Defaultview/dataview has proven to much to be the most efficient one).

Cor

Ant said:
Hello Kerem,
Thanks for your answer. I appreciate it. As for the performance gains, I'm
not sure. I just read it & thought it mght be a good thing to be aware of.
I
don't know the legitimacy of it though. Here is the information quoted:
"
It is more efficient to use the FindRows method over the RowFilter. The
RowFilter property returns multiple rows if more than one row matches the
search critera, but it is not the most efficient manner because it
requires
the DataViews index to be rebuilt.
"
Don't know. The part about the RowFilter "returning" multiple rows sounds
strange as it doesn't *return* anything; it restricts the rows of the
Dataview. But anyway...
It's good to know how to turn the array into a Datatable. Thank you.

Kerem OZMAN said:
Ok this works butI'm not so sure about performance gains and losses.
How to create a table from DataRowView[]:

//..
DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Assume the DataTable on which you created dv is named myTable.
// We are going to copy the drv rows into copyTable and copyTable
// should have the same structure as the source table i.e myTable
DataTable copyTable = myTable.Clone();
foreach(DataRowView rowView in drv)
{
copyTable.ImportRow(rowView.Row);
}

So now you have a DataTable named copyTable which has the found rows of
drv
and you can use it as a data source. As a side note you can actually use
arrays as datasources but Array items should expose some (public)
properties
to use as display and value members but in this case I was not able to
display drv's individual item names on a listbox in anyway. Oh could you
please tell me what exactly you meant by DataView.RowFilter and
rebuilding
the index?
Ant said:
HI Kerem, Thanks but...

I'm actually trying to accustomize myself to the use of the FindRows
method.
I understand this can be done with RowFilter, but I believe it is more
efficient with the FindRows as it doesn't rebuild the index. That
aside,
I'd
just like to know how I could do this. Any ideas. (Thanks kindly so
far)

Ant

:

You can use that LIKE expression eith DataView's RowFilter property
that
is:
DataView dv = new DataView(dsMaster.Customers);
dv.RowFilter = "CompanyName LIKE a%";
lstBox.DataSource = dv;

Hi,
I'm using VS2003. I am trying to use the FindRows method to create
something
with the returned rows that I can use as a datasource for a list
box. I
am
storing the returned rows of the FindRows in a DataRowView array.
What
can
i
do to be able to change this into something I can use as a
DataSource.
(A
dataView for instance).

Here is the code:
DataView dv = new DataView(dsMaster.Customers);

string filterString = "CompanyName LIKE 'a%'";


DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Create something to use as a Datasource// This doesn't work

DataView newView = new DataView();

// Iterate thru the array of rowViews
foreach (DataRowView rowView in drv)
{
newView.Table.Rows.Add(rowView); // must convert it somehow
filteredTable.Rows.Add( );
}

lstBox.DataSource = whateverKindOfDataSource;

Thanks for your help in advance
Ant
 
Hi Cor,
Agreed, it's quite a messy process & quite limited as it seems you cannot
use queries with wildcards as FindRows criteria. I was just curious as to how
I could go about getting an array into a format that I could use to bind to a
data set. What Kerem showed me solved that problem. (Basically clone the
original datatable & import the rowviews of the DataRowView array as
DataRows )

Thanks for your thoughts again
Ant

Cor Ligthert said:
Ant,

Kerem shows you that the dataview findrows returns a collection of
unformated datarows. Those you can not direct bind to a control. You have
first to add those to a datatable and if you with again to a dataview.

In my opinion do you want to use a very inefficient method.

(The Defaultview/dataview has proven to much to be the most efficient one).

Cor

Ant said:
Hello Kerem,
Thanks for your answer. I appreciate it. As for the performance gains, I'm
not sure. I just read it & thought it mght be a good thing to be aware of.
I
don't know the legitimacy of it though. Here is the information quoted:
"
It is more efficient to use the FindRows method over the RowFilter. The
RowFilter property returns multiple rows if more than one row matches the
search critera, but it is not the most efficient manner because it
requires
the DataViews index to be rebuilt.
"
Don't know. The part about the RowFilter "returning" multiple rows sounds
strange as it doesn't *return* anything; it restricts the rows of the
Dataview. But anyway...
It's good to know how to turn the array into a Datatable. Thank you.

Kerem OZMAN said:
Ok this works butI'm not so sure about performance gains and losses.
How to create a table from DataRowView[]:

//..
DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Assume the DataTable on which you created dv is named myTable.
// We are going to copy the drv rows into copyTable and copyTable
// should have the same structure as the source table i.e myTable
DataTable copyTable = myTable.Clone();
foreach(DataRowView rowView in drv)
{
copyTable.ImportRow(rowView.Row);
}

So now you have a DataTable named copyTable which has the found rows of
drv
and you can use it as a data source. As a side note you can actually use
arrays as datasources but Array items should expose some (public)
properties
to use as display and value members but in this case I was not able to
display drv's individual item names on a listbox in anyway. Oh could you
please tell me what exactly you meant by DataView.RowFilter and
rebuilding
the index?
HI Kerem, Thanks but...

I'm actually trying to accustomize myself to the use of the FindRows
method.
I understand this can be done with RowFilter, but I believe it is more
efficient with the FindRows as it doesn't rebuild the index. That
aside,
I'd
just like to know how I could do this. Any ideas. (Thanks kindly so
far)

Ant

:

You can use that LIKE expression eith DataView's RowFilter property
that
is:
DataView dv = new DataView(dsMaster.Customers);
dv.RowFilter = "CompanyName LIKE a%";
lstBox.DataSource = dv;

Hi,
I'm using VS2003. I am trying to use the FindRows method to create
something
with the returned rows that I can use as a datasource for a list
box. I
am
storing the returned rows of the FindRows in a DataRowView array.
What
can
i
do to be able to change this into something I can use as a
DataSource.
(A
dataView for instance).

Here is the code:
DataView dv = new DataView(dsMaster.Customers);

string filterString = "CompanyName LIKE 'a%'";


DataRowView[] drv = dv.FindRows("CompanyName LIKE 'a%'");

// Create something to use as a Datasource// This doesn't work

DataView newView = new DataView();

// Iterate thru the array of rowViews
foreach (DataRowView rowView in drv)
{
newView.Table.Rows.Add(rowView); // must convert it somehow
filteredTable.Rows.Add( );
}

lstBox.DataSource = whateverKindOfDataSource;

Thanks for your help in advance
Ant
 
Back
Top