Search This Blog

Monday, March 28, 2011

row.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);

 private void dtGrdVwRFIDTags_DataSourceChanged(object sender, EventArgs e)
    {
        dtGrdVwRFIDTags.Refresh();
        this.dtGrdVwRFIDTags.Columns[1].Visible = false;
        foreach (DataGridViewRow row in this.dtGrdVwRFIDTags.Rows)
        {
            if (row.Cells["TagStatus"].Value != null && row.Cells["TagStatus"].Value.ToString() == "Lost" || row.Cells["TagStatus"].Value != null && row.Cells["TagStatus"].Value.ToString() == "Damaged" || row.Cells["TagStatus"].Value != null && row.Cells["TagStatus"].Value.ToString() == "Discarded")
            {
                row.DefaultCellStyle.BackColor = Color.LightGray;
                row.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
            }
            else
                row.DefaultCellStyle.BackColor = Color.Ivory;
        }
    }

Black Wasp - C#

Black Wasp

Tuesday, March 22, 2011

Create DataTable Dynamically

        public DataTable CreateTempTable()
        {
            DataTable dt = new DataTable();

            DataColumn myDataColumn;

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.Int32");
            myDataColumn.ColumnName = "RequestNo";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "BankCode";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "BSB";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "AccountNo";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "AccountName";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "CBAAddressee";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "WBCAddressee";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.Boolean");
            myDataColumn.ColumnName = "NameVariationRequired";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "NameVariation1";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "NameVariation2";
            dt.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "NameVariation3";
            dt.Columns.Add(myDataColumn);

            DataRow row;
            for (int i = 0; i < dataGridViewOutstandingLetters.Rows.Count; i++)
            {
                if ((bool)dataGridViewOutstandingLetters.Rows[i].Cells[("LetterPrinted")].Value)
                {
                    row = dt.NewRow();

                    row["RequestNo"] = dataGridViewOutstandingLetters.Rows[i].Cells[("RequestNo")].Value;
                    row["BankCode"] = dataGridViewOutstandingLetters.Rows[i].Cells[("BankCode")].Value;
                    row["BSB"] = dataGridViewOutstandingLetters.Rows[i].Cells[("BSB")].Value;
                    row["AccountNo"] = dataGridViewOutstandingLetters.Rows[i].Cells[("AccountNo")].Value;
                    row["AccountName"] = dataGridViewOutstandingLetters.Rows[i].Cells[("AccountName")].Value;
                    row["CBAAddressee"] = Session.cbaAddressee;
                    row["WBCAddressee"] = Session.wbcAddressee;
                    row["NameVariationRequired"] = dataGridViewOutstandingLetters.Rows[i].Cells[("NameVariationRequired")].Value;
                    row["NameVariation1"] = dataGridViewOutstandingLetters.Rows[i].Cells[("NameVariation1")].Value;
                    row["NameVariation2"] = dataGridViewOutstandingLetters.Rows[i].Cells[("NameVariation2")].Value;
                    row["NameVariation3"] = dataGridViewOutstandingLetters.Rows[i].Cells[("NameVariation3")].Value;

                    dt.Rows.Add(row);
                }
            }

            return dt;
        }

Saturday, March 19, 2011

