Solved

Program button or link into datagrid VB

Posted on 2010-11-21
6
500 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

707 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

11 Experts available now in Live!

Get 1:1 Help Now