Monday 26 November 2012

how to bind data to listview in c#.net

             InitializeComponent();

            // Set the view to show details.
            lvwResult.View = View.Details;

            // Allow the user to edit item text.
            lvwResult.LabelEdit = true;

            // Allow the user to rearrange columns.
            lvwResult.AllowColumnReorder = true;

            // Select the item and subitems when selection is made.
            lvwResult.FullRowSelect = true;

            // Display grid lines.
            lvwResult.GridLines = true;

            // Sort the items in the list in ascending order.
            lvwResult.Sorting = SortOrder.Ascending;


private void Form_Load()
        {
            lvwResult.Columns.Clear();
            lvwResult.Items.Clear();
            lvwResult.View = View.Details;
            lvwResult.LabelEdit = true;
            lvwResult.Columns.Add("Company ID", 100, HorizontalAlignment.Center);
            lvwResult.Columns.Add("Company Name", 150, HorizontalAlignment.Center);
            lvwResult.Columns.Add("Company Website", 150, HorizontalAlignment.Center);
            lvwResult.Columns.Add("Company Email", 150, HorizontalAlignment.Center);
            con.Open();
            lvwResult.Items.Clear();
            lvwResult.Refresh();
            string OleDb = "select * from Company";
            OleDbCommand cmd = new OleDbCommand(OleDb, con);
            OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ListViewItem lvi = new ListViewItem(dr["companyID"].ToString());
                lvi.SubItems.Add(dr["companyName"].ToString());
                lvi.SubItems.Add(dr["companyWebsite"].ToString());
                lvi.SubItems.Add(dr["companyEmail"].ToString());
                lvwResult.Items.Add(lvi);
                lvwResult.FullRowSelect = true;

            }
            con.Close();
        }



private void lvwResult_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwResult.SelectedItems.Count > 0)
            {
                textBox1.Text = lvwResult.SelectedItems[0].Text;
            }

            ViewCompanyDetails objViewCompanyDetails = new ViewCompanyDetails(textBox1.Text);
            objViewCompanyDetails.MdiParent = this.MdiParent;
            objViewCompanyDetails.Dock = DockStyle.Fill;
            objViewCompanyDetails.Show();
            objViewCompanyDetails.Focus();
            this.Hide();
        }

connection state giving problem

if (dbConnection.State != ConnectionState.Open)
        dbConnection.Open();

how to bind combobox with datatable in c#

con.Open();
            cmd = new OleDbCommand("Select DISTINCT departmentName from Department", con);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            comboBoxDepartment.DisplayMember = "departmentName";
            comboBoxDepartment.ValueMember = "departmentName";
            comboBoxDepartment.DataSource = dt;
            con.Close();

export dataset to excel in c# windows application

Export Data in excel sheet from DataGridView C# Windows Application

Microsoft.Office.Interop.Excel.Application ExcelApp =
            new Microsoft.Office.Interop.Excel.Application();
            ExcelApp.Application.Workbooks.Add(Type.Missing);

            // Change properties of the Workbook

            ExcelApp.Columns.ColumnWidth = 20;

            // Storing header part in Excel
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }

            // Storing Each row and column value to excel sheet
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }
            MessageBox.Show("Excel file created , you can find the file D:\\Employee Data.xlsx");
            ExcelApp.Visible = true;
            ExcelApp.ActiveWorkbook.SaveCopyAs("D:\\Employee Data.xlsx");
            ExcelApp.ActiveWorkbook.Saved = true;
           
            //ExcelApp.Quit();