473,491 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Copying record selected in a datagrid view

Hi

I hav e a smallish app that has a datagridview. A user can select some
columns in the datagrid, and i have a button that i would like to use to
copy the rows that are selected to the clipboard or to a text file.

Any ideas how to accomplish this?

I have all the controls working in this form apart from this feature.

Any sample code would be appreciated.

doug
Jul 30 '06 #1
4 5840
gordon wrote:
Hi

I hav e a smallish app that has a datagridview. A user can select
some columns in the datagrid, and i have a button that i would like
to use to copy the rows that are selected to the clipboard or to a
text file.
Any ideas how to accomplish this?

I have all the controls working in this form apart from this feature.

Any sample code would be appreciated.
See DataGridView.SelectedCells, SelectedRows, SelectedColumns. Which one
you should use depends on how your selections are constrained (are they
always rows? always columns? arbitrary?)

The DataGridView won't help you format the data for clipboard use - that's
something you need to decide on an implement yourself. A good strategy for
grid data is to join together cells from the same row, separated with tabs,
then join together the rows with newlines. Put all of that data on the
clipboard using Clipboard.SetData. That format allows the data to be pasted
directly into Word or Excel and be recognized as a table.

For example, if your selections are always rows:

private void m_copyButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// column headers
sb.Append("Col 1 Title\t");
sb.Append("Col 2 Title\t");
sb.Append("Col 3 Title\t");
sb.Append(Environment.NewLine);
// start concatenating the selected rows together
foreach (DataGridViewRow row in m_dataGridView.SelectedRows)
{
sb.AppendFormat("{0}\t",row.Cells[1].Value);
sb.AppendFormat("{0}\t",row.Cells[2].Value);
sb.AppendFormat("{0}\t",row.Cells[3].Value);
sb.Append(Environment.NewLine);
}
Clipboard.SetData(DataFormats.Text, sb.ToString());
}

-cd
Jul 30 '06 #2
Thanks Carl

Just what i needed. You are right though, the pasted data doesn't look
too hot - but it is usable in word and excel.

Cheers

Doug
Carl Daniel [VC++ MVP] wrote:
gordon wrote:
Hi

I hav e a smallish app that has a datagridview. A user can select
some columns in the datagrid, and i have a button that i would like
to use to copy the rows that are selected to the clipboard or to a
text file.
Any ideas how to accomplish this?

I have all the controls working in this form apart from this feature.

Any sample code would be appreciated.

See DataGridView.SelectedCells, SelectedRows, SelectedColumns. Which one
you should use depends on how your selections are constrained (are they
always rows? always columns? arbitrary?)

The DataGridView won't help you format the data for clipboard use - that's
something you need to decide on an implement yourself. A good strategy for
grid data is to join together cells from the same row, separated with tabs,
then join together the rows with newlines. Put all of that data on the
clipboard using Clipboard.SetData. That format allows the data to be pasted
directly into Word or Excel and be recognized as a table.

For example, if your selections are always rows:

private void m_copyButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// column headers
sb.Append("Col 1 Title\t");
sb.Append("Col 2 Title\t");
sb.Append("Col 3 Title\t");
sb.Append(Environment.NewLine);
// start concatenating the selected rows together
foreach (DataGridViewRow row in m_dataGridView.SelectedRows)
{
sb.AppendFormat("{0}\t",row.Cells[1].Value);
sb.AppendFormat("{0}\t",row.Cells[2].Value);
sb.AppendFormat("{0}\t",row.Cells[3].Value);
sb.Append(Environment.NewLine);
}
Clipboard.SetData(DataFormats.Text, sb.ToString());
}

-cd
Jul 30 '06 #3
Hi again Carl,

In your code, you used "m_dataGridView" for the DataGridView. I have
seen this 'm_' prefix used before but never understood why.

Could you please let me know if this is a c# coding convention, and if
so, what it means?

Thanks

doug
Carl Daniel [VC++ MVP] wrote:
gordon wrote:
Hi

I hav e a smallish app that has a datagridview. A user can select
some columns in the datagrid, and i have a button that i would like
to use to copy the rows that are selected to the clipboard or to a
text file.
Any ideas how to accomplish this?

I have all the controls working in this form apart from this feature.

Any sample code would be appreciated.

See DataGridView.SelectedCells, SelectedRows, SelectedColumns. Which one
you should use depends on how your selections are constrained (are they
always rows? always columns? arbitrary?)

The DataGridView won't help you format the data for clipboard use - that's
something you need to decide on an implement yourself. A good strategy for
grid data is to join together cells from the same row, separated with tabs,
then join together the rows with newlines. Put all of that data on the
clipboard using Clipboard.SetData. That format allows the data to be pasted
directly into Word or Excel and be recognized as a table.

For example, if your selections are always rows:

private void m_copyButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// column headers
sb.Append("Col 1 Title\t");
sb.Append("Col 2 Title\t");
sb.Append("Col 3 Title\t");
sb.Append(Environment.NewLine);
// start concatenating the selected rows together
foreach (DataGridViewRow row in m_dataGridView.SelectedRows)
{
sb.AppendFormat("{0}\t",row.Cells[1].Value);
sb.AppendFormat("{0}\t",row.Cells[2].Value);
sb.AppendFormat("{0}\t",row.Cells[3].Value);
sb.Append(Environment.NewLine);
}
Clipboard.SetData(DataFormats.Text, sb.ToString());
}

-cd
Jul 31 '06 #4
qu********@hotmail.com wrote:
Hi again Carl,

In your code, you used "m_dataGridView" for the DataGridView. I have
seen this 'm_' prefix used before but never understood why.

Could you please let me know if this is a c# coding convention, and
if
so, what it means?
It's a naming convention that I use - m_ denotes a member variable. Some
people like it, others don't. The only important convention is to be
consistent.

-cd
Jul 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2249
by: Megan | last post by:
Hi everybody- I inherited a database that somehow uses a bound combo box as a record selector. Let me give you some background. The form is based on data from 2 tables. The first table, Person,...
19
3441
by: davidgordon | last post by:
Hi, I need some pointers/help on how to do the following if it possible: In my access db, I have the following: Tables: Products, Sub-Assembly, Product-Pack Table, Products
1
1715
by: Eugene | last post by:
Hello, I'm stuck with this question: Lets say I have a DataView with 10 columns, 5 out of which are displayed in DataGrid. Record Id is not displayed - so how do I figure out the Record Id of...
1
2488
by: David | last post by:
What I envision is this. The user gets a set of records in a data grid Then the user gets to select one of the rows to view the detail information about the record. Then instead of backing...
5
2455
by: Nathan Sokalski | last post by:
I am writing an ASP.NET application in which I need to copy DataRows from one DataTable to another. When I use code such as the following: temprows = nodes.Select("state='PA'")...
3
2005
by: pjcraig | last post by:
This is driving me crazy! I have a form that a user will access from another form by selecting the item that they wish to view. When they open the new form, I pass through the id of the item they...
0
1092
by: Iain | last post by:
Hi All I have a datagrid which takes it's data from a table on a remote i-series. This datagrid is loaded on Page Load and the user is able to select a record to edit by pressing the...
0
1439
by: dave k | last post by:
I want to copy data from selected fields in a record on a tabular form (called 'ProductUpdate') to the next record when those two records have a common field - called 'BaseFund' . The selected...
7
1680
by: Hemang Shah | last post by:
I think what i'm trying to do is passing by value. Here is the scenario: Main Form - which calls a Search form. - I pass Mainform as a reference so that I can pass data between the two. Code:...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.