How can I copy to the clipboard the contents of cells in a DataGri

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a DataGridView from Windows.Forms and need to be able to copy the
contents of the current cell to the clipboard, yet have not found the way to
do so.

Any pointers will be very appreciated.
 
Hi,
Following code i have written is doing exactly what you asked for.In this
code there is one datagridview,one button called btnCopyToClipBoard to copy
content of current cell to clipboard,another button called
btnDisplayClipBoardData to display data fron clipboard.:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

DataTable dt = new DataTable();
DataColumn col = new DataColumn();
col.DataType = typeof(System.String);
DataColumn col1 = new DataColumn();
col1.DataType = typeof(System.String);
dt.Columns.Add(col);
dt.Columns.Add(col1);

DataRow dr1 = dt.NewRow();
DataRow dr2 = dt.NewRow();
dr1[0] = "Manish";
dr1[1] = "Bafna";
dt.Rows.Add(dr1);

dr2[0] = "Sanjay";
dr2[1] = "Bafna";

dt.Rows.Add(dr2);
dt.AcceptChanges();

dataGridView1.DataSource = dt;


}

private void btnDisplayClipBoardData_Click(object sender, EventArgs e)
{
IDataObject iData = Clipboard.GetDataObject();
//Determine whether the data is in a format you can use.
if (iData.GetDataPresent(DataFormats.Text))
{
string str = (String)iData.GetData(DataFormats.Text);
MessageBox.Show(str);
}
}

private void btnCopyToClipBoard_Click(object sender, EventArgs e)
{
//the second parameter indicates that data copied to clipboard will remain
//there even after one exists application

Clipboard.SetDataObject(dataGridView1.CurrentCell.Value.ToString(),true);

}

}
}
please do let know if this is what you were looking for.Have a nice day.
 
Yes this helps a lot. I misstated my question slightly though and what I was
really trying to do was make a cell editable (by pressing the traditional F2)
and then copy that content to the clipboard.

How can I make the cells editable? I have marked the grid editable but that
does not seem to make the cells editable...


--
Thanks in advance,

Juan Dent, M.Sc.


Manish Bafna said:
Hi,
Following code i have written is doing exactly what you asked for.In this
code there is one datagridview,one button called btnCopyToClipBoard to copy
content of current cell to clipboard,another button called
btnDisplayClipBoardData to display data fron clipboard.:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

DataTable dt = new DataTable();
DataColumn col = new DataColumn();
col.DataType = typeof(System.String);
DataColumn col1 = new DataColumn();
col1.DataType = typeof(System.String);
dt.Columns.Add(col);
dt.Columns.Add(col1);

DataRow dr1 = dt.NewRow();
DataRow dr2 = dt.NewRow();
dr1[0] = "Manish";
dr1[1] = "Bafna";
dt.Rows.Add(dr1);

dr2[0] = "Sanjay";
dr2[1] = "Bafna";

dt.Rows.Add(dr2);
dt.AcceptChanges();

dataGridView1.DataSource = dt;


}

private void btnDisplayClipBoardData_Click(object sender, EventArgs e)
{
IDataObject iData = Clipboard.GetDataObject();
//Determine whether the data is in a format you can use.
if (iData.GetDataPresent(DataFormats.Text))
{
string str = (String)iData.GetData(DataFormats.Text);
MessageBox.Show(str);
}
}

private void btnCopyToClipBoard_Click(object sender, EventArgs e)
{
//the second parameter indicates that data copied to clipboard will remain
//there even after one exists application

Clipboard.SetDataObject(dataGridView1.CurrentCell.Value.ToString(),true);

}

}
}
please do let know if this is what you were looking for.Have a nice day.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



Juan Dent said:
Hi,

I have a DataGridView from Windows.Forms and need to be able to copy the
contents of the current cell to the clipboard, yet have not found the way to
do so.

Any pointers will be very appreciated.
 
Hi,
So on pressing F12 you want to copy contents of current cell to
clipboard.For that you need to overide ProcessKeyPreview.Add following code
to your existing code:

protected override bool ProcessKeyPreview(ref Message m)
{

KeyEventArgs args1 = new KeyEventArgs(((Keys)((int)m.WParam)) |
Control.ModifierKeys);

if (args1.KeyCode == Keys.F12)
{

Clipboard.SetDataObject(dataGridView1.CurrentCell.Value.ToString(), true);
return false;
}
return base.ProcessKeyPreview(ref m);
}
--
If you any questions then please free to ask me
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
 
Hi,
Also in DataGidView cells are editable by default.You need to do anything to
make them editable.In my DataGridView i have not done anything and still
cells are editable.If you are still facing problems then you can post your
code here
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
 
Hi,
As cells in datagridview are editable by default but somehow if in your case
it is not working then you can try following modified ProcessKeyPreview.What
i have done is on pressing F12 i make cells editable(that is set readonly
property to false) and thereafter on pressing F11 i copy contents of cell to
clipboard and make that cell again non-editable.I think this is what you were
looking for:
protected override bool ProcessKeyPreview(ref Message m)
{

KeyEventArgs args1 = new KeyEventArgs(((Keys)((int)m.WParam)) |
Control.ModifierKeys);

if (args1.KeyCode == Keys.F12)
{
dataGridView1.CurrentCell.ReadOnly = false ;
return false;
}
if (args1.KeyCode == Keys.F11)
{

Clipboard.SetDataObject(dataGridView1.CurrentCell.Value.ToString(), true);
dataGridView1.CurrentCell.ReadOnly = true;
return false;
}
return base.ProcessKeyPreview(ref m);
}
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
 
My data grid view is bound to List<> collections so maybe that's why I can't
order by columns nor edit cell contents...

any ideas?
 
Hi,
You are correct when we bind List generic collection to datagrid then sells
are neither editable nor datagrid is sortable.The best solution is to use the
BindingList<T> type (generics). This generic list can be bound to the
various components (DataGrid, DataGridView, and so on).However The
BindingList<T> class is a much more heavyweight class compared to the List<T>
class. So, unless you really need the extended capabilities offered by
BindingList<T>, you’re better off keeping all the optimization that’s offered
by the List<T> class.
Check out the Windows Forms DataBinding FAQ for more details on this.Search
for BindingList:
http://www.windowsforms.net/Samples/Go To Market/Data Binding/DataBinding FAQ.doc
As shown in avove link i added following code in form constructor:
public Form1()
{
InitializeComponent();

BindingList<Customer> blc = new BindingList<Customer>();

blc.Add(new Customer(0, "Mr. Zero", 10.0M));
blc.Add(new Customer(1, "Mrs. One", 15.0M));
blc.Add(new Customer(2, "Dr. Two", 20.0M));


/*******************************************************************
* Bind the DataGridView to the list of Customers using Complex
* binding (customer business type is shown above).

******************************************************************/

this.dataGridView1.DataSource = blc;
}
I also added customer class.And now DataGridView is editable.But the
datagridview is still not sortable.While BindingList(Of T) implements the
IBindingList interface, it does not automatically implement the sorting
features.For more details on how to enable sorting refer below link.Look for
Figure 12 in below link:
http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/
Please do let me know if this has solved your query.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
 
Back
Top