PDA

Click to See Complete Forum and Search --> : [Winforms] Datagrid with cut and paste


BrightSoul
Sep 1st, 2004, 09:30 AM
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

Danial
Sep 2nd, 2004, 06:49 AM
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.


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);
}
}

}
}

}

BrightSoul
Sep 2nd, 2004, 07:53 AM
wow, much appreciated! this is just what I've been looking for. It also makes me understand how to handle different data types stored in the Clipboard, thanks.

One last question:
you are pasting data into new rows:

dr = dt.NewRow();
dr[0]= info[0];
dr[1]= info[1];

and while this is nice, I would also like to implement a feature that OVERWRITES the contents of the cells the user selected. i.e. I may want to start pasting from existent cell 2,3. If none are selected, than start pasting at 0,0 OR insert data as new rows.

How do I get the cell selection boundaries from a datagrid? I just need 4 numbers: start from cell 0,1; end to cell 2,3.

I just need a guideline, then I can help myself using a classbrowser. Thanks again :)

Danial
Sep 2nd, 2004, 08:07 AM
This will over write the data in selected cell.


private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();

dt=(DataTable)this.dataGrid1.DataSource;

int c;
int r;
r = this.dataGrid1.CurrentCell.RowNumber;
c = this.dataGrid1.CurrentCell.ColumnNumber;

dt.Rows[r][c]="Hi ";

}