last day (15 days later) » 

10:19 PM
1
A: Using Listbox to filter values in DataGridView()

Blake ThingstadBelow is a sample of how you could filter the DataGridView according to the ListBox selection. The most important method to look at is listBox1_SelectedIndexChanged. It shows where a DataTable containing the source data is filtered. The DataGridView's DataSource then gets set to this filtered Dat...

 
thank you for your answer :) Hmm I could get rid off it but I still need it to control the data in my DataGridView, the purpose of this is to use the ListBox values to filter other information in my DataGridView each company has employees so if I select Company1 then I want the Company1 employees listed in my DataGridView if you understand how I mean
 
@SterlinkArcher was my answer able to help with your question, is there anything else you need help with or cleared up?
 
I understand your answer, but I want to know how to parameterize the selectedValue from the listbox and use it in my conditional logic at my DataGridView. You can see my updated code and question
 
@SterlinkArcher I've updated my answer to show filtering of the DataGridView. Let me know if you have any problems applying what I've demonstrated to your own code.
 
thank you for your answer, I understand your concept. But 0 of my own code is incorporated in it and there's no consideration of the use of SQL, Connection string that is being used to fill the listbox and the datagridView. Which makes it hard for me to implement the code.
 
10:19 PM
I was avoiding using your code because I couldn't use your connection string and such, but I'll have type it all out for you since you're having a hard time implementing it. Can't guarantee it is bug free since I can't test it completely.
 
Ok I understand, that is true. My connectionstring is based on azure but all IP's have access to it
 
I'll start on this in a moment, shouldn't take too long
 
really appreciate it, usually never work with winforms or web just mobile :/
 
that's cool, I've become accustomed to winforms, currently trying out a book on swift though, swift feels so different from other languages I've used
 
yeah mate, it's totally different... took me about 6 months to be able to do simple -> semi advanced apps.. Using Xamarin mostly though so good for crossdev
 
10:28 PM
what events are companyList_Load and dataEmployees_Load attached to?
I'm just wondering at what point they are called
 
