How to pass parameter to datagridview

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
I have a datagridview control that's bound a stored procedure, no problem there. The problem I have is I can't find out how to pass the parameter value to the stored procedure at run time on the load of the form.
Could use some help here.

Thanks,
Tom
 
Ok its important to note that the DataGridView is a means of displaying data by its datasource only and that you dont have to pass commands on the DGV itself. Meaning that you are most likely filling a DataSet/DataTable that is then connected to the dgv, it is in this that you want to add your paramter too not the DGV.

You havent provided the details of how your filling your DataSet/DataTable but either way you would add the parameter to the command object that is calling your stored procedure.

VB.NET:
[COLOR="Blue"]Dim [/COLOR]dsMyDataSet [COLOR="blue"]As New [/COLOR]DataSet

[COLOR="blue"]Using [/COLOR]con [COLOR="blue"]As New [/COLOR]SqlConnection(g_strDatabaseConnection)
    [COLOR="blue"]Dim [/COLOR]cmdSelect [COLOR="blue"]As New [/COLOR]SqlCommand
    [COLOR="blue"]Dim [/COLOR]da [COLOR="blue"]As New [/COLOR]SqlDataAdapter

    cmdSelect.Connection = con
    cmdSelect.CommandType = CommandType.StoredProcedure
    cmdSelect.CommandText = [COLOR="DarkRed"]"spMyStoredProcedureName"[/COLOR]

    [COLOR="SeaGreen"]'Two parameters for the stored procedure[/COLOR]
    [COLOR="seagreen"]'First is @PckId and Second is @CustKey[/COLOR]
    cmdSelect.Parameters.AddWithValue([COLOR="darkred"]"@PckId"[/COLOR], strPck)
    cmdSelect.Parameters.AddWithValue([COLOR="darkred"]"@CustKey"[/COLOR], intCustKey)

    da.Fill(dsMyDataSet, [COLOR="DarkRed"]"MyTableName"[/COLOR])

    da.Dispose()
    cmdSelect.Dispose()
[COLOR="blue"]End Using[/COLOR] [COLOR="seagreen"]'con[/COLOR]

dgvDataViewer.DataSource = dsMyDataSet.Tables([COLOR="DarkRed"]"MyTableName"[/COLOR]) [COLOR="seagreen"]'or table index[/COLOR]
 
Last edited:
I have a datagridview control that's bound a stored procedure, no problem there. The problem I have is I can't find out how to pass the parameter value to the stored procedure at run time on the load of the form.
Could use some help here.

Thanks,
Tom

You should post the code you use to fill the grid without the parameter..

What's confusing to me is, if you went the datasets/tableadapters route (see DW2 link in my signature, look for a section on using stored procedures) then you'd have already had the parameter worked into the code..
 
Back
Top