Visual Basic .net, Limit rows in Gridview

I am having trouble trying limit the rows returned in a datagridview. Everytime I try to use it, it only returns the whole table. I just want to limit it to certains rows based on values in a column.

I have tried to alter the SQL but to no avail. During setup the datasource always seems to default to a whole table.
jonsuns7Asked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
Are you using typed datasets (dragged and dropped on form)? You can try following on page load

Dim dbadp As New SqlDataAdapter("Select ... Where GoalID=" & GoalHolder, "connection string here")
Dim dTable As New DataTable
dbadp.Fill(dTable)
dbadp.Dispose()

DataGridView1.DataSource = dTable
0
 
CrashmanCommented:
0
 
jonsuns7Author Commented:
Thanks Crashman,
In a nutshell, I am passing a value from one form to another (that part I was able to do), I copied the value into a variable and a label on the second form to make sure it was passed.

What I want to do know is when the form opens, I would like for the rows returned in the datagridview to be limited to only contain those with the value in the variable (or label). The name of of the field in the table is "GOAL_ID".

Thanks,

Here is some of the SQL code I am using:

SELECT        GoalID, GoalDescription, LOB, LOBProgramArea, GoalCompleted, GoalCompletionDate, GaolComplSignature, GoalComplMgrAppr, GoalYear
FROM            WorkPlanGoals
WHERE        (GoalID = N'&GoalHolder')


In this case GoalHolder is a variable

I have the same value in a label marked  GoalIDLabel

I just need the SQL to key in on either the variable value or the text of the label.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CrashmanCommented:
still without understand, what is the error?, whats is the real problem, is the query?

Dim SQL as String = "SELECT        GoalID, GoalDescription, LOB, LOBProgramArea, GoalCompleted, GoalCompletionDate, GaolComplSignature, GoalComplMgrAppr, GoalYear
FROM            WorkPlanGoals
WHERE        (GoalID = " & GoalHolder &")"
0
 
jonsuns7Author Commented:
Okay, let me try again. I have a form, lets call it Form1. I select a row from a datagridview in that form (actually, I select a single row).

I pass the primary key value for that row from Form1,  to another form  Form2. (I have confirmed this by using both a variable and a label that shows the value brought over from Form1.)

The problem I am having, is that I want the datagridview in Form 2 to display only that row based on that primary key value.
0
 
jonsuns7Author Commented:
I'm using the Query builder for the DataSet of the DataGridView.

I just need for it to be able to read for the where clause of the SQL Statement from a variable (or from the label text) whatever gets the job done.

Thanks,
0
 
CodeCruiserCommented:
I know you already have designer generated stuff but I like it really easy to use an approach like http:#a38425464
0
 
CrashmanCommented:
this

and

this

is almost the same, the difference is in the second you write the query directly in the adapter
the first is in a variable, why you can't use any of this, where is the error, whats is the real problema, i dont understand any yet, can you post your code where you wish this behavior
0
 
jonsuns7Author Commented:
CodeCruiser

if it were that easy I would be doing back flips by now.
0
 
jonsuns7Author Commented:
I had to concatenate single quotes to both the preceeding sting and then variable and it was successful.

"Select ... Where GoalID=" & "'" & GoalHolder & "'"
0
 
CrashmanCommented:
for avoid this behavior you must use string.format,
you can use a variable or directly in the dataadapter

String.Format("Select * from table Where GoalID= {0} ",GoalHolder)
in this case we assume is a integer variable, if not,

String.Format("Select * from table Where GoalID= '{0}' ",GoalHolder)
just add '  '

http://www.techrepublic.com/article/easily-format-string-output-with-nets-stringformat/5794526

this is a good practice
0
All Courses

From novice to tech pro — start learning today.