I am using DataTable
to fill a DataGridView
for a search / filter.
The DataGridView shows the actual Database column names.
But I want to change those column names. eg tbl_Users
has a column labeled LocalNo
that I want to change to Unit No.
Here is my code:
namespace FrequencyBook
{
public partial class Form11 : Form
{
private static Form11 alreadyOpened = null;
public Form11()
{
InitializeComponent();
{
if (alreadyOpened != null && !alreadyOpened.IsDisposed)
{
alreadyOpened.Focus(); // Bring the old one to top
Shown += (s, e) => this.Close(); // and destroy the new one.
return;
}
// Otherwise store this one as reference
alreadyOpened = this;
}
}
private DataTable dt = new DataTable();
private void Form11_Load(object sender, EventArgs e)
{
dataGridView11.DataSource = GetSearchForm();
}
private DataTable GetSearchForm()
{
string connString = ConfigurationManager.ConnectionStrings["FrequencyBook.Properties.Settings.db_FrequenciesConnectionString"].ConnectionString;
using (OleDbConnection conn = new OleDbConnection(connString))
{
using (OleDbCommand cmd = new OleDbCommand("Select * FROM tbl_Users", conn))
{
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
}
}
return dt;
}
private void closeFormToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void resetFormToolStripMenuItem_Click(object sender, EventArgs e)
{
tboxSearchLicensee.Clear();
tboxSearchCallsign.Clear();
tboxSearchLocation.Clear();
tboxSearchBand.Clear();
tboxSearchRID.Clear();
tboxSearchLocalNo.Clear();
}
private void tboxSearchLicensee_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "Licensee LIKE '%" + tboxSearchLicensee.Text + "%'" ;
}
private void tboxSearchCallsign_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "Callsign LIKE '%" + tboxSearchCallsign.Text + "%'" ;
}
private void tboxSearchLocation_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "Location LIKE '%" + tboxSearchLocation.Text + "%'" ;
}
private void tboxSearchBand_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "Band LIKE '%" + tboxSearchBand.Text + "%'" ;
}
private void tboxSearchRID_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("RID1 LIKE '%{0}%' OR RID2 LIKE '%{0}%' OR RID3 LIKE '%{0}%' OR RID4 LIKE '%{0}%'", tboxSearchRID.Text);
//DataView dv = dt.DefaultView;
//dv.RowFilter = "RID1 LIKE '%" + tboxSearchRID.Text + "%'" ;
}
private void tboxSearchLocalNo_TextChanged(object sender, EventArgs e)
{
DataView dv = dt.DefaultView;
dv.RowFilter = "LocalNo LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
}
I've read this post here: How to change the DataTable Column Name? The first answer makes sense to me, but I don't know where I need to add the new lines. I've tried in a few places, but NO JOY.