Programmatically create a datagrid

shawne

Well-known member
Joined
Feb 22, 2006
Messages
103
Location
Pennsylvania
Programming Experience
10+
I'm trying to programmatically create a datagrid. I found the following code snippet, but I get a "Type expected." error when trying to declare the datagrid. I'm sure i'm missing something very simple, but I'm having a rough time figuring it out. Here is the snippet, thanks for any help:

Public Sub CreateGrid()

'declare a new datagrid and set properties
' Dim DataGrid1 As DataGrid


Dim DataGrid1 As New DataGrid

DataGrid1.BorderWidth = 2
DataGrid1.CellPadding = 10
DataGrid1.GridLines = Both
DataGrid1.BorderColor = Color.Blue
DataGrid1.ShowHeader = True
DataGrid1.AutoGenerateColumns = False
DataGrid1.SelectedItemStyle.BackColor = Color.Yellow


'add bound columns to the datagrid
Dim datagridcol As New BoundColumn
datagridcol.HeaderText = "Candy Type"
datagridcol.DataField = "CandyType"
DataGrid1.Columns.Add(datagridcol)

datagridcol = New BoundColumn
datagridcol.HeaderText = "Description"
datagridcol.DataField = "CandyDescription"
DataGrid1.Columns.Add(datagridcol)

Dim selectcol As New ButtonColumn
selectcol.ButtonType = ButtonColumnType.PushButton
selectcol.Text = "Purchase"
selectcol.CommandName = "Select"
DataGrid1.Columns.Add(selectcol)

'bind datagrid
DataGrid1.DataSource = GetDataSet()
DataGrid1.DataBind()

'add datagrid to the page
Page.Controls(1).Controls.Add(DataGrid1)
End Sub

Private Function GetDataSet()
Dim ws As New localhost.SweetsService
Dim ds As New DataSet
ds = ws.GetCandyInfo("truffles")
Return ds
End Function


End Class
 
Why are you doing it like that?

Also, you've dimmed the grid inside the sub... which means that as soon as the sub is over, the grid will go away.

Additionaly, unless you put it on the form and display it, what use is it?

-tg
 
Here is where I got the code snippet from. All I need the datagrid to do is display information and be able to select it. I don't want any of the editing functions from it. IE the right click edit menu. I have my own context menu I will be using instead. I talked to our in house app. developer here and he told me I should try creating my datagrids programmatically instead of using the GUI. Go easy on me, i'm still a noob to vb.net ..
 
Open your form, drop a grid on it, give it a name.... then look through the properties, and turn off the Allow Edit, Delete, and New properties. That will turn your grid into a read only.

The only thing you should programaticaly is binding the Dataset to the grid.... in my sig there's a could of ADO.NET tutorials that show how to do that.

After you are done reading them, take this trout right here and go smack your in-house dev on the head for suggesting such a thing (I suspect he wanted you to become frustrated w/ it and "let" him do it instead. But that's just speculation)

-tg

ps - were all noobs with .net at some point... I still have tons to learn too....
 
hehehe ... thanks for the trout, i'll keep it handy.

That was how I originally did my grid, but if I click in a cell it highlights the context and "acts" like I can edit it, but I can't, which is fine except then when I hit my right click instead of getting my context menu, I get the edit menu. My datagrid has the read-only property set and I double checked the dataview and it has all three of the properties you mentioned set to false. I think everything is working the way it's suppose to, but it's not the way I want it to.

Basically I want it to act like a list box. I tried using a list box to display my data, but since you can't bind it like you can a datagrid, it was a bit clumsy and slow.
 
Change the selection mode to Entire Row... that'll help the look some.... havew you thought about using the list view instead of the list box? You would have to do a little more work since you can't just bind it, but if it's for read only purposes, there's no need to bind it..... that might solve your problems....

What you would have to do is this:
1) Replace the grid with a ListView
2) Add column to the ListView - this can be done in design or at runtime.
3) After getting your DataSet, loop through each row in the datatable, add the fields into an array
4) Create New ListViewItem passing in the array... each element in the array corresponds to the columns in the ListView
5) If you want, set the .Tag property to the DataRow object (for retrieval later). I usualy do this so I can alter get back to some of the data elements that aren't actualy displayed.
6) ListView.Items.Add the new ListViewITem you just created.
7) Set the ListView to display in Details mode (Again this can be done in code or in design mode.)

It would be a good idea to invoke the ListView.BeginUpdate before starting and the ListView.EndUpdate when done to prevewnt flickering as each item is added.

if you need a code snip on how to do some of this stuff, let me know and I'll whip something up.

-tg
 
Thanks TG. Ironically I think I was on the right track before I tried to use a datagrid. Lemme bang on it and i'll post back if I get stuck. Thanks again!
 
dataList is still undefined because it has no elements....

This
VB.NET:
Dim dataList(0) as string
dataList(0) = "BOO!"
dataList(1) = "ROCK-ON!"
is supposed to work, but I don't trust it...

VB.NET:
Dim dataList(1) as string
dataList(0) = "BOO!"
dataList(1) = "ROCK-ON!"
works for sure....

The reason your first sample works is because it is happening at the same time as the instanciation. It can see how many elements are needed and adjusts the array accordingly.

-tg
 
That was it....I don't know where, but somewhere I thought I read that by not defining the number of elements, it left it open ended. Either I read it wrong or it was in another lang. Thanks a ton for all your help TG, ya got me on track now!
 
YEah, that's how it WAS in VB6.... but in .NET everything is an object now, including arrays, so it's easier to expand them when needed.

=tg
 
Back
Top