i want to print datagridview printing by user selecting rows and columns below code is only first and second rows and colomns text
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Font fntString = new Font("Times New Roman", 10, FontStyle.Bold);
//first row values
e.Graphics.DrawString("Id : ", fntString, Brushes.Black, 24, 70);
e.Graphics.DrawString(dataGridView1.Rows[0].Cells[0].Value.ToString(), fntString, Brushes.Black, 100, 70);
e.Graphics.DrawString("Name : ", fntString, Brushes.Black, 24, 90);
e.Graphics.DrawString(dataGridView1.Rows[0].Cells[1].Value.ToString(), fntString, Brushes.Black, 100, 90);
//second row values
e.Graphics.DrawString("Id : ", fntString, Brushes.Black, 180, 70);
e.Graphics.DrawString(dataGridView1.Rows[0].Cells[0].Value.ToString(), fntString, Brushes.Black, 220, 70);
e.Graphics.DrawString("Name: ", fntString, Brushes.Black, 180, 90);
e.Graphics.DrawString(dataGridView1.Rows[0].Cells[1].Value.ToString(), fntString, Brushes.Black, 220, 90);
}
Hide Copy Code
i want to print like above code but selecting multiple or single row and colomns.
please help.iam stuck here.
thankyou
Partha SarathiPosted Oct 6, 2015, 2:14 AM
Use the http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectedcolumns.aspx property. To enable users to select columns, you must set the http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode.aspxproperty to FullColumnSelect or ColumnHeaderSelect. here is a good code for you
privatevoidselectedColumnsButton_Click(objectsender, System.EventArgs e){Int32 selectedColumnCount = dataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Selected);if(selectedColumnCount > 0){System.Text.StringBuilder sb =newSystem.Text.StringBuilder();for(inti = 0; i < selectedColumnCount; i++){sb.Append("Column: ");sb.Append(dataGridView1.SelectedColumns[i].Index.ToString());sb.Append(Environment.NewLine);}sb.Append("Total: "+ selectedColumnCount.ToString());MessageBox.Show(sb.ToString(),"Selected Columns");}}here is one more good link to print the selected rows of gridview.
http://www.codeproject.com/KB/grid/PrintDataGridView.aspx
Hope this will help you
Nilesh JadavPosted Oct 6, 2015, 2:00 AM