LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2006, 08:18 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
How to print a data grid in visual C#?


Does anyone know how to print a data grid in visual C#? My particular datagrid has close to 50 columns. I have tried the code on MSDN, c sharp corner, and code project but niether seem to account for the additional columnspast the viewable area on the form of the data grid. Hence only 10 columns actually print. Can anyone help me with this?
 
Old 04-25-2006, 12:11 PM   #2
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Why not just print it plain-text tab delimited data?
 
Old 04-26-2006, 07:13 AM   #3
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
How would you go about do that? I am not familiar with that kind of thing.
 
Old 04-26-2006, 01:15 PM   #4
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Well, its pretty simple.

Code:
DataTable dt = (DataTable)dataGrid.DataSource;

string output = "";
foreach(DataRow dr in dt.Rows)
{
      bool isFirst = true;
      foreach(DataColumn dc in dt.Columns)
      {
           if(!isFirst)
               output += "\t";
           output += dr[dc].ToString();
           isFirst = false;
      }
      output += "\r\n"; 
}
Something along those lines.

Last edited by Vagrant; 04-26-2006 at 01:19 PM.
 
Old 04-27-2006, 10:04 AM   #5
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Here is the code I am trying to use. I am getting a specified cast is not valid error on the first line of code. What do you see that may be causing this. Is there anything that I need to change or add. Also what will this look like when completed and printed?
Code:
System.Data.DataTable dt = (System.Data.DataTable)dataGrid.DataSource;
			
			string output = "";
			foreach(DataRow dr in dt.Rows)
			{
				bool isFirst = true;
				foreach(DataColumn dc in dt.Columns)
				{
					if(!isFirst)
						output += "\t";
					output += dr[dc].ToString();
					isFirst = false;
				}
				output += "\r\n";
}
 
Old 04-27-2006, 12:47 PM   #6
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Well, it depends on what exactly the dataGrid.DataSource is. You need to know that before you continue with this part of the code. Perhaps it is an entire dataset, which would mean you would have to change the code to something like this:

System.Data.DataTable dt = ((System.Data.DataSet)dataGrid.DataSource).Tables[0]; // or which ever table you need
 
Old 04-28-2006, 07:55 AM   #7
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Ok, I am making progress, however I still cannot print. I have populated output but I now need to have a nice and neat printed document formatted with headers as well. How can this be done?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Open Source Grid Community Talks Data Management LXer Syndicated Linux News 0 02-09-2006 06:01 AM
Painting the row of a data grid a color in VB.net mrobertson Programming 1 07-30-2005 04:22 PM
vb.net error when trying to format data grid mrobertson Programming 23 07-29-2005 11:52 AM
appending data to a data grid mrobertson Programming 1 07-28-2005 10:07 PM
appending data to a data grid VB.net mrobertson Programming 6 07-28-2005 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration