Solved

Program button or link into datagrid VB

Posted on 2010-11-21
6
510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

695 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