my winform app contains a combo box, a button and two datagridview among other controls. Both DGV's have 3 columns. When I select an item/items from one or both of the DGV's and hit the button the app would create/open a word file(*.docx) and first add a header and a date on the top,then add a table with the selected DGV rows with a sum of the 3rd DGV column in a new page.
Example 1 (only 1st DGV items are selected)
The resulting word file should look like this
Example 2 (both DGV items are selected)
Then the resulting word file should look like
The code I've done, the rough idea behind it is like this (its messy, badly organized and redundant and needs refinement):
Check if any row/rows from the 2nd DGV is chosen or not, if yes, you cannot select more than 3 in the 2nd DGV & also you cannot select more than 8 rows in the 1st DGV when any item from the 2nd DGV is chosen.
If the word file exists open it or create a new file
Calculate the sum of the 3 rd columns of the selected rows of both the DGV's and Calculate their difference (DGV1-DGV2)
Add the selected rows in the word file in table format (see image below)
If no row/rows from the 2nd DGV is chosen, Check how many rows from the 1st DGV are selected. Minimum 1 item and/or maximum 15 items can be chosen. Then follow the steps from 2-4, only calculate sum of the 3rd columns of selected rows of DGV1 in this case.
string fileName=@"G:\forwarding.docx"; if (kryptonDataGridView2.SelectedRows.Count > 0) { if (kryptonDataGridView2.SelectedRows.Count > 3) { MessageBox.Show(this,"Please select 3 or less Credit/Debit notes","Selection Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); return; } if (kryptonDataGridView1.SelectedRows.Count > 0 && kryptonDataGridView1.SelectedRows.Count > 8) { MessageBox.Show(this,"Please select atleast 1 bill and/or maximum 8 bills","Selection Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } else { decimal total = kryptonDataGridView1.SelectedRows.OfType<DataGridViewRow>() .Sum(tt => Convert.ToDecimal(tt.Cells[2].Value)); decimal _total = kryptonDataGridView2.SelectedRows.OfType<DataGridViewRow>() .Sum(tt => Convert.ToDecimal(tt.Cells[2].Value)); if (_total > total) { MessageBox.Show(this,"Credit/Debit note total value cannot be higher than total bill value","Selection Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); return; } decimal adj_total=total - _total; if (File.Exists(fileName)) { using( var document = DocX.Load( fileName ) ) { document.AddHeaders(); Xceed.Document.NET.Paragraph para=document.InsertParagraph(); para.InsertPageBreakBeforeSelf(); Xceed.Document.NET.Header header_default = document.Headers.Odd; Xceed.Document.NET.Paragraph p1 = header_default.InsertParagraph(); p1.FontSize(20).Append("W").Bold().Font("Arial").Color(Color.Red); p1.FontSize(20).Append("I").Bold().Font("Arial").Color(Color.Brown); p1.FontSize(20).Append("Z").Bold().Font("Arial").Color(Color.Gold); p1.FontSize(20).Append("D").Bold().Font("Arial").Color(Color.Chocolate); p1.FontSize(20).Append("E").Bold().Font("Arial").Color(Color.Teal); p1.FontSize(20).Append("R").Bold().Font("Arial").Color(Color.Violet); p1.FontSize(20).Append("M").Bold().Font("Arial").Color(Color.Orange); p1.Alignment=Alignment.center; p1.FontSize(20); Xceed.Document.NET.Paragraph fdate=document.InsertParagraph(); fdate.Append(" Date: "+DateTime.Today.ToString("dd-MMM-yyyy")); fdate.Alignment=Alignment.right; Xceed.Document.NET.Paragraph blank=document.InsertParagraph(); var tab = document.AddTable( 1, 3 ); tab.Design = TableDesign.TableGrid; tab.Alignment=Alignment.center; tab.Rows[0].MergeCells(0,2); tab.AutoFit=AutoFit.Contents; tab.Rows[0].Cells[0].Paragraphs.First().Append(kryptonComboBox1.Text).Bold().Alignment=Alignment.center; document.InsertTable( tab ); var table = document.AddTable( kryptonDataGridView1.SelectedRows.Count+3, kryptonDataGridView1.Columns.Count ); table.Design = TableDesign.TableGrid; table.Alignment=Alignment.center; table.AutoFit = AutoFit.Contents; int rowNumber = 0; int columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(column.HeaderText).Bold(); columnNumber++; } rowNumber++; foreach (DataGridViewRow row in kryptonDataGridView1.SelectedRows.OfType<DataGridViewRow>().OrderBy(c=>c.Index)) { columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(row.Cells[columnNumber].Value.ToString()); columnNumber++; } rowNumber++; } table.Rows[kryptonDataGridView1.SelectedRows.Count+1].Cells[2].Paragraphs.First().Append(total.ToString()).Bold(true); table.Rows[kryptonDataGridView1.SelectedRows.Count+1].MergeCells(0,1); table.Rows[kryptonDataGridView1.SelectedRows.Count+2].Cells[0].Paragraphs.First().Append("Credit/Debit Note").Bold(true).Alignment=Alignment.center; table.Rows[kryptonDataGridView1.SelectedRows.Count+2].MergeCells(0,2); document.InsertTable(table); var cndn_table = document.AddTable( kryptonDataGridView2.SelectedRows.Count+3, kryptonDataGridView2.Columns.Count ); cndn_table.Design = TableDesign.TableGrid; cndn_table.Alignment=Alignment.center; cndn_table.AutoFit = AutoFit.Contents; int _rowNumber = 0; int _columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView2.Columns) { cndn_table.Rows[_rowNumber].Cells[_columnNumber].Paragraphs.First().Append(column.HeaderText).Bold(); _columnNumber++; } _rowNumber++; foreach (DataGridViewRow row in kryptonDataGridView2.SelectedRows.OfType<DataGridViewRow>().OrderBy(c=>c.Index)) { _columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView2.Columns) { cndn_table.Rows[_rowNumber].Cells[_columnNumber].Paragraphs.First().Append(row.Cells[_columnNumber].Value.ToString()); _columnNumber++; } _rowNumber++; } cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+1].Cells[2].Paragraphs.First().Append(_total.ToString()).Bold(true); cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+1].MergeCells(0,1); cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+2].Cells[0].Paragraphs.First().Append("Total after adjustment :").Bold(true).Alignment=Alignment.right; cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+2].Cells[2].Paragraphs.First().Append(adj_total.ToString()).Bold(true); cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+2].MergeCells(0,1); document.InsertTable(cndn_table); document.Save(); } } else { using( var document = DocX.Create( fileName ) ) { document.AddHeaders(); Xceed.Document.NET.Paragraph para=document.InsertParagraph(); Xceed.Document.NET.Header header_default = document.Headers.Odd; Xceed.Document.NET.Paragraph p1 = header_default.InsertParagraph(); p1.FontSize(20).Append("W").Bold().Font("Arial").Color(Color.Red); p1.FontSize(20).Append("I").Bold().Font("Arial").Color(Color.Brown); p1.FontSize(20).Append("Z").Bold().Font("Arial").Color(Color.Gold); p1.FontSize(20).Append("D").Bold().Font("Arial").Color(Color.Chocolate); p1.FontSize(20).Append("E").Bold().Font("Arial").Color(Color.Teal); p1.FontSize(20).Append("R").Bold().Font("Arial").Color(Color.Violet); p1.FontSize(20).Append("M").Bold().Font("Arial").Color(Color.Orange); p1.Alignment=Alignment.center; p1.FontSize(20); Xceed.Document.NET.Paragraph fdate=document.InsertParagraph(); fdate.Append(" Date: "+DateTime.Today.ToString("dd-MMM-yyyy")); fdate.Alignment=Alignment.right; Xceed.Document.NET.Paragraph blank=document.InsertParagraph(); var tab = document.AddTable( 1, 3 ); tab.Design = TableDesign.TableGrid; tab.Alignment=Alignment.center; tab.Rows[0].MergeCells(0,2); tab.AutoFit=AutoFit.Contents; tab.Rows[0].Cells[0].Paragraphs.First().Append(kryptonComboBox1.Text).Bold().Alignment=Alignment.center; document.InsertTable( tab ); var table = document.AddTable( kryptonDataGridView1.SelectedRows.Count+3, kryptonDataGridView1.Columns.Count ); table.Design = TableDesign.TableGrid; table.Alignment=Alignment.center; table.AutoFit = AutoFit.Contents; int rowNumber = 0; int columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(column.HeaderText).Bold(); columnNumber++; } rowNumber++; foreach (DataGridViewRow row in kryptonDataGridView1.SelectedRows.OfType<DataGridViewRow>().OrderBy(c=>c.Index)) { columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(row.Cells[columnNumber].Value.ToString()); columnNumber++; } rowNumber++; } table.Rows[kryptonDataGridView1.SelectedRows.Count+1].Cells[2].Paragraphs.First().Append(total.ToString()).Bold(true); table.Rows[kryptonDataGridView1.SelectedRows.Count+1].MergeCells(0,1); table.Rows[kryptonDataGridView1.SelectedRows.Count+2].Cells[0].Paragraphs.First().Append("Credit/Debit Note").Bold(true).Alignment=Alignment.center; table.Rows[kryptonDataGridView1.SelectedRows.Count+2].MergeCells(0,2); document.InsertTable(table); var cndn_table = document.AddTable( kryptonDataGridView2.SelectedRows.Count+3, kryptonDataGridView2.Columns.Count ); cndn_table.Design = TableDesign.TableGrid; cndn_table.Alignment=Alignment.center; cndn_table.AutoFit = AutoFit.Contents; int _rowNumber = 0; int _columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView2.Columns) { cndn_table.Rows[_rowNumber].Cells[_columnNumber].Paragraphs.First().Append(column.HeaderText).Bold(); _columnNumber++; } _rowNumber++; foreach (DataGridViewRow row in kryptonDataGridView2.SelectedRows.OfType<DataGridViewRow>().OrderBy(c=>c.Index)) { _columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView2.Columns) { cndn_table.Rows[_rowNumber].Cells[_columnNumber].Paragraphs.First().Append(row.Cells[_columnNumber].Value.ToString()); _columnNumber++; } _rowNumber++; } cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+1].Cells[2].Paragraphs.First().Append(_total.ToString()).Bold(true); cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+1].MergeCells(0,1); cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+2].Cells[0].Paragraphs.First().Append("Total after adjustment :").Bold(true).Alignment=Alignment.right; cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+2].Cells[2].Paragraphs.First().Append(adj_total.ToString()).Bold(true); cndn_table.Rows[kryptonDataGridView2.SelectedRows.Count+2].MergeCells(0,1); document.InsertTable(cndn_table); document.Save(); } } MessageBox.Show(this,"Done","Info",MessageBoxButtons.OK,MessageBoxIcon.Information); kryptonDataGridView1.Rows.Clear(); kryptonDataGridView2.Rows.Clear(); } } else { if (kryptonDataGridView1.SelectedRows.Count < 1 || kryptonDataGridView1.SelectedRows.Count > 15) { MessageBox.Show(this,"Please select minimum 1 item and/or maximum 15 items","Selection Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } else { if (File.Exists(fileName)) { using( var document = DocX.Load( fileName ) ) { document.AddHeaders(); Xceed.Document.NET.Paragraph para=document.InsertParagraph(); para.InsertPageBreakBeforeSelf(); Xceed.Document.NET.Header header_default = document.Headers.Odd; Xceed.Document.NET.Paragraph p1 = header_default.InsertParagraph(); p1.FontSize(20).Append("W").Bold().Font("Arial").Color(Color.Red); p1.FontSize(20).Append("I").Bold().Font("Arial").Color(Color.Brown); p1.FontSize(20).Append("Z").Bold().Font("Arial").Color(Color.Gold); p1.FontSize(20).Append("D").Bold().Font("Arial").Color(Color.Chocolate); p1.FontSize(20).Append("E").Bold().Font("Arial").Color(Color.Teal); p1.FontSize(20).Append("R").Bold().Font("Arial").Color(Color.Violet); p1.FontSize(20).Append("M").Bold().Font("Arial").Color(Color.Orange); p1.Alignment=Alignment.center; p1.FontSize(20); Xceed.Document.NET.Paragraph fdate=document.InsertParagraph(); fdate.Append(" Date: "+DateTime.Today.ToString("dd-MMM-yyyy")); fdate.Alignment=Alignment.right; Xceed.Document.NET.Paragraph blank=document.InsertParagraph(); var tab = document.AddTable( 1, 3 ); tab.Design = TableDesign.TableGrid; tab.Alignment=Alignment.center; tab.Rows[0].MergeCells(0,2); tab.AutoFit=AutoFit.Contents; tab.Rows[0].Cells[0].Paragraphs.First().Append(kryptonComboBox1.Text).Bold().Alignment=Alignment.center; document.InsertTable( tab ); var table = document.AddTable( kryptonDataGridView1.SelectedRows.Count+2, kryptonDataGridView1.Columns.Count ); table.Design = TableDesign.TableGrid; table.Alignment=Alignment.center; table.AutoFit = AutoFit.Contents; int rowNumber = 0; int columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(column.HeaderText).Bold(); columnNumber++; } rowNumber++; foreach (DataGridViewRow row in kryptonDataGridView1.SelectedRows.OfType<DataGridViewRow>().OrderBy(c=>c.Index)) { columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(row.Cells[columnNumber].Value.ToString()); columnNumber++; } rowNumber++; } decimal total = kryptonDataGridView1.SelectedRows.OfType<DataGridViewRow>() .Sum(tt => Convert.ToDecimal(tt.Cells[2].Value)); table.Rows[kryptonDataGridView1.SelectedRows.Count+1].Cells[0].Paragraphs.First().Append("Total :").Bold(true).Alignment=Alignment.right; table.Rows[kryptonDataGridView1.SelectedRows.Count+1].Cells[2].Paragraphs.First().Append(total.ToString()).Bold(true); table.Rows[kryptonDataGridView1.SelectedRows.Count+1].MergeCells(0,1); document.InsertTable(table); document.Save(); } } else { using( var document = DocX.Create( fileName ) ) { document.AddHeaders(); Xceed.Document.NET.Paragraph para=document.InsertParagraph(); Xceed.Document.NET.Header header_default = document.Headers.Odd; Xceed.Document.NET.Paragraph p1 = header_default.InsertParagraph(); p1.FontSize(20).Append("W").Bold().Font("Arial").Color(Color.Red); p1.FontSize(20).Append("I").Bold().Font("Arial").Color(Color.Brown); p1.FontSize(20).Append("Z").Bold().Font("Arial").Color(Color.Gold); p1.FontSize(20).Append("D").Bold().Font("Arial").Color(Color.Chocolate); p1.FontSize(20).Append("E").Bold().Font("Arial").Color(Color.Teal); p1.FontSize(20).Append("R").Bold().Font("Arial").Color(Color.Violet); p1.FontSize(20).Append("M").Bold().Font("Arial").Color(Color.Orange); p1.Alignment=Alignment.center; p1.FontSize(20); Xceed.Document.NET.Paragraph fdate=document.InsertParagraph(); fdate.Append(" Date: "+DateTime.Today.ToString("dd-MMM-yyyy")); fdate.Alignment=Alignment.right; Xceed.Document.NET.Paragraph blank=document.InsertParagraph(); var tab = document.AddTable( 1, 3 ); tab.Design = TableDesign.TableGrid; tab.Alignment=Alignment.center; tab.Rows[0].MergeCells(0,2); tab.AutoFit=AutoFit.Contents; tab.Rows[0].Cells[0].Paragraphs.First().Append(kryptonComboBox1.Text).Bold().Alignment=Alignment.center; document.InsertTable( tab ); var table = document.AddTable( kryptonDataGridView1.SelectedRows.Count+2, kryptonDataGridView1.Columns.Count ); table.Design = TableDesign.TableGrid; table.Alignment=Alignment.center; table.AutoFit = AutoFit.Contents; int rowNumber = 0; int columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(column.HeaderText).Bold(); columnNumber++; } rowNumber++; foreach (DataGridViewRow row in kryptonDataGridView1.SelectedRows.OfType<DataGridViewRow>().OrderBy(c=>c.Index)) { columnNumber = 0; foreach (DataGridViewColumn column in kryptonDataGridView1.Columns) { table.Rows[rowNumber].Cells[columnNumber].Paragraphs.First().Append(row.Cells[columnNumber].Value.ToString()); columnNumber++; } rowNumber++; } decimal total = kryptonDataGridView1.SelectedRows.OfType<DataGridViewRow>() .Sum(tt => Convert.ToDecimal(tt.Cells[2].Value)); table.Rows[kryptonDataGridView1.SelectedRows.Count+1].Cells[0].Paragraphs.First().Append("Total :").Bold(true).Alignment=Alignment.right; table.Rows[kryptonDataGridView1.SelectedRows.Count+1].Cells[2].Paragraphs.First().Append(total.ToString()).Bold(true); table.Rows[kryptonDataGridView1.SelectedRows.Count+1].MergeCells(0,1); document.InsertTable(table); document.Save(); } } MessageBox.Show(this,"Done","Info",MessageBoxButtons.OK,MessageBoxIcon.Information); } }