Click here to Skip to main content
15,039,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to display a row of data from one excel form in separate boxes in a form. The way I see the solution is to place multiple datagridviews on one form and separate the rows to have 1 row of data per dgv.

Where the second dgv (dg2) displays row 4 from the datatable.

Any help would be appreciated.

What I have tried:

<pre lang="vb">Imports System.Data.OleDb

Public Class Form1
    Dim exceldata As New DataTable
    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim conn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ash\Dropbox\Sac U2\s1.xlsx;Extended Properties = ""Excel 12.0 Xml;HDR=YES"""
        Dim inserts As String = "SELECT [Name], [Date], [Description (basic)], [Location], [Size] FROM [sheet1$]"
        Dim adapter As New OleDbDataAdapter(inserts, conn)

        dg1.DataSource = exceldata

        Dim row4 As DataRow = exceldata.Rows(4)
        dg2.DataSource = row1

    End Sub
Updated 13-Oct-20 2:51am
Richard MacCutchan 12-Oct-20 4:46am
Why would you want each row in a separate DataGridView? The whole reason for using such a control is to display all rows/columns in a way that makes it easy for the user.
Gerry Schmitz 12-Oct-20 13:40pm
If you go through life picking the "easiest" control for yourself (vs the user), you will get nowhere. If someone needs a "form", a grid doesn't cut it.

1 solution

You haven't actually told us what the problem is, which is in the following
dg2.DataSource = row1
If you go to the documentation on DataGridView.DataSource[^] you will see
The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:

- The IList interface, including one-dimensional arrays.
- The IListSource interface, such as the DataTable and DataSet classes.
- The IBindingList interface, such as the BindingList<t> class.
- The IBindingListView interface, such as the BindingSource class.

If you go to the documentation for the DataRow Class[^] you will note that it does not implement any of those interfaces which means it cannot be used as a DataSource for your DataGridView. You probably want a DataTable [^] for that.

Something like this might work (untested!)
Dim tempData As New DataTable
dg2.DataSource = tempData
This doesn't get away from the fact that this is bad UI design - see the comments from @Richard-MacCutchan and @Gerry-Schmitz.
It is not difficult to design a pleasing and easy-to-use form consisting of the appropriate controls and to assign the individual values in the datarow columns to those controls. Your approach smacks of laziness
Member 14961724 18-Oct-20 22:28pm
Thank you, It might be obvious but I fairly new to coding. The design was garbage because the project was for a small school task and was lacking time and the task had no marks or requests for a good design so I was just chucking controls everywhere.

Next year is when we do a proper project including a proper design so I will be sure to check out those @'s. Really appreciate the response.

I don't think I expressed the problem well. What I am trying to do is separate or call individual rows from one datatable and send them to multiple datagridviews (one row per grid view). Hoperfully that makes more sense

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900