Friday, 2 October 2015

Export Datagridview to Excel in VS 2012

DataSet ds=new DataSet();
ds=LoadData(qry);

            // creating Excel Application
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();


            // creating new WorkBook within Excel application
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);


            // creating new Excelsheet in workbook
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

            // see the excel sheet behind the program
            app.Visible = true;

            // get the reference of first sheet. By default its name is Sheet1.
            // store its reference to worksheet
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;

            // changing the name of active sheet
            worksheet.Name = "Students List";


            // storing header part in Excel
            for (int i = 1; i < ds.Tables[0].Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = ds.Tables[0].Columns[i - 1].ColumnName.ToString();
            }



            // storing Each row and column value to excel sheet
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
                }
            }


            // save the application
            workbook.SaveAs("d:\\output.xls");


Befor that add Microsoft.Office.Interop.Excel dll in reference


if (dataGridView1.Rows.Count > 0)
            {
                Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                XcelApp.Application.Workbooks.Add(Type.Missing);

                for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                {
                    XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    }
                }

                XcelApp.Columns.AutoFit();
                XcelApp.Visible = true;

            }
            else
            {
                MessageBox.Show("No Data to Export");
            }
          

VS 2008 Exporting Datagrid to Excel

Friday, 21 August 2015

Change Control Focus on Enter Key Press in C#
(write the code in form keydown event,{enable keypreview=true;})
 if (e.KeyCode == Keys.Enter) { SendKeys.Send("{TAB}");}

Thursday, 20 August 2015

  1. PRIME NUMBER - Defnition
  2. prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

Tuesday, 18 August 2015

Source Code to Display Prime Numbers Between two Intervals

/* C program to display all prime numbers between Two interval entered by user. */
#include <stdio.h>
int main()
{
  int n1, n2, i, j, flag;
  printf("Enter two numbers(intevals): ");
  scanf("%d %d", &n1, &n2);
  printf("Prime numbers between %d and %d are: ", n1, n2);
  for(i=n1+1; i<n2; ++i)
  {
      flag=0;
      for(j=2; j<=i/2; ++j)
      {
        if(i%j==0)
        {
          flag=1;
          break;
        }
      }
      if(flag==0)
        printf("%d ",i);
  }
  return 0;
}

C Program to Add Two Matrix



#include <stdio.h>
 
int main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
 
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");
 
   for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
         scanf("%d", &first[c][d]);
 
   printf("Enter the elements of second matrix\n");
 
   for (c = 0; c < m; c++)
      for (d = 0 ; d < n; d++)
            scanf("%d", &second[c][d]);
 
   printf("Sum of entered matrices:-\n");
 
   for (c = 0; c < m; c++) {
      for (d = 0 ; d < n; d++) {
         sum[c][d] = first[c][d] + second[c][d];
         printf("%d\t", sum[c][d]);
      }
      printf("\n");
   }
 
   return 0;
}