• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 13105
  • Last Modified:

Add button to datagridview in vb.net 2005

Hi,

I have the following code: -

        If ds.Tables.Contains("Items") Then
            ds.Tables("Items").Clear()
        End If

        dbOpen()
        sql = "select      PurchaseOrderLineno as 'Line No', " & _
                        "Qty_Ordered as 'On Order', " & _
                        "Qty_Accepted as 'Qty Acc', " & _
                        "Product_Description, " & _
                        "OrderID, " & _
                        "Part_No, " & _
                        "dbo.Products.ProductID, " & _
                        "Notes " & _
                        "from dbo.Orders " & _
                    "INNER JOIN dbo.Products " & _
                         "on dbo.Orders.ProductID = dbo.Products.ProductID " & _
                "WHERE PurchaseOrderNo = '" & cmbOrderNumbers.Text.ToString & "' AND Closed = 'false'"

        Dim da3 As New SqlClient.SqlDataAdapter(sql, oConn)
        da3.Fill(ds, "Items")

        DataGridView1.DataSource = ds.Tables("Items")
        Me.DataGridView1.Columns(0).Width = 30
        Me.DataGridView1.Columns(1).Width = 50
        Me.DataGridView1.Columns(2).Width = 50
        Me.DataGridView1.Columns(3).Width = 140
        Me.DataGridView1.Columns(4).Visible = False
        Me.DataGridView1.Columns(5).Width = 140
        Me.DataGridView1.Columns(6).Visible = False
        Me.DataGridView1.Columns(7).Width = 140

Basically it populates a datagridview. What I am trying to find out is how I add an extra column at the end of the row that says book In.

Many Thanks
Lee
0
ljhodgett
Asked:
ljhodgett
  • 3
  • 3
1 Solution
 
ChumadCommented:
Is your gridview created already in the aspx page? You could always add it there...

Of crouse, if you want to add a column dynamically, you should be able to do that using the Me.DataGridView1.Columns.Add method.
0
 
ljhodgettAuthor Commented:
Hi,

Sorry its a winform. I'm new to this how would I use the .add method for a button.

Best Regards
Lee
0
 
ChumadCommented:
"To add a column dynamically to a GridView you use GridView.Columns.Add() or
GridView.Columns.Insert() but you have to pass it a DataControlField so it
knows what type of column it is adding (TemplateField, BoundField etc.).

e.g.

TemplateField MyTemplateField = new TemplateField();
Me.DataGridView1.Columns.Add(MyTemplatefield);"

-----

Now to translate to VB -- which I'm rusty with so bear with me :)
Dim MyTemplateField as new TemplateField()
Me.DataGridView1.Columns.Add(MyTemplateField)

'if you want a bound field instead of a template field, do this:
Dim MyBoundField as new BoundField()
MyBoundField.DataField = "some DB field name goes here"
Me.DataGridView1.Columns.Add(MyBoundFIeld)

You'll have to do a few more steps in here depending on what you really want to do with the column as far as data binding and such. Also, make sure you add the column PRIOR to using the DataBind() method.

Hope this helps :)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ljhodgettAuthor Commented:
Hi,

Sorry I'm quite new to vb.net. I've managed to create the code that does something when the button is clicked but not too sure about the code you have put above. The code i'm using is: -

