By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,068 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,068 IT Pros & Developers. It's quick & easy.

Copying record selected in a datagrid view

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.