They are called once the form is loaded
public partial class CompanyForm : Form
{
    public CompanyForm()
    {
        InitializeComponent();
        Load += new EventHandler(dataEmployees_Load);
        Load += new EventHandler(companyList_Load);
    }
 
I think your companyList_Load is fine. I did clean it up a bit though.
private void companyList_Load(object sender, EventArgs e) {
using (SqlConnection con = new SqlConnection(cs)) {
try {
con.Open();
const string sql = "Select companyName from dbo.Company";
using (SqlCommand cmd = new SqlCommand(sql, con))
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
using (DataTable dt = new DataTable()) {
adapter.Fill(dt);
companyList.DataSource = dt;
companyList.ValueMember = "companyName";
}
} catch (Exception ex) {
MessageBox.Show("There are no companies to display");
oh wow that formatting looks horrible
 
Yeah my companyList_Load works good :) Thanks
The biggest problem for me has been using the companyList and the listbox in it to filter my DataGridView
 
10:44 PM
How would you like the DataGridView's data to be initialized? Should it contain all data for all companies before a company is selected, or should the first company be selected on load and thus that is how the data would be selected by?
 
Just load all at first, then when you select a company it filters it
 
10:58 PM
dataEmployees is a DataGridView right?
 
yes
It loads data from my database
Just like ListBox they load data from two tables but have common columns. Listbox should only work as a option see all companies in listbox --> select one --> filter applied and shows the employees in the datagridview
 
"Select fname, ename FROM dbo.Users u inner join dbo.Company c on c.companyName = u.company;"
shouldn't this query also SELECT companyName?
 
it can yes
 
you can make that column not visible if that's the desired result
 
no we can add it to clearify things :)
 
11:15 PM
I've edited my answer with my code combined with yours
hopefully it's easy to follow and change yours to this, but if not we'll walk through it
 
Awesome, I'll test it to see how it goes :) really appreciate all the help
 
you don't need the initListBox and initDgv in your code, those were just for setting it up in my code
no problem, lots of fun to help[
also, I'm expecting that there are issues since I couldn't really test it. Would be great if there wasn't, but we'll see
 
ah ok, so I can remove those?
that's why I have ambigity
 
Well it does load both tables with data. But it does not filter :/
I think I have to create a button or something that triggers an update/filter event
 
11:30 PM
did you add companyList.SelectedIndexChanged += listBox1_SelectedIndexChanged;
or whatever you renamed it to
you shouldn't need a button
I know you don't because I have it working without a button
 
aah damn so close, where do I add that code?
 
in the constructor
 
found it in the constructor
 
so it wasn't filtering with that line of code in the constructor?
 
no I forgot to add it in the constructor
 
11:35 PM
oh cool
 
you mean public partial class MainForm : Form {
private ListBox companyList = new ListBox();

private DataTable dataEmployeesSource = new DataTable();
private DataGridView dataEmployees = new DataGridView();
in that piece of code
 
in
public MainForm() {
InitializeComponent();

initListBox();
initDgv();

this.Load += companyList_Load;
this.Load += dataEmployees_Load;

companyList.SelectedIndexChanged += listBox1_SelectedIndexChanged;
}
 
yeah
changed it :)

getting this exception though

"An exception of type 'System.Data.EvaluateException' occurred in System.Data.dll but was not handled in user code"
 
can you copy and paste the line of code that it is occurring on?
 
    private void listbox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataRowView selectedRow = (DataRowView)companyList.SelectedItem;
        string selectedText = (string)selectedRow.Row["companyName"];
        DataView dv = dataEmployeesSource.DefaultView;
        string filter = string.Format("companyName = '{0}'", selectedText);
        dv.RowFilter = filter;
        dataEmployees.DataSource = dv;
it cannot find the companyName column
 
11:43 PM
if you put a breakpoint somewhere in that method, can you check what the column names are for dataEmployeesSource?
 
column names for that are fname, ename, company
it gets null
here it gets an inparameter

DataView dv = dataEmployeesSource.DefaultView;
 
can you check if this query works?
Select fname, ename, c.companyName AS companyName FROM dbo.Users u inner join dbo.Company c on c.companyName = u.company;
 
it does
returns users + their company
Dragan Draganovic Dragans Pizza AB
Andrej Curcic Andrejs IT
Paulo Guimaraes Roots BJJ
Pilo Stylin Yamasaki BJJ
Victor Backman Storebro
Karl Karlsson Nilson Group AB
Marcus Almeida Buchecha BJJ
Jim Donelly Roots BJJ
Graham Berkman Roots BJJ
Thiago Giant Roots BJJ
Peter Purple Roots BJJ
Tony Hberg Yamasaki BJJ
Andrej Curcic Roots BJJ
Dhulasi Siv Roots BJJ
Steven Stevensson Roots BJJ
Fred Fredson Roots BJJ
aklsd akjlr Roots BJJ
Nijaz Hareni Yamasaki BJJ
Bengt Svenssons Svenssons Cyklar AB
Leif Leffes Leffes Pizzeria
just with dummy data
 
cool, next thing...
 
it breaks on this row dv.RowFilter = filter;
 
11:52 PM
private void dataEmployees_Load(object sender, EventArgs e) {
const string sql = "Select fname, ename, c.companyName AS companyName FROM dbo.Users u inner join dbo.Company c on c.companyName = u.company;";
dataEmployeesSource = selectIntoDataTable(sql);

string column1Name = dataEmployeesSource.Columns[0].ColumnName;
string column2Name = dataEmployeesSource.Columns[1].ColumnName;
string column3Name = dataEmployeesSource.Columns[2].ColumnName;
MessageBox.Show(column1Name + " " + column2Name + " " + column3Name, "", MessageBoxButtons.OK);
add in those 4 new lines in that method and copy and paste the message that pops up when you run it
 
it just skips it even if I add breakpoints
I use this code at the end of my first form, where the user fills out his details

CompanyForm frm = new CompanyForm();
frm.Show();
I'll comment out that part and see
column1Name "fname" string
column2Name "ename" string
column3Name "company" string
 
really weird that its changing it to "company"
I'm not honestly sure why it's doing that
 

  last day (15 days later) »