Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

using Microsoft.Office.Interop; using Microsoft.Office.Interop.Excel; using System.Configuration; using System.Data.OleDb; using System.IO; using System.Data.SqlClient; using Microsoft.

CSharp; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void tb1_LostFocus(object sender, EventArgs e) { int n; if (!int.TryParse(tb1.Text, out n)) { tb1.Focus(); } } private void Form1_Load(object sender, EventArgs e) { dis(); } public void dis() { OleDbConnection cn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=|DataDirectory|\\Student_Data.mdb"); cn.Open(); OleDbDataAdapter adp = new OleDbDataAdapter("select * from data", cn); DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; cn.Close(); } public void a()

{ MessageBox.Show("Plese Select any Check BOX"); } public void dis2() { OleDbConnection cn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=|DataDirectory|\\Student_Data.mdb"); cn.Open(); if (cb1.Checked || cb2.Checked || cb3.Checked) { if (cb1.Checked == true && cb2.Checked == true && cb3.Checked == true) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from data where bcl<=" + "'" + tb2.Text + "'" + " and ssc>=" + "'" + tb1.Text + "'" + " and " + " inter>= " + "'" + tb1.Text + "'" + " and " + " btech>= " + "'" + tb1.Text + "'", cn); DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; } else if (cb1.Checked == true && cb2.Checked == true) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from data where bcl<=" + "'" + tb2.Text + "'" + " and ssc>=" + "'" + tb1.Text + "'" + " and " + " inter>= " + "'" + tb1.Text + "'", cn); DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; } else if (cb1.Checked == true && cb3.Checked == true) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from data where bcl<=" + "'" + tb2.Text + "'" + " and ssc>=" + "'" + tb1.Text + "'" + " and " + " btech>= " + "'" + tb1.Text + "'", cn); DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; } else if (cb2.Checked == true && cb3.Checked == true) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from data where bcl<=" + "'" + tb2.Text + "'" + " and inter>= " + "'" + tb1.Text + "'" + " and " + " btech>= " + "'" + tb1.Text + "'", cn);

DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; } else if (cb1.Checked == true) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from data where bcl<=" + "'" + tb2.Text + "'" + " and ssc>=" + "'" + tb1.Text + "'", cn); DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; } else if (cb2.Checked == true) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from data where bcl<=" + "'" + tb2.Text + "'" + " and inter>=" + "'" + tb1.Text + "'", cn); DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; } else if (cb3.Checked == true) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from data where bcl<=" + "'" + tb2.Text + "'"+" and btech>=" + "'" + tb1.Text + "'", cn); DataSet ds1 = new DataSet(); adp.Fill(ds1, "data"); dgv1.DataSource = ds1.Tables[0]; }

} else { a(); } } public void dis3() {

String S = "selected values are : " + tb1.Text + "% in "; String S1 = "Number of Back Locks are " + tb2.Text; if (cb1.Checked == true) S = S + cb1.Text + " "; if (cb2.Checked == true) S = S + "and " + cb2.Text + " "; if (cb3.Checked == true) S = S + "and " + cb3.Text + " "; lb2.Text = S; if (tb2.Text == "true") S1 = "and " + S1 + tb2.Text; lb5.Text = S1; // ankal(); } private void button1_Click(object sender, EventArgs e) { dis2(); dis3(); } private void button2_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel Documents (*.xls)|*.xls"; sfd.FileName = "export.xls"; if (sfd.ShowDialog() == DialogResult.OK) { ToCsV(dgv1, sfd.FileName); } } private void ToCsV(DataGridView data, string file) { string stOutput = ""; string sHeaders = ""; for (int j = 0; j < data.Columns.Count; j++) sHeaders = sHeaders.ToString() + Convert.ToString(data.Columns[j].HeaderText) + "\t"; stOutput += sHeaders + "\r\n"; for (int i = 0; i < data.RowCount - 1; i++) { string stLine = "";

for (int j = 0; j < data.Rows[i].Cells.Count; j++) stLine = stLine.ToString() + Convert.ToString(data.Rows[i].Cells[j].Value) + "\t"; stOutput += stLine + "\r\n"; } Encoding utf16 = Encoding.GetEncoding(1254); byte[] output = utf16.GetBytes(stOutput); FileStream fs = new FileStream(file, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(output, 0, output.Length); bw.Flush(); bw.Close(); fs.Close(); } } }

You might also like