Setup MS Word in C#

 DataView dvQuestion;
        DataTable dtQuestion;
        dvQuestion = ObjD.SelectAllRecords(0, "QuestionId", "ASC");
        dtQuestion = dvQuestion.ToTable();

        //Get page URL
        string strSiteURL = ConfigurationManager.AppSettings["URL"].ToString();
        object oMissing = System.Reflection.Missing.Value;
        object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

        //Start Word and create a new document.
        Microsoft.Office.Interop.Word._Application oWord;
       
        Microsoft.Office.Interop.Word._Document oDoc;
        Microsoft.Office.Interop.Word.Selection wrdSelection;      
       
        oWord = new Microsoft.Office.Interop.Word.Application();

        oWord.Visible = false;
       
        oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        //Add Border
        oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleDot;
        oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleDot;
        oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleDot;
        oDoc.ActiveWindow.Selection.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleDot;


       oDoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;
        //oDoc.PageSetup.LayoutMode = WdLayoutMode.wdLayoutModeLineGrid;
        oDoc.PageSetup.PageWidth = oWord.MillimetersToPoints(85F);
        oDoc.PageSetup.PageHeight = oWord.MillimetersToPoints(250F);
        //oDoc.PageSetup.PageHeight = oWord.MillimetersToPoints(240F);

       

        //Insert a paragraph at the beginning of the document.
        Microsoft.Office.Interop.Word.Paragraph oParaTitle;
        object oRngTitle;
        oRngTitle = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
        oParaTitle = oDoc.Content.Paragraphs.Add(ref oRngTitle);
        oParaTitle.Range.Font.Name = "Comic Sans MS";
        oParaTitle.Range.Text = "1. SET OPERATIONS";
        oParaTitle.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
        oParaTitle.Range.Font.Bold = 10;
        oParaTitle.Range.Font.Size = 12;
        //oPara1.Format.SpaceAfter = 2;    //24 pt spacing after paragraph.
        oParaTitle.Range.InsertParagraphAfter();



        object oRngLine;
        Microsoft.Office.Interop.Word.Paragraph oParaLine;

        oRngLine = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
        oParaLine= oDoc.Content.Paragraphs.Add(ref oRngLine);
        wrdSelection = oWord.Selection;
        wrdSelection.TypeText(oParaLine.Range.Text);
        string strObjLine = strSiteURL + "/images/line.jpg";
        Object oAnsRange = oParaLine.Range;

        oParaLine.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
        oParaLine.Application.Selection.InlineShapes.AddPicture(strObjLine, ref oMissing, ref oMissing, ref oRngLine);
        oParaLine.Range.InsertParagraphAfter();
       

        //Insert a paragraph at the beginning of the document.
        Microsoft.Office.Interop.Word.Paragraph oPara1;
        object oRngHed;
        oRngHed = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
        oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
        oPara1.Range.Text = "<html><body>Question/Answer";
        oPara1.Format.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
        oPara1.Range.Font.Bold = 10;
        oPara1.Range.Font.Size = 18;
        //oPara1.Format.SpaceAfter = 2;    //24 pt spacing after paragraph.
        oPara1.Range.InsertParagraphAfter();

        object  oRngQuestion;
       
        for (int i = 0; i < dtQuestion.Rows.Count; i++)
        {
            //Insert a paragraph at the end of the document.
                Microsoft.Office.Interop.Word.Paragraph oParaChapter;
                oRngQuestion = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                oParaChapter = oDoc.Content.Paragraphs.Add(ref oRngQuestion);
                string strQuestion;
                strQuestion = dtQuestion.Rows[i]["Question"].ToString();
                strQuestion = strQuestion.Replace("../../userfiles", "http://localhost:1490/Test123/userfiles");
                strQuestion = strQuestion.Replace("/Test123/fckeditor", "http://localhost:1490/Test123/fckeditor");

                oParaChapter.Range.Font.Name = "Times New Roman";
                oParaChapter.Range.Text = "<br><strong>" + Server.HtmlDecode(strQuestion) + "</strong>";
                //oParaChapter.Format.SpaceAfter = 6;
                oParaChapter.Range.Font.Bold = 1;
                oParaChapter.Range.Font.Size = 5;
                oParaChapter.Format.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                oParaChapter.Range.InsertParagraphAfter();

                oParaChapter.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
                oParaChapter.LineSpacing = 8F;
                oParaChapter.Format.SpaceAfter = oWord.MillimetersToPoints(0F);
                oParaChapter.Format.SpaceBefore = oWord.MillimetersToPoints(0F);
                oParaChapter.Format.SpaceAfterAuto =0 ;
                oParaChapter.Format.SpaceBeforeAuto = 0 ;
           
                int intQuestionId = Convert.ToInt32(dtQuestion.Rows[i]["QuestionId"].ToString());
                DataView dvAnswer;
                dvAnswer = ObjD.SelectQuestionAnswer(intQuestionId);
                if (dvAnswer.Table.Rows.Count > 0)
                {
                    object  oRngAnswer;
                    for (int j = 0; j < dvAnswer.Table.Rows.Count; j++)
                    {
                        Microsoft.Office.Interop.Word.Paragraph oParaAnswer;
                        oRngAnswer = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        oParaAnswer = oDoc.Content.Paragraphs.Add(ref oRngAnswer);
                        string strAnswer;
                        strAnswer = dvAnswer.Table.Rows[j]["Answer"].ToString();
                        strAnswer = strAnswer.Replace("../../userfiles", "http://localhost:4411/Test123/userfiles");
                        strAnswer = strAnswer.Replace("/Test123/fckeditor", "http://localhost:4411/Test123/fckeditor");
                        oParaAnswer.Range.Font.Name = "Times New Roman";
                        oParaAnswer.Range.Text = "<br>&nbsp;&nbsp;" + Server.HtmlDecode(strAnswer);
                        oParaAnswer.Format.SpaceAfter = 0;
                        oParaAnswer.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
                        oParaAnswer.LineSpacing = 8F;
                        //oParaAnswer.LineSpacing = 0F;
                        oParaAnswer.Format.SpaceAfter = oWord.MillimetersToPoints(0F);
                        oParaAnswer.Format.SpaceBefore = oWord.MillimetersToPoints(0F);
                        oParaAnswer.Format.SpaceAfterAuto = 0;
                        oParaAnswer.Format.SpaceBeforeAuto = 0;
                        oParaAnswer.Range.Font.Size = 7;
                        oParaAnswer.Range.Font.Bold = 0;
                        oParaAnswer.Format.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                        oParaAnswer.Range.InsertParagraphAfter();
                    }
                }
          }
        //Add new text on new page
       
        //insert break, so you can insert a copy of the template as a new page.
          object oRngNewPage;
          Microsoft.Office.Interop.Word.Paragraph oParaNew;
          oRngNewPage = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
          oParaNew = oDoc.Content.Paragraphs.Add(ref oRngNewPage);
          oParaNew.Range.Font.Name = "Times New Roman";
          oParaNew.Range.Text = "<br><strong>This is test for new page</strong>";
          //oParaChapter.Format.SpaceAfter = 6;
          oParaNew.Range.Font.Bold = 1;
          oParaNew.Range.Font.Size = 7;
          oParaNew.Format.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
          oParaNew.Range.InsertParagraphAfter();

         
       

          //Insert a paragraph at the beginning of the document.
          object oRngFooter;
          Microsoft.Office.Interop.Word.Paragraph oParaFooter;
          oRngFooter = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
          oParaFooter = oDoc.Content.Paragraphs.Add(ref oRngFooter);
          oParaFooter.Range.Text = "</body></html>";
          oParaFooter.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
          oParaFooter.Range.Font.Size = 8;
          oParaFooter.Range.InsertParagraphAfter();

         // oWord.Selection.InsertNewPage();

          oDoc.PageSetup.TwoPagesOnOne = true;

        
          //oDoc.PageSetup.LayoutMode = WdLayoutMode.wdLayoutModeGrid;
        string strFileName;
        strFileName = Guid.NewGuid().ToString() + ".doc";
        object o_filename = Server.MapPath("NewFile/") + strFileName;
        object o_format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
        object o_endings = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
        object o_null = Missing.Value;

        oDoc.PageSetup.TopMargin = oWord.MillimetersToPoints(5F);
        oDoc.PageSetup.BottomMargin = oWord.MillimetersToPoints(10F);
        oDoc.PageSetup.RightMargin = oWord.MillimetersToPoints(5F);
        oDoc.PageSetup.LeftMargin = oWord.MillimetersToPoints(5F);
        oDoc.PageSetup.OddAndEvenPagesHeaderFooter = 0;
        //code by me
        oDoc.PageSetup.HeaderDistance = oWord.MillimetersToPoints(5F);
        oDoc.PageSetup.FooterDistance = oWord.MillimetersToPoints(5F);
        //end

        //New One
       
        //oWord.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = 1;
        oWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
        oWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
       
       


       

        //ENTERING A PARAGRAPH BREAK "ENTER"

        oWord.Selection.TypeParagraph();



        //INSERTING THE PAGE NUMBERS CENTRALLY ALIG7NED IN THE PAGE FOOTER

        oWord.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

        oWord.ActiveWindow.Selection.Font.Name = "Times New Roman";

        oWord.ActiveWindow.Selection.Font.Size = 6;
       

        oWord.ActiveWindow.Selection.TypeText("Even Page Header");
       
       

        //INSERTING TAB CHARACTERS

        object CurrentPage = WdFieldType.wdFieldPage;
       

       
        oWord.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);

        //oWord.ActiveWindow.Selection.TypeText(" of ");

        object TotalPages = WdFieldType.wdFieldNumPages;


        oWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
        oWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;

       
       

        //ENTERING A PARAGRAPH BREAK "ENTER"

       // oWord.Selection.TypeParagraph();
       

        //INSERTING THE PAGE NUMBERS CENTRALLY ALIGNED IN THE PAGE FOOTER

        oWord.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight;

        oWord.ActiveWindow.Selection.Font.Name = "Times New Roman";

        oWord.ActiveWindow.Selection.Font.Size = 6;
       
       // oWord.ActiveWindow.Selection.TypeText("Document #: " + docNumber + " " + revisionNumber);



        //INSERTING TAB CHARACTERS

       //oWord.ActiveWindow.Selection.TypeText("Page ");

         CurrentPage = WdFieldType.wdFieldPage;

         oWord.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);

        //oWord.ActiveWindow.Selection.TypeText(" of ");

         TotalPages = WdFieldType.wdFieldNumPages;

       // oWord.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, ref TotalPages, ref oMissing, ref oMissing);

       
        //SETTING FOCUES BACK TO DOCUMENT

        oWord.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;

        oWord.Selection.Font.Bold = 1;

        oWord.Selection.Font.Color = WdColor.wdColorAqua;

        //oWord.Selection.Font.Italic = 1;

        oWord.Selection.Font.Underline = WdUnderline.wdUnderlineDashHeavy;
        oWord.Selection.ClearFormatting();

        //object gotoPage = WdGoToItem.wdGoToPage;
        //object gotoNext = WdGoToDirection.wdGoToNext;
        //object gotoCount = null;
        //object gotoName = "21";

        //oWord.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);

        ////Insert a blank page 
        //oWord.Selection.InsertNewPage(); 


        oDoc.SpellingChecked = true;

        oDoc.SaveAs(ref o_filename, ref o_format, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_null, ref o_endings, ref o_null);

        //Assign null value to created object
        oDoc = null;
        oWord.Quit(ref o_null, ref o_null, ref o_null);
        Response.Redirect("NewFile/" + strFileName);

Wednesday, March 2, 2011

GetExcelVersion

        public static string GetExcelVersion()
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"Excel.Application\CurVer");
            if (key == null) return null;
            object ver = key.GetValue("");
            if (ver == null) return null;
            return ver.ToString();
        }