Add array to DataGridView VB.NET 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbrown
    New Member
    • Aug 2007
    • 5

    Add array to DataGridView VB.NET 2005

    So I've got an array of string values which I'd like to display in an unbound DataGridView. What is the process necessary to simply load strings into cells of a datagrid?

    Ideally, we would go from this:

    strArray(0) = "Ralph"
    strArray(1) = "Johnston"
    strArray(2) = "12/15/87"

    to this (forgive the crude drawing):

    | Ralph | Johnston | 12/15/87 |


    I've had success getting values in with the code below, but this adds a new column onto the datagrid. What I want is to add data values into and existing column structure that I have in place. Thanks for your time.

    Code:
    Dim strArray() As String = {"Ralph"}
    Dim dt As New DataTable
    dt.Columns.Add("FirstName", GetType(String))
    Dim dr As DataRow = dt.NewRow
    dr("FileName") = strArray(0)
    dt.Rows.Add(dr)
    ' Set the datagrid's source
    Me.dgMain.DataSource = dt
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You can manipulate the DataGridView items themselves (provided it is unbound)

    [code=c#]
    int myRowIdx=myData GridView.Rows.A dd();
    DataGridViewRow dgr = myDataGridView. Rows[myRowIdx];
    dgr.Cells["MyColumnNa me"].Value = "Some value";
    [/code]

    Comment

    Working...