Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very Newbie Question 1

Status
Not open for further replies.

ThinWhiteDude

Technical User
Sep 21, 2004
97
US
Please forgive me if this is too basic for the forum, have tried to find the solution through subject search, no luck:

I am learning VB Net after a hiatus since last using VB6 . . . what I would like to do is populate a column of a data grid with a "hard list" for demo purposes, until I can get it bound to a SQL table.

Is such a thing possible? If so, how? Is there another forum I should be going to?

Thanks in advice for your patience,
TWD
 
You don't say which version of VB.NET you are using, but you say DataGrid (which is 2002/2003) so:

Code:
  Private dt As DataTable

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    dt = New DataTable
    dt.Columns.Add(New DataColumn("Col1"))
    dt.Columns.Add(New DataColumn("Col2"))
    dt.Columns.Add(New DataColumn("Col3"))
    Dim dr As DataRow
    For a As Integer = 0 To 2
      dr = dt.NewRow
      dr.Item(0) = a.ToString
      dr.Item(1) = a.ToString
      dr.Item(2) = a.ToString
      dt.Rows.Add(dr)
    Next
    DataGrid1.DataSource = dt

  End Sub

should point you in the right direction.

It is slightly different for 2005.


Hope this helps.

[vampire][bat]
 
A quick test in 2005, shows that the code is virtually identical:

Code:
	Private dt As DataTable

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		dt = New DataTable
		dt.Columns.Add(New DataColumn("Col1"))
		dt.Columns.Add(New DataColumn("Col2"))
		dt.Columns.Add(New DataColumn("Col3"))
		Dim dr As DataRow
		For a As Integer = 0 To 2
			dr = dt.NewRow
			dr.Item(0) = a.ToString
			dr.Item(1) = a.ToString
			dr.Item(2) = a.ToString
			dt.Rows.Add(dr)
		Next
		DataGridView1.DataSource = dt

	End Sub

The difference being DataGrid in 2002/2003 and DataGridView in 2005, although there is a much greater difference between them than just the name. [smile]


Hope this helps.

[vampire][bat]
 

Oh Gosh, earthandfire, I forgot a couple of things:

(First rule. . . tell them what you're using. . .)
Yes, I'm using version 5

(2nd rule . . .give them ALL the info. . .)
I am actually trying to populate one combobox in the first field (column) of the datagrid.

I am sorry for breaking the rules here, I did put your code into the load event of the form, works like a charm, so thanks so much for that, it will go into my permanent notes.

Is there a way to do this one?

TWD
 
Assuming you did mean 2005, then try this:

Code:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim comboboxstrings() As String = {"comboOne", "comboTwo", "comboThree"}
		With DataGridView1
			.Columns.Add(New DataGridViewComboBoxColumn())
			.Columns.Add(New DataGridViewTextBoxColumn)
			.Columns.Add(New DataGridViewCheckBoxColumn)
			.Columns(0).HeaderText = "Combo1"
			.Columns(1).HeaderText = "Text1"
			.Columns(2).HeaderText = "Check1"
			CType(.Columns(0), DataGridViewComboBoxColumn).Items.AddRange(comboboxstrings)
			For a As Integer = 0 To 2
				.Rows.Add()
				.Rows(a).Cells(0).Value = comboboxstrings(a)
				.Rows(a).Cells(1).Value = a.ToString
				.Rows(a).Cells(2).Value = IIf(a Mod 2 = 0, True, False)
			Next
		End With

	End Sub

Hopefully this makes sense!

[vampire][bat]
 
That's exactly what I need! I've already adapted it and put my 6 choices on the list. Thank you so my for all your help ... and have a star!

Can I give you an extra star for all your patience with my errors?

TWD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top