[Winforms] Datagrid with cut and paste
Hi,
can you advice me with some .NET component similar to a datagrind (with databinding support) that has cut and paste funcionality?
I need to copy data from MS Excel and paste it into my application.
...or a tutotial on how to tweak a common datagrid would do too.
thanks, bye
Re: [Winforms] Datagrid with cut and paste
Quote:
Originally posted by BrightSoul
Hi,
can you advice me with some .NET component similar to a datagrind (with databinding support) that has cut and paste funcionality?
I need to copy data from MS Excel and paste it into my application.
...or a tutotial on how to tweak a common datagrid would do too.
thanks, bye
Here is an simple example i put together, modify it to your need.
Place a Datagrid in you form and paste the following in Form Load and KeyDown event.
Code:
private void Form1_Load(object sender, System.EventArgs e)
{
DataColumn dc;
DataTable dt = new DataTable();
dc = new DataColumn();
dc.ColumnName="First Name";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Last Name";
dt.Columns.Add(dc);
DataRow dr;
dr = dt.NewRow();
dr["First Name"]= "Joe";
dr["Last Name"] = "Blogs";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["First Name"]= "Robert";
dr["Last Name"] = "Frisk";
dt.Rows.Add(dr);
this.dataGrid1.DataSource=dt;
}
private void dataGrid1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.Control && e.KeyCode== Keys.V)
{
DialogResult ret;
ret = MessageBox.Show("Are you sure you want to Paste Data?", "", MessageBoxButtons.YesNoCancel);
if (ret==DialogResult.Yes)
{
IDataObject data = Clipboard.GetDataObject();
string strData;
if (data.GetDataPresent(DataFormats.Text))
{
strData = data.GetData(DataFormats.Text).ToString();
MessageBox.Show(strData);
string [] row;
string [] info;
row = strData.Split('\n');
foreach(string s in row)
{
info=s.Split('\t');
DataRow dr;
DataTable dt = new DataTable();
dt = (DataTable)this.dataGrid1.DataSource;
dr = dt.NewRow();
dr[0]= info[0];
dr[1]= info[1];
dt.Rows.Add(dr);
}
}
}
}
}