Okay so I have a data gridview which will not only just display the data but will also allow me to filter the data.
You can get an example of how the filter will look like on the gridview by looking at the following picture.
Gridview Picture
you can also read about it through here
Building a Drop-Down Filter List for a DataGridView Column Header Cell[
^]
Now with that explained,
the issue i am having is that it works perfectly only if I bind data through visual studio by going to "Server Explorer" and basically auto binding the data without having to write a query in the back.
What I want to do is to find out a way to add the details ( the query etc) by myself.
The reason I want to do this is because my whole app is kinda done in a way where database is connected to gridviews etc programatically.
For example here is a normal datagridview file that I use in my project
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Call_Logger
{
public partial class Dashboard : Form
{
private OleDbConnection con = new OleDbConnection();
public Dashboard()
{
InitializeComponent();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
}
private void btn_LoadTable_Click(object sender, EventArgs e)
{
try
{
con.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = con;
command.CommandText = ("select * from LogCall");
command.ExecuteNonQuery();
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
private void btn_AddRecords_Click(object sender, EventArgs e)
{
this.Hide();
AddingFunctionality Add = new AddingFunctionality();
Add.ShowDialog();
}
private void btn_Edit_Click(object sender, EventArgs e)
{
this.Hide();
Edit Add = new Edit();
Add.ShowDialog();
}
private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
AddCallStatus Add = new AddCallStatus();
Add.ShowDialog();
}
private void editExistingValueToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
EditCallType Add = new EditCallType();
Add.ShowDialog();
}
private void addNewToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Hide();
LocationAdd Add = new LocationAdd();
Add.ShowDialog();
}
private void editExistingToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
EditLocation Add = new EditLocation();
Add.ShowDialog();
}
private void addNewToolStripMenuItem2_Click(object sender, EventArgs e)
{
this.Hide();
AddCallStatus Add = new AddCallStatus();
Add.ShowDialog();
}
private void editExistingValueToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Hide();
EditCallSubjects Add = new EditCallSubjects();
Add.ShowDialog();
}
}
}
And, here is the code for the datagridview with the feature of Auto-Filtering
using DataGridViewAutoFilter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DesignerSetupDemo
{
public partial class DesignerSetupForm : Form
{
public DesignerSetupForm()
{
InitializeComponent();
}
private void DesignerSetupForm_Load(object sender, EventArgs e)
{
this.newDataSet.ReadXml(@"..\..\..\..\..\TestData.xml");
this.dataGridView1.AutoResizeColumns();
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up))
{
DataGridViewAutoFilterColumnHeaderCell filterCell =
dataGridView1.CurrentCell.OwningColumn.HeaderCell as
DataGridViewAutoFilterColumnHeaderCell;
if (filterCell != null)
{
filterCell.ShowDropDownList();
e.Handled = true;
}
}
}
private void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
String filterStatus = DataGridViewAutoFilterColumnHeaderCell
.GetFilterStatus(dataGridView1);
if (String.IsNullOrEmpty(filterStatus))
{
showAllLabel.Visible = false;
filterStatusLabel.Visible = false;
}
else
{
showAllLabel.Visible = true;
filterStatusLabel.Visible = true;
filterStatusLabel.Text = filterStatus;
}
}
private void showAllLabel_Click(object sender, EventArgs e)
{
DataGridViewAutoFilterColumnHeaderCell.RemoveFilter(dataGridView1);
}
}
}
I need to convert the code of the 2nd example to the code of the 1st example
What I have tried:
Actually the thing is that the results shown in the database does not include the newly added values.