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.ToStr ing & "' AND Closed = 'false'"
'MsgBox(sql)
Dim da3 As New SqlClient.SqlDataAdapter(s ql, 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"
.UseColumnTextForButtonVal ue = True
.AutoSizeMode = DataGridViewAutoSizeColumn Mode.Displ ayedCells
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo lor = 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
I have the following code: -
Private Sub Update_Table()
If ds.Tables.Contains("Items"
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.ToStr
'MsgBox(sql)
Dim da3 As New SqlClient.SqlDataAdapter(s
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
Me.DataGridView1.Columns(1
Me.DataGridView1.Columns(2
Me.DataGridView1.Columns(3
Me.DataGridView1.Columns(5
Me.DataGridView1.Columns(7
Dim buttons As New DataGridViewButtonColumn()
With buttons
.HeaderText = ""
.Text = "Book In"
.UseColumnTextForButtonVal
.AutoSizeMode = DataGridViewAutoSizeColumn
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo
.DisplayIndex = DataGridView1.ColumnCount + 1
End With
Me.DataGridView1.Columns(4
Me.DataGridView1.Columns(6
'DataGridView1.Columns.Add
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
So what exactly do you want? Do you want 5 or 7 columns at runtime?
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_SelectedIn dexChanged _1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbOrderNumbers.SelectedIn dexChanged
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
Me.DataGridView1.Columns(4
Me.DataGridView1.Columns(6
I use the code: -
Private Sub cmbOrderNumbers_SelectedIn
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"
.UseColumnTextForButtonVal ue = True
.AutoSizeMode = DataGridViewAutoSizeColumn Mode.Displ ayedCells
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo lor = Color.Honeydew
.DisplayIndex = DataGridView1.ColumnCount + 1
End With
'DataGridView1.Columns.Add (buttons)
----
....does it give an error? Does it give the same output?
If you type the code like this:
DataGridView1.DataSource = ds.Tables("Items")
Me.DataGridView1.Columns(0
Me.DataGridView1.Columns(1
Me.DataGridView1.Columns(2
Me.DataGridView1.Columns(3
Me.DataGridView1.Columns(4
Me.DataGridView1.Columns(5
Me.DataGridView1.Columns(6
Me.DataGridView1.Columns(7
Dim buttons As New DataGridViewButtonColumn()
With buttons
.HeaderText = ""
.Text = "Book In"
.UseColumnTextForButtonVal
.AutoSizeMode = DataGridViewAutoSizeColumn
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo
.DisplayIndex = DataGridView1.ColumnCount + 1
End With
'DataGridView1.Columns.Add
----
....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()
?
If ds.Tables.Contains("Items"
ds.Tables("Items").Clear()
End If
Why not directly:
ds.Tables("Items").Clear()
?
ASKER
hi,
Yes it does I'm afraid.
Best Regards
Lee
Yes it does I'm afraid.
Best Regards
Lee
ASKER
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
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
ASKER
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_Loa d(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(s ql, oConn)
da3.Fill(ds, "Orders")
cmbOrderNumbers.DisplayMem ber = "PurchaseOrderNo"
cmbOrderNumbers.DataSource = ds.Tables("Orders")
Dim buttons As New DataGridViewButtonColumn()
With buttons
.HeaderText = ""
.Text = "Book In"
.UseColumnTextForButtonVal ue = True
.AutoSizeMode = DataGridViewAutoSizeColumn Mode.Displ ayedCells
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo lor = 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_SelectedIn dexChanged (ByVal sender As System.Object, ByVal e As System.EventArgs)
Update_Table()
End Sub
Private Sub DataGridView1_CellContentC lick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles DataGridView1.CellContentC lick
Dim Returned_input As String
Dim Returned_Closed As Boolean
Returned_input = ""
If DataGridView1.Columns(e.Co lumnIndex) .CellType. Equals(Get Type(DataG ridViewBut tonCell)) Then
If Not DataGridView1.Item("OrderI D", 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.StoredProcedur e
myCommand.Parameters.AddWi thValue("@ qty", Returned_input)
myCommand.Parameters.AddWi thValue("@ closed", Returned_Closed)
myCommand.Parameters.AddWi thValue("@ OrderID", DataGridView1.Item("OrderI D", e.RowIndex).Value.ToString )
myCommand.Parameters.AddWi thValue("@ ProductID" , DataGridView1.Item("Produc tID", 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.ToStr ing & "' AND Closed = 'false'"
'MsgBox(sql)
Dim da3 As New SqlClient.SqlDataAdapter(s ql, 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_SelectedIn dexChanged _1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbOrderNumbers.SelectedIn dexChanged
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
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_Loa
firstpass = True
ds = New DataSet
dbOpen()
sql = "SELECT PurchaseOrderNo from Orders where dbo.Orders.Closed = 'false' GROUP by dbo.Orders.PurchaseOrderNo
Dim da3 As New SqlClient.SqlDataAdapter(s
da3.Fill(ds, "Orders")
cmbOrderNumbers.DisplayMem
cmbOrderNumbers.DataSource
Dim buttons As New DataGridViewButtonColumn()
With buttons
.HeaderText = ""
.Text = "Book In"
.UseColumnTextForButtonVal
.AutoSizeMode = DataGridViewAutoSizeColumn
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo
.DisplayIndex = DataGridView1.ColumnCount + 1
End With
DataGridView1.Columns.Add(
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_SelectedIn
Update_Table()
End Sub
Private Sub DataGridView1_CellContentC
Dim Returned_input As String
Dim Returned_Closed As Boolean
Returned_input = ""
If DataGridView1.Columns(e.Co
If Not DataGridView1.Item("OrderI
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"
myCommand.CommandType = CommandType.StoredProcedur
myCommand.Parameters.AddWi
myCommand.Parameters.AddWi
myCommand.Parameters.AddWi
myCommand.Parameters.AddWi
myCommand.ExecuteNonQuery(
Update_Table()
End If
End If
End Sub
Private Sub Update_Table()
If ds.Tables.Contains("Items"
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.ToStr
'MsgBox(sql)
Dim da3 As New SqlClient.SqlDataAdapter(s
da3.Fill(ds, "Items")
DataGridView1.DataSource = ds.Tables("Items")
Me.DataGridView1.Columns(0
Me.DataGridView1.Columns(1
Me.DataGridView1.Columns(2
Me.DataGridView1.Columns(3
Me.DataGridView1.Columns(4
Me.DataGridView1.Columns(5
Me.DataGridView1.Columns(6
Me.DataGridView1.Columns(7
End Sub
Private Sub cmbOrderNumbers_SelectedIn
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
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(s ql, oConn)
da3.Fill(ds, "Orders")
cmbOrderNumbers.DisplayMem ber = "PurchaseOrderNo"
cmbOrderNumbers.DataSource = ds.Tables("Orders")
Dim buttons As New DataGridViewButtonColumn()
With buttons
.HeaderText = ""
.Text = "Book In"
.UseColumnTextForButtonVal ue = True
.AutoSizeMode = DataGridViewAutoSizeColumn Mode.Displ ayedCells
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo lor = Color.Honeydew
.DisplayIndex = DataGridView1.ColumnCount + 1
End With
DataGridView1.Columns.Add( buttons)
end if
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
Dim da3 As New SqlClient.SqlDataAdapter(s
da3.Fill(ds, "Orders")
cmbOrderNumbers.DisplayMem
cmbOrderNumbers.DataSource
Dim buttons As New DataGridViewButtonColumn()
With buttons
.HeaderText = ""
.Text = "Book In"
.UseColumnTextForButtonVal
.AutoSizeMode = DataGridViewAutoSizeColumn
.FlatStyle = FlatStyle.Standard
.CellTemplate.Style.BackCo
.DisplayIndex = DataGridView1.ColumnCount + 1
End With
DataGridView1.Columns.Add(
end if
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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?
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
Bob