How to Change the Header Name / Column Name Of a Datatable Fed to dataGridView
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.
c# datagridview datatable
add a comment |
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.
c# datagridview datatable
You can change the Column Name as mentioned in the link in the GetSearchForm, right before your return "dt". You can also do it in the Form_Load. You need to assign the value returned by GetSearchForm to a variable, and before you set the DataSource, you can change the Column names
– Anu Viswan
Nov 27 '18 at 3:48
You could change thedataGridView1.Columns[N].HeaderText = "[SomethingElse]";
. Using some source to match the pairs:DataColumn[N].ColumnName -> DGV.Column[N].HeaderText
. It looks like you're just filtering the rows.
– Jimi
Nov 27 '18 at 3:50
Thank you Anu. I knew it would work, but I kept putting it in the wrong place and I was not changing DataTable.Columns to dt.Columns. Worked perfect before the return dt; ..... Thank you again
– Jim Neel
Nov 27 '18 at 5:20
Actually @Anu Viswan, I did find a problem. While your solution does address and fix the header names, nothing happens when I fill in the actual form with search terms. It's obviously because of the dv.RowFilter lines below.
– Jim Neel
Nov 27 '18 at 22:54
add a comment |
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.
c# datagridview datatable
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.
c# datagridview datatable
c# datagridview datatable
edited Nov 27 '18 at 7:05
JohnB
1,88511218
1,88511218
asked Nov 27 '18 at 3:32
Jim NeelJim Neel
82
82
You can change the Column Name as mentioned in the link in the GetSearchForm, right before your return "dt". You can also do it in the Form_Load. You need to assign the value returned by GetSearchForm to a variable, and before you set the DataSource, you can change the Column names
– Anu Viswan
Nov 27 '18 at 3:48
You could change thedataGridView1.Columns[N].HeaderText = "[SomethingElse]";
. Using some source to match the pairs:DataColumn[N].ColumnName -> DGV.Column[N].HeaderText
. It looks like you're just filtering the rows.
– Jimi
Nov 27 '18 at 3:50
Thank you Anu. I knew it would work, but I kept putting it in the wrong place and I was not changing DataTable.Columns to dt.Columns. Worked perfect before the return dt; ..... Thank you again
– Jim Neel
Nov 27 '18 at 5:20
Actually @Anu Viswan, I did find a problem. While your solution does address and fix the header names, nothing happens when I fill in the actual form with search terms. It's obviously because of the dv.RowFilter lines below.
– Jim Neel
Nov 27 '18 at 22:54
add a comment |
You can change the Column Name as mentioned in the link in the GetSearchForm, right before your return "dt". You can also do it in the Form_Load. You need to assign the value returned by GetSearchForm to a variable, and before you set the DataSource, you can change the Column names
– Anu Viswan
Nov 27 '18 at 3:48
You could change thedataGridView1.Columns[N].HeaderText = "[SomethingElse]";
. Using some source to match the pairs:DataColumn[N].ColumnName -> DGV.Column[N].HeaderText
. It looks like you're just filtering the rows.
– Jimi
Nov 27 '18 at 3:50
Thank you Anu. I knew it would work, but I kept putting it in the wrong place and I was not changing DataTable.Columns to dt.Columns. Worked perfect before the return dt; ..... Thank you again
– Jim Neel
Nov 27 '18 at 5:20
Actually @Anu Viswan, I did find a problem. While your solution does address and fix the header names, nothing happens when I fill in the actual form with search terms. It's obviously because of the dv.RowFilter lines below.
– Jim Neel
Nov 27 '18 at 22:54
You can change the Column Name as mentioned in the link in the GetSearchForm, right before your return "dt". You can also do it in the Form_Load. You need to assign the value returned by GetSearchForm to a variable, and before you set the DataSource, you can change the Column names
– Anu Viswan
Nov 27 '18 at 3:48
You can change the Column Name as mentioned in the link in the GetSearchForm, right before your return "dt". You can also do it in the Form_Load. You need to assign the value returned by GetSearchForm to a variable, and before you set the DataSource, you can change the Column names
– Anu Viswan
Nov 27 '18 at 3:48
You could change the
dataGridView1.Columns[N].HeaderText = "[SomethingElse]";
. Using some source to match the pairs: DataColumn[N].ColumnName -> DGV.Column[N].HeaderText
. It looks like you're just filtering the rows.– Jimi
Nov 27 '18 at 3:50
You could change the
dataGridView1.Columns[N].HeaderText = "[SomethingElse]";
. Using some source to match the pairs: DataColumn[N].ColumnName -> DGV.Column[N].HeaderText
. It looks like you're just filtering the rows.– Jimi
Nov 27 '18 at 3:50
Thank you Anu. I knew it would work, but I kept putting it in the wrong place and I was not changing DataTable.Columns to dt.Columns. Worked perfect before the return dt; ..... Thank you again
– Jim Neel
Nov 27 '18 at 5:20
Thank you Anu. I knew it would work, but I kept putting it in the wrong place and I was not changing DataTable.Columns to dt.Columns. Worked perfect before the return dt; ..... Thank you again
– Jim Neel
Nov 27 '18 at 5:20
Actually @Anu Viswan, I did find a problem. While your solution does address and fix the header names, nothing happens when I fill in the actual form with search terms. It's obviously because of the dv.RowFilter lines below.
– Jim Neel
Nov 27 '18 at 22:54
Actually @Anu Viswan, I did find a problem. While your solution does address and fix the header names, nothing happens when I fill in the actual form with search terms. It's obviously because of the dv.RowFilter lines below.
– Jim Neel
Nov 27 '18 at 22:54
add a comment |
1 Answer
1
active
oldest
votes
Here is my new code. It works for the header / column name change, but it breaks the actual search / filter function. I actually changed the new lines back to the original name and got the Form to work again. Any suggestions? Here is a screenshot of my Search / Filter Form:
My search form
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);
}
}
dt.Columns["ID"].ColumnName = "ID";
dt.Columns["Licensee"].ColumnName = "Licensee";
dt.Columns["Callsign"].ColumnName = "Callsign";
dt.Columns["LocalNo"].ColumnName = "Unit";
dt.Columns["Location"].ColumnName = "Location";
dt.Columns["FreqBand"].ColumnName = "Band";
dt.Columns["Email"].ColumnName = "Email";
dt.Columns["Phone"].ColumnName = "Phone";
dt.Columns["RID1"].ColumnName = "RID1";
dt.Columns["RID2"].ColumnName = "RID2";
dt.Columns["RID3"].ColumnName = "RID3";
dt.Columns["RID4"].ColumnName = "RID4";
dt.Columns["Notes"].ColumnName = "Notes";
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 = "Unit LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
}
private void button1_Click(object sender, EventArgs e)
{
//This button has changed to btnReturnToUsers
this.Hide();//Hide the 'current' form, form11
//show another form ( form12 )
Form12 frm = new Form12();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
private void btnReturnToMain_Click(object sender, EventArgs e)
{
this.Hide();//Hide the 'current' form, form11
//show another form ( form2 )
Form2 frm = new Form2();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492333%2fhow-to-change-the-header-name-column-name-of-a-datatable-fed-to-datagridview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is my new code. It works for the header / column name change, but it breaks the actual search / filter function. I actually changed the new lines back to the original name and got the Form to work again. Any suggestions? Here is a screenshot of my Search / Filter Form:
My search form
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);
}
}
dt.Columns["ID"].ColumnName = "ID";
dt.Columns["Licensee"].ColumnName = "Licensee";
dt.Columns["Callsign"].ColumnName = "Callsign";
dt.Columns["LocalNo"].ColumnName = "Unit";
dt.Columns["Location"].ColumnName = "Location";
dt.Columns["FreqBand"].ColumnName = "Band";
dt.Columns["Email"].ColumnName = "Email";
dt.Columns["Phone"].ColumnName = "Phone";
dt.Columns["RID1"].ColumnName = "RID1";
dt.Columns["RID2"].ColumnName = "RID2";
dt.Columns["RID3"].ColumnName = "RID3";
dt.Columns["RID4"].ColumnName = "RID4";
dt.Columns["Notes"].ColumnName = "Notes";
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 = "Unit LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
}
private void button1_Click(object sender, EventArgs e)
{
//This button has changed to btnReturnToUsers
this.Hide();//Hide the 'current' form, form11
//show another form ( form12 )
Form12 frm = new Form12();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
private void btnReturnToMain_Click(object sender, EventArgs e)
{
this.Hide();//Hide the 'current' form, form11
//show another form ( form2 )
Form2 frm = new Form2();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
}
}
add a comment |
Here is my new code. It works for the header / column name change, but it breaks the actual search / filter function. I actually changed the new lines back to the original name and got the Form to work again. Any suggestions? Here is a screenshot of my Search / Filter Form:
My search form
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);
}
}
dt.Columns["ID"].ColumnName = "ID";
dt.Columns["Licensee"].ColumnName = "Licensee";
dt.Columns["Callsign"].ColumnName = "Callsign";
dt.Columns["LocalNo"].ColumnName = "Unit";
dt.Columns["Location"].ColumnName = "Location";
dt.Columns["FreqBand"].ColumnName = "Band";
dt.Columns["Email"].ColumnName = "Email";
dt.Columns["Phone"].ColumnName = "Phone";
dt.Columns["RID1"].ColumnName = "RID1";
dt.Columns["RID2"].ColumnName = "RID2";
dt.Columns["RID3"].ColumnName = "RID3";
dt.Columns["RID4"].ColumnName = "RID4";
dt.Columns["Notes"].ColumnName = "Notes";
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 = "Unit LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
}
private void button1_Click(object sender, EventArgs e)
{
//This button has changed to btnReturnToUsers
this.Hide();//Hide the 'current' form, form11
//show another form ( form12 )
Form12 frm = new Form12();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
private void btnReturnToMain_Click(object sender, EventArgs e)
{
this.Hide();//Hide the 'current' form, form11
//show another form ( form2 )
Form2 frm = new Form2();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
}
}
add a comment |
Here is my new code. It works for the header / column name change, but it breaks the actual search / filter function. I actually changed the new lines back to the original name and got the Form to work again. Any suggestions? Here is a screenshot of my Search / Filter Form:
My search form
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);
}
}
dt.Columns["ID"].ColumnName = "ID";
dt.Columns["Licensee"].ColumnName = "Licensee";
dt.Columns["Callsign"].ColumnName = "Callsign";
dt.Columns["LocalNo"].ColumnName = "Unit";
dt.Columns["Location"].ColumnName = "Location";
dt.Columns["FreqBand"].ColumnName = "Band";
dt.Columns["Email"].ColumnName = "Email";
dt.Columns["Phone"].ColumnName = "Phone";
dt.Columns["RID1"].ColumnName = "RID1";
dt.Columns["RID2"].ColumnName = "RID2";
dt.Columns["RID3"].ColumnName = "RID3";
dt.Columns["RID4"].ColumnName = "RID4";
dt.Columns["Notes"].ColumnName = "Notes";
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 = "Unit LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
}
private void button1_Click(object sender, EventArgs e)
{
//This button has changed to btnReturnToUsers
this.Hide();//Hide the 'current' form, form11
//show another form ( form12 )
Form12 frm = new Form12();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
private void btnReturnToMain_Click(object sender, EventArgs e)
{
this.Hide();//Hide the 'current' form, form11
//show another form ( form2 )
Form2 frm = new Form2();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
}
}
Here is my new code. It works for the header / column name change, but it breaks the actual search / filter function. I actually changed the new lines back to the original name and got the Form to work again. Any suggestions? Here is a screenshot of my Search / Filter Form:
My search form
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);
}
}
dt.Columns["ID"].ColumnName = "ID";
dt.Columns["Licensee"].ColumnName = "Licensee";
dt.Columns["Callsign"].ColumnName = "Callsign";
dt.Columns["LocalNo"].ColumnName = "Unit";
dt.Columns["Location"].ColumnName = "Location";
dt.Columns["FreqBand"].ColumnName = "Band";
dt.Columns["Email"].ColumnName = "Email";
dt.Columns["Phone"].ColumnName = "Phone";
dt.Columns["RID1"].ColumnName = "RID1";
dt.Columns["RID2"].ColumnName = "RID2";
dt.Columns["RID3"].ColumnName = "RID3";
dt.Columns["RID4"].ColumnName = "RID4";
dt.Columns["Notes"].ColumnName = "Notes";
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 = "Unit LIKE '%" + tboxSearchLocalNo.Text + "%'" ;
}
private void button1_Click(object sender, EventArgs e)
{
//This button has changed to btnReturnToUsers
this.Hide();//Hide the 'current' form, form11
//show another form ( form12 )
Form12 frm = new Form12();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
private void btnReturnToMain_Click(object sender, EventArgs e)
{
this.Hide();//Hide the 'current' form, form11
//show another form ( form2 )
Form2 frm = new Form2();
frm.ShowDialog();
//Close the form.(form11)
this.Close();
}
}
}
answered Nov 27 '18 at 23:06
Jim NeelJim Neel
82
82
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492333%2fhow-to-change-the-header-name-column-name-of-a-datatable-fed-to-datagridview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can change the Column Name as mentioned in the link in the GetSearchForm, right before your return "dt". You can also do it in the Form_Load. You need to assign the value returned by GetSearchForm to a variable, and before you set the DataSource, you can change the Column names
– Anu Viswan
Nov 27 '18 at 3:48
You could change the
dataGridView1.Columns[N].HeaderText = "[SomethingElse]";
. Using some source to match the pairs:DataColumn[N].ColumnName -> DGV.Column[N].HeaderText
. It looks like you're just filtering the rows.– Jimi
Nov 27 '18 at 3:50
Thank you Anu. I knew it would work, but I kept putting it in the wrong place and I was not changing DataTable.Columns to dt.Columns. Worked perfect before the return dt; ..... Thank you again
– Jim Neel
Nov 27 '18 at 5:20
Actually @Anu Viswan, I did find a problem. While your solution does address and fix the header names, nothing happens when I fill in the actual form with search terms. It's obviously because of the dv.RowFilter lines below.
– Jim Neel
Nov 27 '18 at 22:54