473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Populating DataGridView with Excel data (is there an easy way todatabind with datasource-property?)

Hi,

I've been implementing a program that populates winforms DataGridView
from an excel sheet (.xlsx-file).
The app works ok. Now I'm refactoring it and I've got a feeling that
databinding could be done more elaborately than I'm doing currently.

First I read in the excel file and extract the first row and make it's
columns the header columns in the datagridview...
foreach (string s in CreateRow(xh, columns, 1))
dataGridView1.Columns.Add(s, s);

(CreateRow is a subroutine that return IList<stringby the row number
entere in the last argument)
....and then I add needed amount of rows in one line...

dataGridView1.Rows.Add(rows - 1);

(note that rows-variable holds the number of rows in the excel sheet)

....then I populate the cells in each row manually...

for (i = 0; i <= rows - 1; i++)
{
int j = 0;
foreach (string s in CreateRow(xh, columns, i + 2))
dataGridView1.Rows[i].Cells[j++].Value = s;
}
This works. But I'm just wondering if it's possible to say:

dataGridView1.DataSource = myMagicListOfExcelData;

???

I could do it if I knew the format of the excel data.
For example if I knew that it contains columns called "Product" and
"Price" I could do some kind of holder-class for them...

// just example
class ProductAndPriceContainer
{
public string Product { get; set; }

public float Price { get; set; }
}

....and then I could list these in an IEnumerable-list and say...

dataGridView1.DataSource = myProductsAndContainersList;

....or I could do an anonymous types to create a class on the fly, but
that too requires mee to know the fields...

var Holder = new {
Product = getfromexcel...;
Price = getfromexcel...;
}
But I want to make a dynamic solution, not fixed to predetermined
fields.
Any ideas?

Nov 15 '07 #1
1 3256
The simplest option here might be to create a DataTable, add columns
and then rows?

I know of a few other tricks for binding to dynamic data, but the
above is very simple...

Example code (different context): http://tinyurl.com/ypr3yw

Marc
Nov 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Henok Girma | last post by:
Hello Gurus, I want to save the state of an unbound DataGridView on my Windows Form application to an XML file so i can later load it back.. Basically, on my form I have a DataGridView, it's got...
11
by: Kevin | last post by:
I've been searching forever for examples of saving data changes in a DataGridView. There's all kinds of examples, but none really show how to save changes. Someone please help me. I have a...
0
by: archuleta37 | last post by:
I'm working on reading data from a spreadsheet and outputing it into a datagridview in a windows form (.net 2.0). The problem I've found is that I'm getting 84 rows where only one cell is...
2
by: Ryan | last post by:
Hi all, I'm loading a datagridview from an Excel file using a dataadapter.fill() method. A few questions: 1) Is there any way to control the column datatypes? The columns are defined by the...
0
by: MathewJose | last post by:
Hi, I have a DatagridView in windows form.It has got a column which has some names that are populated from master table in database.Now i need to copy a set of data against these names. I...
6
by: =?Utf-8?B?TWFyY2Vsbw==?= | last post by:
Hello, I am new to pulling data into VB and using DataGridViews..... so this might be a dumb question but I can not seem to understand how to make this work. What I need acomplished: Exec a...
1
by: szuccaro | last post by:
I am reading an excel file into a datagridview and trying to perform some function based on the column header that is clicked. I currently can produce an event to occur (msgbox) but, I need to get...
3
by: WestAussie | last post by:
Hello I'm trying to populate a DataGridView with a stored procedure using VB.NET. The query I need to use is within a Access 2007 datadase. When I run it in Access it is showing data but the...
1
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
1
by: =?Utf-8?B?VGFtbXk=?= | last post by:
Hi! I am using VB 2008 with SQL Server 2000 and SQL Server 2005 (depending which server the user selects to connect to). I have a combox in which the user types the server to connect to. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.