Why does my Dataview NOT work!!!

A

Aaron Ackerman

Me.SqlDataAdapter1.Fill(Me.vdsVehicle)

Dim dt As New DataTable
dt = Me.vdsVehicle.Tables(0)

Dim dv As New DataView(dt)
dv.RowFilter = "VehicleYear = '1985'"


both the Dataset and the Dataview still have three records. It is not
filtered down to ONE record like it should be. Why??
 
S

Scott M.

Private Sub MakeDataView()
Dim dv As New DataView
With dv
.Table = vdsVehicle.Tables(0)
.AllowDelete = True
.AllowEdit = True
.AllowNew = True
.RowFilter = "VehicleYear = '1985'"
.RowStateFilter = DataViewRowState.ModifiedCurrent
.Sort = "someField"
End With
End Sub
 
A

Aaron Ackerman

I'm not following, I am passing my DataTable to the DataView then doing my
filter.
Can you post code that shows how to do it correctly??
 
A

Aaron Ackerman

Nope.

when I do: ?dv.Table.Rows.Count I get 3 rows when I should be getting 1
row.
 
W

William Ryan eMVP

Because the Table still has 3 rows in it. That's what I was referring to
above. Scott's code is identical to yours in and should yeild no difference
b/c the properties that are set are merely the default values or are
unrelated to count.

Try dv.Count and you should see 1. I used your code and got 1 in the first
test. Then I redid it with another table I created, did it in C# just to
show it's not the language or anything like that. The results are correct.

dv.Table.Rows.Count is going to be 3 b/c if you do dv.Table.TableName you'll
see it's dt. The rowfilter only affects the view. If you do dv.Count
you'll see it's one. I posted another snippet of my second attempt, this
time with 2 records at 1985. Works exactly as expected.

I think you're fine, it's like I said in the first post, your measurement of
Rows is the problem.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
W

William Ryan eMVP

Make sure you are checking dv.Count not the count of the rows in the table.
They aren't the same if you set a rowfilter that filters anything out.

Here's the code recreated in C#. I used a new table and everything. I
already did it with your code and verified that it's the way you are
counting rows, but if you follow my comments, it should clear it up. Note
that this pass I have 2 rows with 1985.
private void button6_Click(object sender, System.EventArgs e)

{

//Created a Table "TestTable" in NorthWind

//With 5 records total. Two Columns, VehicleYear

//and IDCode - IDCode does nothing but serve

//as a dummy Key so I can add multiple years to test this

//properly. Two instances of 1985 are currently in the db

//As expected, after applying the filter, 2 records exist

//in the DataView (dv), 5 in the DataTable (dt or
dsTest.Tables[0]

DataSet dsTest = new DataSet();

sqlDataAdapter4.Fill(dsTest, "TestTable");

DataTable dt = dsTest.Tables[0];

DataView dv = dt.DefaultView;

MessageBox.Show(dt.Rows.Count.ToString());//5

dv.RowFilter = "VehicleYear = '1985'";

MessageBox.Show(dv.Count.ToString()); //2

}


--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 

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