Solved

Program button or link into datagrid VB

Posted on 2010-11-21
6
501 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:fixitben
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34185118
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
0
 
LVL 7

Author Comment

by:fixitben
ID: 34185164
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

0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34185225
Im so sorry, should have read the question properly!
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34204552
You can use the DataGridView.CellContentClick event and following code is an example


Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)

	If e.ColumnIndex = 1 Then 'Assuming the button column as second column, if not can change the index

		Dim f As New Form1()

		f.navId = dataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString()

		f.Show()

	End If

End Sub

Open in new window

0
 
LVL 7

Author Closing Comment

by:fixitben
ID: 34232677
That worked great thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34232936
Glad to help :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now