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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView being populated programmably 1

Status
Not open for further replies.

bminaeff

Programmer
Dec 26, 2007
49
US
Hi all,

Does anyone have a good example or guide that shows how to programmably fill a datagridview? I have always used the wizard, but now I have a requirement to use a datagridview to view data in varying tables. If anyone has anything on this I would appreciate it.

It seems that there is more information on the DataGrid, but in 2005 the DataGrid.SetDataBinding method does not exist and I think I am trying to do something similar to that with the DataGridView


Thanks
-Bill
 
Are you saying you want to fill an unbound DataGridView? If so, try this sample. Add it to the Form_Load event of a test project.

Code:
        Dim DGV As New DataGridView
        Dim Rand As New Random
        DGV.Columns.Add("Column1", "Column1")
        DGV.Columns.Add("Column2", "Column2")

        For i As Integer = 0 To 20
            Dim NumValue As Integer
            Dim StringValue As String
            Dim Obj(1) As Object
            NumValue = Rand.Next(1, 100)
            StringValue = Chr(Rand.Next(33, 126)) + Chr(Rand.Next(33, 126)) + Chr(Rand.Next(33, 126))
            Obj(0) = NumValue
            Obj(1) = StringValue
            DGV.Rows.Add(Obj)
        Next

        Me.Controls.Add(DGV)
        DGV.Top = 6
        DGV.Left = 6
        DGV.Size = New Size(300, 300)
 
RiverGuy,

I am getting the data from an SQL 2005 table, but the table will be varying.

-Bill
 
You can retrieve the data and put it into a table withing a dataset then simply set the gridview datasource to it.

datagridview1.datasource=mydataset.tables("Tablename")


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top