Resizing Datagrid columns nightmare

J

Jim Mirra

Hey,

I have been working on this for a while now and I keep getting the same
error.

here is my code:

------- start code ---------
Dim strSQL = "Select tagid,itemid,description,status, transtype, (select
max(datetime) from transactions where tagid = inventory.tagid) as statusdate
from inventory"

Dim connect as new SqlConnection([Sql Connection Stringgoes here])
connect.open()

Dim ds as New DataSet
Dim da as New DataAdapter(strSQL, connect)
da.Fill(ds, "Inventory")
da.FillSchema(ds.Tables("Inventory"), SchemaType.Source)

DataGrid1.DataSource = ds.Tables("Inventory")

DataGrid1.TableStyles.Clear()
Dim objTblStyle as DataGridTableStyles = new DataGridTableStyles()
objTblStyle.MappingName = "Inventory"

Dim objGridColStyles as GridColumnStylesCollection
objGridColStyles = objTblStyle.GridColumnStyles

DataGrid1.TableStyles.Add(objTblStyle)

DataGrid1.TableStyles("Inventory").AlternatingBackColor = Color.LightGray

DataGrid1.TableStyles("Inventory").GridColumnStyles(0).Width = 150

------- end code --------

If I comment the last line of the code the code executes and the alternating
backcolors work.

with the last line uncommented, i get an Index Out of Range Error

and if i replace the (0) with ("tagid") or the name of another column I get
an Item not set to an instance of object error.

any help would be greatly appreciated.

Thanks,

Jim
 
B

BB

Jim,

I didn't play with the code, but am I missing where you
actually create each grid column? Looks to me from the
error message that your objGridColStyles is empty.
 
H

Herfried K. Wagner [MVP]

* "Jim Mirra said:
I have been working on this for a while now and I keep getting the same
error.

Multipost topics /are/ indeed a nightmare.
 
P

Peter Huang

Hi,

I changed your code as follows, and that works on my machine.
You may have a try and let me know the result.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strSQL = "select CustomerId,(select CompanyName from customers
where ContactName = 'Maria Anders') as des from customers"
Dim connectstring As String = Me.SqlConnection1.ConnectionString
Dim connect As New SqlClient.SqlConnection(connectstring)
connect.Open()
Dim ds As New DataSet
Dim da As New SqlClient.SqlDataAdapter(strSQL, connect)
da.Fill(ds, "Customers")
da.FillSchema(ds.Tables("Customers"), SchemaType.Source)
DataGrid1.DataSource = ds.Tables("Customers")
DataGrid1.TableStyles.Clear()
Dim objTblStyle As DataGridTableStyle = New DataGridTableStyle
objTblStyle.MappingName = "Customers"
Dim objGridColStyles As GridColumnStylesCollection
objGridColStyles = objTblStyle.GridColumnStyles
DataGrid1.TableStyles.Add(objTblStyle)
DataGrid1.TableStyles("Customers").AlternatingBackColor =
Color.LightGray
DataGrid1.TableStyles("Customers").GridColumnStyles(0).Width = 150
End Sub

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jim Mirra

My apologies this is my first time using newsgroups and wasn't sure if my
post went through as it did not show up in my newsreader.

Sorry for any harm I may have caused you.
 
J

Jim Mirra

No it still gives the same error. Do you think it has to do with the fact
that my query comes from multiple tables and not from one table as in your
example?

Jim
 
P

Peter Huang

Hi Jim,

I also test with the SQL statement below.
Dim strSQL = "select CustomerId,(select count(OrderID) from Orders where
CustomerID = 'ALFKI')as des from customers"
I still can not reproduce the problem.

To isolate the problem, I think you may try to simplify your sql statement.
e.g. select tagid from inventory
or you can try to use another database, e.g. northwind it will be shipped
with office ,VB98 or Microsoft SDK and it will be named NWIND.MDB.

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top