using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; using System.IO; using System; namespace KM_Artefact { public partial class Form1 : Form { public Form1() { InitializeComponent(); DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn(); checkBoxColumn.HeaderText = ""; checkBoxColumn.Width = 60; checkBoxColumn.Name = "checkBoxColumn"; dataGridView1.Columns.Insert(0, checkBoxColumn); } private void button1_Click(object sender, EventArgs e) { try { String str = "Server=" + IPfield.Text + ";" + "database=" + DBfield.Text + ";" + "UID=" + IDfield.Text + ";" + "password=" + pwdfield.Text; /*String tbl = "Table=" + textBox6.Text;*/ //System.Console.WriteLine(str); Boolean flag = false; if (DBfield.Text == "") { MessageBox.Show("Enter database name"); flag = true; } else if (Queryfield.Text == "") { MessageBox.Show("Enter SQL Query"); flag = true; } else if (IPfield.Text == "") { MessageBox.Show("Enter SQL IP"); flag = true; } else if (IDfield.Text == "") { MessageBox.Show("Enter Username"); flag = true; } else if (pwdfield.Text == "") { MessageBox.Show("Enter Password"); flag = true; } if (flag == false) { SqlConnection con = new SqlConnection(str); String query = Queryfield.Text; SqlCommand cmd = new SqlCommand(query, con); SqlDataAdapter dataadapter = new SqlDataAdapter(query, con); DataSet ds = new DataSet(); con.Open(); /*dataadapter.Fill(ds, "VMS");*/ dataadapter.Fill(ds, query); //MessageBox.Show("connect with SQL server"); //MessageBox.Show("Connected"); DataTable dt = ds.Tables[query]; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "CSV|*.csv"; saveFileDialog1.Title = "Save a CSV File"; //saveFileDialog1.ShowDialog(); DialogResult dr = saveFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { string strFilePath = saveFileDialog1.FileName; //save file using stream. //StreamWriter sw = new StreamWriter(strFilePath, true); CreateCSVFile(dt, strFilePath); } //CreateCSVFile(dt, ""); //MessageBox.Show("CSV generated"); con.Close(); // DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn(); // checkBoxColumn.HeaderText = "SELECT"; // checkBoxColumn.Width = 60; //checkBoxColumn.Name = "checkBoxColumn"; //dataGridView1.Columns["checkBoxColumn"].ReadOnly = false; //checkBoxColumn.Selected = true; //dataGridView1.Columns.Insert(0, checkBoxColumn); dataGridView1.DataSource = ds; dataGridView1.DataMember = query; } } catch (Exception es) { MessageBox.Show(es.Message); //MessageBox.Show("Unable to Connect \nPlease Enter Correct Credentials"); } } public void CreateCSVFile(DataTable dt, string strFilePath) { #region Export Grid to CSV // Create the CSV file to which grid data will be exported. StreamWriter sw = new StreamWriter(strFilePath, false); // First we will write the headers. //DataTable dt = m_dsProducts.Tables[0]; int iColCount = dt.Columns.Count; for (int i = 0; i < iColCount; i++) { sw.Write(dt.Columns[i]); if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); // Now write all the rows. foreach (DataRow dr in dt.Rows) { for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(dr[i])) { sw.Write(dr[i].ToString()); } if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); } sw.Close(); #endregion } private void button2_Click(object sender, EventArgs e) { try { String str = "Server=" + IPfield.Text + ";" + "database=" + DBfield.Text + ";" + "UID=" + IDfield.Text + ";" + "password=" + pwdfield.Text; /*String tbl = "Table=" + textBox6.Text;*/ //System.Console.WriteLine(str); Boolean flag = false; if (DBfield.Text == "") { MessageBox.Show("Enter database name"); flag = true; } else if (Queryfield.Text == "") { MessageBox.Show("Enter SQL Query"); flag = true; } else if (IPfield.Text == "") { MessageBox.Show("Enter SQL IP"); flag = true; } else if (IDfield.Text == "") { MessageBox.Show("Enter Username"); flag = true; } else if (pwdfield.Text == "") { MessageBox.Show("Enter Password"); flag = true; } if (flag == false) { SqlConnection con = new SqlConnection(str); String query = Queryfield.Text; SqlCommand cmd = new SqlCommand(query, con); SqlDataAdapter dataadapter = new SqlDataAdapter(query, con); DataSet ds = new DataSet(); con.Open(); /*dataadapter.Fill(ds, "VMS");*/ dataadapter.Fill(ds, query); MessageBox.Show("connect with SQL server"); //MessageBox.Show("Connected"); DataTable dt = ds.Tables[query]; con.Close(); /* DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn(); checkBoxColumn.HeaderText = "SELECT"; checkBoxColumn.Width = 60; checkBoxColumn.Name = "checkBoxColumn"; dataGridView1.Columns.Insert(0, checkBoxColumn);*/ // dataGridView1.Columns["checkBoxColumn"].ReadOnly = false; //checkBoxColumn.Selected = true; dataGridView1.DataSource = ds; dataGridView1.DataMember = query; } } catch (Exception es) { MessageBox.Show(es.Message); //MessageBox.Show("Unable to Connect \nPlease Enter Correct Credentials"); } } private void GetCellData(string path) { string data = ""; //string userDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); TextWriter tw = new StreamWriter(path,false); // Count each row in the datagrid for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Cells["checkBoxColumn"].Value != null && (bool)dataGridView1.Rows[i].Cells["checkBoxColumn"].Value) { foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells) for(int j=0; j < dataGridView1.Columns.Count; j++ ) { data = data + (cell.Value + ","); } data = data + "\n"; } else { continue; } } tw.WriteLine(data, "data"); tw.Close(); } private void button3_Click(object sender, EventArgs e) { //for (int i = 0; i < dataGridView1.Rows.Count;i++ ) int i=0; if (dataGridView1.Rows[i].Cells["checkBoxColumn"].Value != null && (bool)dataGridView1.Rows[i].Cells["checkBoxColumn"].Value) { //dataGridView1.Columns[0].Visible = false; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "CSV|*.csv"; saveFileDialog1.Title = "Save a CSV File"; //saveFileDialog1.ShowDialog(); DialogResult dr = saveFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { string Path = saveFileDialog1.FileName; //save file using stream. //StreamWriter sw = new StreamWriter(strFilePath, true); GetCellData(Path); } } else { //continue; MessageBox.Show("Select data"); } } } }
checkBoxColumn
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)