Results 1 to 6 of 6

Thread: Datagrid trouble

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    96

    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
    Last edited by theguyinthehat; Oct 6th, 2009 at 10:43 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    96

    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
    Last edited by theguyinthehat; Oct 6th, 2009 at 10:50 AM.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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?
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    96

    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?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    96

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width