Link to home
Start Free TrialLog in
Avatar of ljhodgett
ljhodgett

asked on

datagridview not showing columns correctly in vb.net 2005

hi,

I have the following code: -

    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'"

        'MsgBox(sql)

        Dim da3 As New SqlClient.SqlDataAdapter(sql, oConn)
        da3.Fill(ds, "Items") ' populate data set "da" with the data returned from sql command "ds" and set dataset name to "Rigs"
        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(5).Width = 140
        Me.DataGridView1.Columns(7).Width = 140


        Dim buttons As New DataGridViewButtonColumn()
        With buttons
            .HeaderText = ""
            .Text = "Book In"
            .UseColumnTextForButtonValue = True
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .CellTemplate.Style.BackColor = Color.Honeydew
            .DisplayIndex = DataGridView1.ColumnCount + 1
        End With
        Me.DataGridView1.Columns(4).Visible = False
        Me.DataGridView1.Columns(6).Visible = False
        'DataGridView1.Columns.Add(buttons)
    End Sub

I'm having trouble with the datagridview. It has 7 columns when it is run for the first time, the end one being a button. When I run the code again it goes down to 5 columns. what am I doing wrong please.

Many Thanks
Lee
Avatar of digitalZo
digitalZo
Flag of India image

So what exactly do you want? Do you want 5 or 7 columns at runtime?
Avatar of ljhodgett
ljhodgett

ASKER

What it does is return values off the server but column 4 and 6 are sql indexes and I do not want them to be shown to the end user but I use it in my code elsewhere. Hence thats why I have tried : -

Me.DataGridView1.Columns(4).Visible = False
Me.DataGridView1.Columns(6).Visible = False

I use the code: -

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

to run the code above. when it runs for the first time it shows the columns correctly as which is correct: -

Line No
On Order
Qty Acc
Product Description
Part no
notes

when I click on the combobox again it only returns the columns which is incorrect: -

Line No
On Order
Qty Acc
notes

hope this makes sense, if not please let me know.

Lee
Ahh okay, I get what is happening. It is not clearing the tables in the dataset and therefore, on the second click it's setting the visibility of the 4th and 6th column to false.

If you type the code like this:

      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


        Dim buttons As New DataGridViewButtonColumn()
        With buttons
            .HeaderText = ""
            .Text = "Book In"
            .UseColumnTextForButtonValue = True
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .CellTemplate.Style.BackColor = Color.Honeydew
            .DisplayIndex = DataGridView1.ColumnCount + 1
        End With
         
        'DataGridView1.Columns.Add(buttons)

----
....does it give an error? Does it give the same output?
A q - why have you given this condition?

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

Why not directly:

 ds.Tables("Items").Clear()

?
hi,

Yes it does I'm afraid.

Best Regards
Lee
hi,

I've had to do it like this as it comes up Object reference not set to an instance of an object if I just put in: -

ds.Tables("Items").Clear()

Best Regards
Lee
Hi,

the full code that I am 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")

        Dim buttons As New DataGridViewButtonColumn()
        With buttons
            .HeaderText = ""
            .Text = "Book In"
            .UseColumnTextForButtonValue = True
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .CellTemplate.Style.BackColor = Color.Honeydew
            .DisplayIndex = DataGridView1.ColumnCount + 1
        End With

        DataGridView1.Columns.Add(buttons)


    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'"

        'MsgBox(sql)

        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

It seems to be something to do with adding the button called "Book In". When I remove this the code works fine but I need the button at the end of the columns.

Best Regards
Lee
Avatar of Pratima
Put the code for adding button call inside ispostback condtion as that it excuted only at first time

if ( me.ispostback = false)

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")

        Dim buttons As New DataGridViewButtonColumn()
        With buttons
            .HeaderText = ""
            .Text = "Book In"
            .UseColumnTextForButtonValue = True
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .CellTemplate.Style.BackColor = Color.Honeydew
            .DisplayIndex = DataGridView1.ColumnCount + 1
        End With

        DataGridView1.Columns.Add(buttons)

end if
ASKER CERTIFIED SOLUTION
Avatar of digitalZo
digitalZo
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pratima, i thought of is not postback too, but the update code isn't on page_load. will the postback still work?
when we go for any event on the page it first excutes the page_load .
So If we check the postback condtion , then at the time of postback it will notexcute the same things agian
Lee,

1) DataGridView is WinForms, not ASP.NET, so there isn't any post-backs.

2) Are you defining columns through the designer?  

3) Did you set AutoGenerateColumns = False in code somewhere?

Bob
<<1) DataGridView is WinForms, not ASP.NET, so there isn't any post-backs.>>

You're right! ><

<<
3) Did you set AutoGenerateColumns = False in code somewhere?>>

Is there a behavior option 'AutoGenerateColumns = False' in DataGridView?
Yes, the AutoGenerateColumns for the DataGridView, oddly enough, is not a browsable property, so you can only set it in code.

Bob