Link to home
Start Free TrialLog in
Avatar of fixitben
fixitben

asked on

Program button or link into datagrid VB

Hi

I need to add a row to the end of one of my columns for a view button or linklabel.

What I want to do is when that button or link is clicked it will grab the value of the ID column of that row and run some code based on that value.

I am using visual studio 2008 and this is a vb application.

I have attached the code that I have so far. It populates the datagrid and updates it.

Thanks
Fixitben


Imports System.Data.SqlClient
Public Class Form1
    Private Const SELECT_STRING As String = "SELECT * FROM WO_TYPE"
    Private Const CONNECT_STRING As String = "Data Source=xxxxxxx;Initial Catalog=xxxxx;Integrated Security=True"

    ' The DataSet that holds the data.
    Private m_DataSet As DataSet


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As  _
    System.EventArgs) Handles MyBase.Load
        Dim data_adapter As SqlDataAdapter

        ' Create the SqlDataAdapter.
        data_adapter = New SqlDataAdapter(SELECT_STRING, _
            CONNECT_STRING)

        ' Map Table to Contacts.
        data_adapter.TableMappings.Add("Table", "WO_TYPE")

        ' Fill the DataSet.
        m_DataSet = New DataSet()

        Try
            'Fills the datagrid with data
            data_adapter.Fill(m_DataSet)
            DataGrid1.SetDataBinding(m_DataSet, "WO_TYPE")

            'setsup mapping for columns
            Dim subtypeTableStyle As New DataGridTableStyle()
            subtypeTableStyle.MappingName = "WO_TYPE"

            'Create ColumnStyle and set its mapping name & other properties.
            Dim colSubTypeID As New DataGridTextBoxColumn()
            colSubTypeID.HeaderText = "ID"
            colSubTypeID.MappingName = "WO_TYPE_ID"
            colSubTypeID.Width = 50

            Dim colSubTypeName As New DataGridTextBoxColumn()
            colSubTypeName.HeaderText = "TYPE"
            colSubTypeName.MappingName = "WO_TYPE"
            colSubTypeName.Width = 100

            'Call the Add method of GridColumnStylesCollection object to add the column to the table style.
            subtypeTableStyle.GridColumnStyles.Add(colSubTypeID)
            subtypeTableStyle.GridColumnStyles.Add(colSubTypeName)

            'Call the Add method of GridTbaleStylesCollection object to add the table style to the data grid.
            DataGrid1.TableStyles.Add(subtypeTableStyle)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


        ' Bind the DataGrid control to the Contacts DataTable.

    End Sub


    Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
    As System.ComponentModel.CancelEventArgs) Handles _
    MyBase.Closing
        If m_DataSet.HasChanges() Then
            Dim data_adapter As SqlDataAdapter
            Dim command_builder As SqlCommandBuilder

            ' Create the DataAdapter.
            data_adapter = New SqlDataAdapter(SELECT_STRING, _
                CONNECT_STRING)

            ' Map Table to Contacts.
            data_adapter.TableMappings.Add("Table", "WO_TYPE")

            ' Make the CommandBuilder generate the
            ' insert, update, and delete commands.
            command_builder = New  _
                SqlCommandBuilder(data_adapter)

            ' Uncomment this code to see the INSERT,
            ' UPDATE, and DELETE commands.
            'Debug.WriteLine("*** INSERT ***")
            'Debug.WriteLine(command_builder.GetInsertCommand.CommandText)
            'Debug.WriteLine("*** UPDATE ***")
            'Debug.WriteLine(command_builder.GetUpdateCommand.CommandText)
            'Debug.WriteLine("*** DELETE ***")
            'Debug.WriteLine(command_builder.GetDeleteCommand.CommandText)

            ' Save the changes.

            Try
                data_adapter.Update(m_DataSet)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try



        End If
    End Sub
End Class

Open in new window

Avatar of gingermoleman
gingermoleman

Hi fb,

If your using VS2008 then you should have been able to do this on the page via the GUI, it can some times trap you functionality wise but its very quick.
To do it in the way that your going are you happy using a URLString? ie add a hyperlink column

Dim urlCol As New HyperLinkColumn()
urlCol.DataTextField = "id"
urlCol.DataNavigateUrlField = "yourpage.aspx?id={0}"
urlCol.HeaderText = "Click Here!"

You'd then need to check for this string in the on page load function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then
        Else
            'get querystring values
            myID = Request.QueryString("id").ToString()
            //do your specific qry/set values here
EndIf

GMM
Avatar of fixitben

ASKER

This is a VB application not a VB webpage.

What I would really like to do is grab the ID field once the button is clicked  for that row and execute a sub using that value.


Thanks
Fixitben

Im so sorry, should have read the question properly!
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked great thanks
Glad to help :-)