Mathew

Mathew

  • NA
  • 3
  • 0

Copying a portion of data from clipboard in Excel sheet to Datagridview in C#.Net windows application

Apr 4 2007 11:36 PM
Hi,

I have a DatagridView in windows form.It has got a column which has got 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 columns 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 sued 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.CommaSeparatedValue))

{

clipboardData = Clipboard.GetDataObject();

using (StreamReader reader = new StreamReader((Stream)(clipboardData.GetData(DataFormats.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;