473,738 Members | 3,854 Online
Bytes | Software Development & Data Engineering Community
+ 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 5849
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.Se lectedCells, 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.SetDa ta. 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_Cl ick(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(Envir onment.NewLine) ;
// start concatenating the selected rows together
foreach (DataGridViewRo w row in m_dataGridView. SelectedRows)
{
sb.AppendFormat ("{0}\t",row.Ce lls[1].Value);
sb.AppendFormat ("{0}\t",row.Ce lls[2].Value);
sb.AppendFormat ("{0}\t",row.Ce lls[3].Value);
sb.Append(Envir onment.NewLine) ;
}
Clipboard.SetDa ta(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.Se lectedCells, 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.SetDa ta. 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_Cl ick(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(Envir onment.NewLine) ;
// start concatenating the selected rows together
foreach (DataGridViewRo w row in m_dataGridView. SelectedRows)
{
sb.AppendFormat ("{0}\t",row.Ce lls[1].Value);
sb.AppendFormat ("{0}\t",row.Ce lls[2].Value);
sb.AppendFormat ("{0}\t",row.Ce lls[3].Value);
sb.Append(Envir onment.NewLine) ;
}
Clipboard.SetDa ta(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.Se lectedCells, 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.SetDa ta. 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_Cl ick(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(Envir onment.NewLine) ;
// start concatenating the selected rows together
foreach (DataGridViewRo w row in m_dataGridView. SelectedRows)
{
sb.AppendFormat ("{0}\t",row.Ce lls[1].Value);
sb.AppendFormat ("{0}\t",row.Ce lls[2].Value);
sb.AppendFormat ("{0}\t",row.Ce lls[3].Value);
sb.Append(Envir onment.NewLine) ;
}
Clipboard.SetDa ta(DataFormats. Text, sb.ToString());
}

-cd
Jul 31 '06 #4
qu********@hotm ail.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
2266
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, records info about a person. The second table, Case, records information about a person's case, almost like a human resources database. The primary key of the table, person, is his/ her social security number. The primary key of the other...
19
3478
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
1729
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 currently selected row in DataGrid ? Thanks a lot.
1
2501
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 out to the datagrid, press Next or Previous to view the next or previous record respectively. How would you implement it in ASP.NET?
5
2465
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'") temptable.Clear() For Each row As DataRow In temprows temptable.Rows.Add(row) Next
3
2014
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 selected so that only the details for the given item are displayed. In order for this to work, I using the following code.... This is working no problems at all: SqlSelectCommand1.Parameters("@blast_no").Value = blastNo So this displays the...
0
1101
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 appropriate button. No problem so far. The problem comes in that I appear to have to reload the data into the datagrid before placing it into editstate.
0
1454
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 fields I am updating are called 'date' 'cash' 'shares' and 'gilts' (amongst others). After completing all the fields in the first record I would like all the selected fields in the record below to be automatically updated if I double click on...
7
1689
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: public frmSearch(Form1 f)
0
8968
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.