Reset all Controls (Textbox, ComboBox, CheckBox, ListBox) in a Windows Form using C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Common
{
public class Utilities
{
public static void ResetAllControls(Control form)
{
foreach (Control control in form.Controls)
{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
textBox.Text = null;
}
if (control is ComboBox)
{
ComboBox comboBox = (ComboBox)control;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
}
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
checkBox.Checked = false;
}
if (control is ListBox)
{
ListBox listBox = (ListBox)control;
listBox.ClearSelected();
}
}
}
}
}
Calling method from code behind form:
private void button1_Click(object sender, EventArgs e)
{
Common.Utilities.ResetAllControls(this);
}
Clear all Text Boxes in a Windows Form using C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Common
{
public class Utilities
{
public static void ClearAllTextBoxes(Control form)
{
foreach (Control control in form.Controls)
{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
textBox.Text = null;
}
}
}
}
}
Calling method from code behind form:
private void button1_Click(object sender, EventArgs e)
{
Common.Utilities.ClearAllTextBoxes(this);
}
Change button background on hover in windows forms
public Form1()
{
InitializeComponent();
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.background_1));
}
private void button1_MouseHover(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.background_2));
}
private void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.background_1));
}
Import Excel file into DataGridView using C#
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);
string query = String.Format("select * from [{0}$]", "Sheet1");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
This article will guide you how to import a CSV file into DataGridView
See the contents of a csv file
The first line is column names:
CustomerID,CompanyName,ContactName
The remaining lines are the content, each line corresponds to a record, the columns are separated by commas “,”
Step 1: Creating StreamReader
System.IO.StreamReader streamReader = new StreamReader(txtPath.Text);
Step 2: Reading CSV file header
rowValue = streamReader.ReadLine();
cellValue = rowValue.Split(',');
for (int i = 0; i <= cellValue.Count() - 1; i++)
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = cellValue[i];
column.HeaderText = cellValue[i];
dataGridView1.Columns.Add(column);
}
Step 3: Reading CSV file content
while (streamReader.Peek() != -1)
{
rowValue = streamReader.ReadLine();
cellValue = rowValue.Split(',');
dataGridView1.Rows.Add(cellValue);
}
Complete Source Code
private void btnLoadData_Click(object sender, EventArgs e)
{
string rowValue;
string[] cellValue;
if (System.IO.File.Exists(txtPath.Text))
{
System.IO.StreamReader streamReader = new StreamReader(txtPath.Text);
// Reading header
rowValue = streamReader.ReadLine();
cellValue = rowValue.Split(',');
for (int i = 0; i <= cellValue.Count() - 1; i++)
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = cellValue[i];
column.HeaderText = cellValue[i];
dataGridView1.Columns.Add(column);
}
// Reading content
while (streamReader.Peek() != -1)
{
rowValue = streamReader.ReadLine();
cellValue = rowValue.Split(',');
dataGridView1.Rows.Add(cellValue);
}
streamReader.Close();
}
else
{
MessageBox.Show("No File is Selected");
}
}