S
Sushil Agarwal
Guest
Hello Experts,
I am trying to paste data in a datagridview.
following is the code.
public static void Add2Grid(DataGridView dgData)
{
try
{
//Adam O'Neil's Development Blog
//Copy paste from Excel into a DataTable
//http://www.seesharpdot.net/?p=221
//16.6.2020 Modied to copy to datagridview , actually it was for insert in table copy of :save2Datatable
DataTable sourcetable = (DataTable)dgData.DataSource;
DataTable tbl = sourcetable.Clone();
tbl.PrimaryKey = null;
if (tbl.Columns.Contains("ui"))
tbl.Columns.Remove("ui");
dgData.DataSource = tbl;
string s = Clipboard.GetText();
char[] rowSplitter = { '\r', '\n' };
string[] lines = s.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
List<string> data = new List<string>(s.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries));
bool firstRow = true;
//!st Rowheader is read
string[] rowData = data[0].Split(new char[] { '\r', '\x09' });
//Get Index of a column searched by name in a datarow
//then each field location (ordinals) ar obtained
int[] fldOrdinals = new int[rowData.Length];
DataRow newRow = tbl.NewRow();
//int ScheduleidNdx=newRow.Table.Columns["Scheduleid"].Ordinal
//Find column headers being copied withh their column postion (ordinal) where
// values are to be copied
for (int i = 0; i < rowData.Length; i++)
{
try
{
rowData = rowData.Trim();
fldOrdinals = newRow.Table.Columns[rowData].Ordinal;
}
catch (Exception)
{
MessageBox.Show(rowData + ":Column not in Table:" + newRow.Table.TableName + "\nCorrect it");
return;
}
}
//Delete New Blank row at the bottom of copy source if its IsNullOrWhiteSpace
if (data.Count > 0 && string.IsNullOrWhiteSpace(data[data.Count - 1]))
{
data.RemoveAt(data.Count - 1);
}
//data copy from remaining rows as per their ordinals (index)
int j = -1;
string failedrows = "";
foreach (string iterationRow in data)
{
if (firstRow)
{
firstRow = false;
continue;
}
j++;
string row = iterationRow;
if (row.EndsWith("\r"))
{
row = row.Substring(0, row.Length - "\r".Length);
}
//rowData = row.Split(new char[] { '\r', '\x09' });
rowData = row.Split('\t');
if (rowData.Length != fldOrdinals.Length)
{
failedrows += "Row:" + j + " Import Skiped\n";
continue;
}
newRow = tbl.NewRow();
for (int i = 0; i < rowData.Length; i++)
{
if (i >= tbl.Columns.Count)
break;
if (rowData != null)
{
try
{
newRow[fldOrdinals] = rowData;
}
catch (Exception ex)
{
failedrows += j + " Row " + newRow.Table.Columns[fldOrdinals].ColumnName + ":Skipped";
}
}
}
tbl.Rows.Add(newRow);
//}
}
if (!string.IsNullOrWhiteSpace(failedrows))
MessageBox.Show(failedrows);
}
catch (FormatException)
{
MessageBox.Show("The data you pasted is in the wrong format for the cell");
}
}
some time i get error on following line, for example if newRow fields datatype is int and pasted rowData is string, it gives error and value do not get applied to newRow[fldOrdinals].
how can i convert runtime rowData value to newRow[fldOrdinal] datatype ?
newRow[fldOrdinals] = rowData;
Continue reading...
I am trying to paste data in a datagridview.
following is the code.
public static void Add2Grid(DataGridView dgData)
{
try
{
//Adam O'Neil's Development Blog
//Copy paste from Excel into a DataTable
//http://www.seesharpdot.net/?p=221
//16.6.2020 Modied to copy to datagridview , actually it was for insert in table copy of :save2Datatable
DataTable sourcetable = (DataTable)dgData.DataSource;
DataTable tbl = sourcetable.Clone();
tbl.PrimaryKey = null;
if (tbl.Columns.Contains("ui"))
tbl.Columns.Remove("ui");
dgData.DataSource = tbl;
string s = Clipboard.GetText();
char[] rowSplitter = { '\r', '\n' };
string[] lines = s.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
List<string> data = new List<string>(s.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries));
bool firstRow = true;
//!st Rowheader is read
string[] rowData = data[0].Split(new char[] { '\r', '\x09' });
//Get Index of a column searched by name in a datarow
//then each field location (ordinals) ar obtained
int[] fldOrdinals = new int[rowData.Length];
DataRow newRow = tbl.NewRow();
//int ScheduleidNdx=newRow.Table.Columns["Scheduleid"].Ordinal
//Find column headers being copied withh their column postion (ordinal) where
// values are to be copied
for (int i = 0; i < rowData.Length; i++)
{
try
{
rowData = rowData.Trim();
fldOrdinals = newRow.Table.Columns[rowData].Ordinal;
}
catch (Exception)
{
MessageBox.Show(rowData + ":Column not in Table:" + newRow.Table.TableName + "\nCorrect it");
return;
}
}
//Delete New Blank row at the bottom of copy source if its IsNullOrWhiteSpace
if (data.Count > 0 && string.IsNullOrWhiteSpace(data[data.Count - 1]))
{
data.RemoveAt(data.Count - 1);
}
//data copy from remaining rows as per their ordinals (index)
int j = -1;
string failedrows = "";
foreach (string iterationRow in data)
{
if (firstRow)
{
firstRow = false;
continue;
}
j++;
string row = iterationRow;
if (row.EndsWith("\r"))
{
row = row.Substring(0, row.Length - "\r".Length);
}
//rowData = row.Split(new char[] { '\r', '\x09' });
rowData = row.Split('\t');
if (rowData.Length != fldOrdinals.Length)
{
failedrows += "Row:" + j + " Import Skiped\n";
continue;
}
newRow = tbl.NewRow();
for (int i = 0; i < rowData.Length; i++)
{
if (i >= tbl.Columns.Count)
break;
if (rowData != null)
{
try
{
newRow[fldOrdinals] = rowData;
}
catch (Exception ex)
{
failedrows += j + " Row " + newRow.Table.Columns[fldOrdinals].ColumnName + ":Skipped";
}
}
}
tbl.Rows.Add(newRow);
//}
}
if (!string.IsNullOrWhiteSpace(failedrows))
MessageBox.Show(failedrows);
}
catch (FormatException)
{
MessageBox.Show("The data you pasted is in the wrong format for the cell");
}
}
some time i get error on following line, for example if newRow fields datatype is int and pasted rowData is string, it gives error and value do not get applied to newRow[fldOrdinals].
how can i convert runtime rowData value to newRow[fldOrdinal] datatype ?
newRow[fldOrdinals] = rowData;
Continue reading...