Results 1 to 4 of 4

Thread: [Winforms] Datagrid with cut and paste

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89

    [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
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: [Winforms] Datagrid with cut and paste

    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);
    				}
    			}
    
    		}
    	}
    
    }
    Last edited by Danial; Sep 2nd, 2004 at 06:52 AM.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Posts
    89
    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
    - mo! I said MOOOOOOO!!
    - ...yep, that's a cow, alright.

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    This will over write the data in selected cell.

    Code:
    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 ";
    
    }
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width