By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,785 Members | 2,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,785 IT Pros & Developers. It's quick & easy.

Copying a set of rows from clipboard in Excel sheet to Datagridview

P: 3
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 tried to populate data to the first column from database and then tried to copy excel sheet data against each name but some exceptions are coming.It says "cannot programmaticaly add rows while using data bind".

This is the code that i used for copying excel to grid.Plz help.....

DataRow row = null;
Array colValues = null;
string excelRow = null;
DataTable excelTable = null;
IDataObject clipboardData = null;
excelTable = new DataTable("Excel");

if (Clipboard.ContainsData(DataFormats.CommaSeparated Value))

{

clipboardData = Clipboard.GetDataObject();

using (StreamReader reader = new StreamReader((Stream)(clipboardData.GetData(DataFo rmats.CommaSeparatedValue))))

{

while (reader.Peek() > 0)

{

excelRow = reader.ReadLine();

colValues = excelRow.Split(',');

if (! (excelTable.Columns.Count > 0))

{

for (int i = 0; i <= colValues.GetUpperBound(0); i++)

{

excelTable.Columns.Add();

}

}

row = excelTable.NewRow();

for (int i = 0; i <= colValues.GetUpperBound(0); i++)

{

row[i] = colValues.GetValue(i);

}

excelTable.Rows.Add(row);

}

}

}
uxGrid.DataSource = excelTable;
Apr 5 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.