How to print data from datagridview C#

I’m trying to print data from a datagridview in pos format…tried some code examples but none was based on datagridview

For the future, it helps to specify the UI when asking about UI things such as this. Since it is the DataGridView I will assume this is for Windows Forms. Also it helps to know how you are putting data into it so the answer can suggest something corresponding to how you do that.

The beginner solution might be to use the DataGridView.Rows Property to put the data in and get the data out. The Rows collection is a list of DataGridViewRow objects and each row object has a DataGridViewRow.Cells Property. If that is how you put the data in then that is how you can get the data out.

Experienced developers seldom do that. They typically create a data source with the data in it. Then instead of accessing the DataGridView directly they access the collection they have used for the data source.

I do not know how much you know about classes and properties in classes and collections of class objects. I apologize if the following is too advanced for you.

Suppose you have a class classSomething with properties. You can create a list of the objects using:

List<classSomething> Somethings = null;

Then you can create a BindingSource and set it’s DataSource to the list, then set the DataGridView’s DataSource to the BindingSource, as in the following.

BindingSource bs = new BindingSource();
bs.DataSource = Somethings;
dgv.DataSource = bs;

So then you can access the items in the Somethings list instead of accessing the DataGridView directly.

Do you understand all that? If you search for articles relevant to any of that then you will find much information. You can also ask here but try to use other resources too.

I see that after asking here you asked this same question in another forum. In that thread Karen asked for clarification, such as how the data is loaded (as I did) and you said that the data is loaded through DataTable. So the DataTable is the data source. You can access it to get the data. I suggest ignoring the suggestion from the Microsoft representative, those Microsoft representatives have gotten … well I just say it is not what experienced .Net programmers would do.

Thanks, I appreciate your response and no it wasn’t too advanced .
I’m working on it now …thanks again.