Imports System.Data.SqlClient
Public Class frmBookInPurchaseOrder

    Dim sql As String
    Dim ds As DataSet
    Dim firstpass As Boolean

    Private Sub frmBookInPurchaseOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        firstpass = True
        ds = New DataSet
        dbOpen()
        sql = "SELECT PurchaseOrderNo  from Orders where dbo.Orders.Closed = 'false' GROUP by dbo.Orders.PurchaseOrderNo ORDER by PurchaseOrderNo"

        Dim da3 As New SqlClient.SqlDataAdapter(sql, oConn)
        da3.Fill(ds, "Orders")

        cmbOrderNumbers.DisplayMember = "PurchaseOrderNo"
        cmbOrderNumbers.DataSource = ds.Tables("Orders")


    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub

    Private Sub cmbOrderNumbers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Update_Table()
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim Returned_input As String
        Dim Returned_Closed As Boolean

        Returned_input = ""

        If DataGridView1.Columns(e.ColumnIndex).CellType.Equals(GetType(DataGridViewButtonCell)) Then

            If Not DataGridView1.Item("OrderID", e.RowIndex).Value.ToString = "" Then
                Returned_input = InputBox("Please enter quantity received")

                If Returned_input = "" Then
                    MsgBox("No quantity entered" & vbCrLf & "No Data has been Saved", MsgBoxStyle.Information, "Error in Field")
                    Exit Sub
                End If

                If IsNumeric(Returned_input) = False Then
                    MsgBox("Invalid entry." & vbCrLf & vbCrLf & "Entry Should be numeric", MsgBoxStyle.Information, "Error in Field")
                    Exit Sub
                End If

                Returned_Closed = False
                If MsgBox("Do you want to close the purchase order", MsgBoxStyle.YesNo, "Close Order") = MsgBoxResult.Yes Then
                    Returned_Closed = True
                End If

                dbOpen()
                Dim myCommand As New SqlCommand("Update_Orders", oConn)
                myCommand.CommandType = CommandType.StoredProcedure

                myCommand.Parameters.AddWithValue("@qty", Returned_input)
                myCommand.Parameters.AddWithValue("@closed", Returned_Closed)
                myCommand.Parameters.AddWithValue("@OrderID", DataGridView1.Item("OrderID", e.RowIndex).Value.ToString)
                myCommand.Parameters.AddWithValue("@ProductID", DataGridView1.Item("ProductID", e.RowIndex).Value.ToString)

                myCommand.ExecuteNonQuery()

                Update_Table()

            End If
        End If

    End Sub

    Private Sub Update_Table()

        If ds.Tables.Contains("Items") Then
            ds.Tables("Items").Clear()
        End If

        dbOpen()
        sql = "select      PurchaseOrderLineno as 'Line No', " & _
                        "Qty_Ordered as 'On Order', " & _
                        "Qty_Accepted as 'Qty Acc', " & _
                        "Product_Description, " & _
                        "OrderID, " & _
                        "Part_No, " & _
                        "dbo.Products.ProductID, " & _
                        "Notes " & _
                        "from dbo.Orders " & _
                    "INNER JOIN dbo.Products " & _
                         "on dbo.Orders.ProductID = dbo.Products.ProductID " & _
                "WHERE PurchaseOrderNo = '" & cmbOrderNumbers.Text.ToString & "' AND Closed = 'false'"

        Dim da3 As New SqlClient.SqlDataAdapter(sql, oConn)
        da3.Fill(ds, "Items")

        DataGridView1.DataSource = ds.Tables("Items")
        Me.DataGridView1.Columns(0).Width = 30
        Me.DataGridView1.Columns(1).Width = 50
        Me.DataGridView1.Columns(2).Width = 50
        Me.DataGridView1.Columns(3).Width = 140
        Me.DataGridView1.Columns(4).Visible = False
        Me.DataGridView1.Columns(5).Width = 140
        Me.DataGridView1.Columns(6).Visible = False
        Me.DataGridView1.Columns(7).Width = 140


    End Sub

    Private Sub cmbOrderNumbers_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbOrderNumbers.SelectedIndexChanged
        Update_Table()
    End Sub
End Class

I would appreciate if you could show me above integrating the code into mine above.

Many Thanks
Lee
0
 
ChumadCommented:
So you only want to add a new column? I don't see anywhere that it's done programmatically to your code now - so I'm betting if you go to the design view you'll be able to add it at design time like the other columns you have are current defined.
0
 
ljhodgettAuthor Commented:
I've managed to add the button in the design view but it just shows the button and not the text in the button to say book in. How do I set this please?

Many Thanks
Lee
0
 
kks81Commented:
>> but it just shows the button and not the text in the button to say book in. How do I set this please?

A little too late ;-)
but this could help others:
You can either set the value for each individual cell:
for each dr as DataGridViewRow in dgv1
   dr("ButtonCol").Value = "Book in"
next

or you can set the "UseColumnTextForButtonValue" property of the ButtonColumn to true (in this case the Text Property of the Column is inherited to each DataGridViewButtonCell)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now