-
Datagrid trouble
I only want specific columns to show up in my DataGridView, so I run a dynamic sql query to select specific columns from a table on a sql server(I know its slow, but its only used once a day). However, every column from the table shows up in the datagrid, and I can't figure out what i'm doing wrong.
Code:
Public Class Assign_Priorities
Dim connect As New SqlConnection("Data Source=IRNTS4SQL;Initial Catalog=Materials;Integrated Security=True")
Dim ds As New DataSet
Dim da As New SqlDataAdapter("SELECT [Priority], [Tool Type], [Lot Number], [Process], [Priority Notes], [Quantity], [Time Job Entry] from [FPAD Process Log]", connect)
Dim cb As New SqlCommandBuilder
Private Sub Assign_Priorities_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cb.DataAdapter = da
da.Fill(ds, "Priorities")
DataGridView1.DataSource = ds.Tables("Priorities")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cb.GetUpdateCommand()
da.Update(ds, "Priorities")
End Sub
End Class
-
Re: Datagrid trouble
It's performing the query "SELECT * From [FPAD Process Log]" and I don't know why.... I only want the columns in my query to show up. I'm trying moving it to a stored procedure
-
Re: Datagrid trouble
I'm baffled by this question. You show the SQL code that selects a whole bunch of columns, you fill the table, then you bind the table to a datagridview, yet you seem to expect that only one column will be shown. Why is that?
-
Re: Datagrid trouble
I'm sorry--I only want the Datagridview to show the columns I selected in the query. Instead, it acts like I said 'SELECT * from [Tablename]'... which is NOT what I want (see sp).my datagridview has no native datasource...i don't know what could be interfering. I moved it to a stored procedure:
Code:
ALTER PROCEDURE [RSTA\TAHuth].[spAssignPriorities]
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
SELECT [Priority], [Tool Type], [Lot Number], [Process], [Priority Notes], [Quantity], [Time Job Entry]
from [FPAD Process Log]
WHERE [Time Work Entry] is Null
ORDER BY [Tool Type]
/* SET NOCOUNT ON */
RETURN
with VB
Code:
Private Sub Assign_Priorities_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cmd As New SqlCommand
cmd.Connection = connect
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spAssignPriorities"
da.TableMappings.Add("Table", "Priorities")
cb.DataAdapter = da
da.Fill(ds, "Priorities")
DataGridView1.DataSource = ds.Tables("Priorities")
End Sub
Does anyone see the problem?
-
Re: Datagrid trouble
Yeah. Your select statement includes all those columns, which means that the table will include those columns, which means that the datagridview will SHOW all those columns. There are two solutions:
1) Get rid of all the columns in the SELECT statement except the one you want to show.
2) Do as you are doing, but then hide all the columns that you don't want to show.
-
Re: Datagrid trouble
I feel like, time and time again, i've embarrassed myself with questions I figured out by simple debugging. It did take me 2 hours to discover it, but the issue was, I was loading a form I'd abandoned in the self-made switchboard....and the query was dynamic (and included all fields). Sorry to waste your time. I'm thinking of not marking this resolved...It won't help people who search for an answer.
In good news, contradictions in programming don't exist. You have to check your premises.