Solved

Program button or link into datagrid VB

Posted on 2010-11-21
6
508 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

685 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