Jump to content

Parametized Query--Filtering a Datagrid with a combobox

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
esco

esco

    Newbie

  • Members
  • Pip
  • 5 posts
Hi everyone

I'm still learning Csharp. And its langauge that I prefer to use to develop programs. I'm building a program that utilizes a datagrid control and a combobox. I use the combobox to filter the datagrid based on the region/city. I got this vb code, and its a bit old. And I want to re-write the code with csharp 2008. The coding style has really changed, no more things like 'generate dataset', and there are new things like 'bindingsource' . I have put the code extract that I'm more concerned with, from the attached file:


Private Sub cboCountry_SelectedIndexChanged( _

 ByVal sender As System.Object, _

 ByVal e As System.EventArgs) _

 Handles cboCountry.SelectedIndexChanged


 ' Get the Parameter object and Set value

 With SqlDataAdapter1.SelectCommand.Parameters

 .Item("@CountryParam").Value = _

 cboCountry.SelectedValue

 End With



 ' Clear the dataset

 DsCustomers1.Clear()

 ' Load the dataset using the parameter value

 SqlDataAdapter1.Fill(DsCustomers1, "Customers")

End Sub


How do I do this? I've tried I get errors like 'missing directive'
Or do I have to re-write the entire project? How? I've put up the project, and its working except the combobox filtering part

Edited by esco, 23 June 2010 - 04:48 AM.
resolved


#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Convert VB.NET to C# - A free code conversion tool - developer Fusion - ASP.NET, C# Programming, VB.NET, .NET Framework, Java and Visual Basic Tutorials
Convert your code, and then post here what you can't solve. Since it is in the C# catgory, don't expect many to understand VB, especially since it is not indented or highlighted. Use [ code] tags [/ code].
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
esco

esco

    Newbie

  • Members
  • Pip
  • 5 posts
Thank you very much

I've been to developer fusion, and I used the tool. It does a great job, but the problem is when I put the code in my project. I get errors like 'missing directive', words like 'SelectCommand' are not being recognized. I tried putting directives like 'Using System.Data.Sqlclient' but this problem is not getting solved ?

#4
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Convert your code to C# with a online Code Converter and Then post if any errors in the same thread.

#5
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

gokuajmes said:

Convert your code to C# with a online Code Converter and Then post if any errors in the same thread.
I talked to James about a new Codecall feature. Users will get ranks depending on how many threads they solved rather then post count if it is implemented. I hope you will like it.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#6
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts

Davide said:

I talked to James about a new Codecall feature. Users will get ranks depending on how many threads they solved rather then post count if it is implemented. I hope you will like it.
Will surely love it, C# section is totally filled with posts from You & Me :D not to mention ASP.NET

#7
esco

esco

    Newbie

  • Members
  • Pip
  • 5 posts
I think I've found an alternative, or the new way of doing this. But I'm failing to do the last part. For testing purposes, I've put up a DataGridView and a ComboBox, using drag and droping. Put up a DataSet, BindingSource, and TableAdadpter.

Then I manually built a collection (the list of items) for the ComboBox. I did it manually to avoid duplicates of values if I was to pull it from a database table column. I also wrote the parameterized query using the xsd Query Builder:
SELECT Company Name, Contact Name, Contact Title, City, Address, Region, Postal Code, Country, Phone, Fax
          FROM Customers
          WHERE City = @CityParam

Then the following part requests a parameter value to fill the DataAdapter successfully, and this is where I'm stuck:
 private void Form1_Load(object sender, EventArgs e)
        {
            this.customersTableAdapter.Fill(this.northwindDS.Customers, [I]string parameter[/I] );
         }

I have another piece of code which might be required, also that may need correction:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            this.customersBindingSource.Filter = "City='comboBox1.SelectedValue'";         
        }

What I really need is to use the value from the ComboBox to filter the DataGridView, or display data based on a value from the ComboBox.

Edited by esco, 22 June 2010 - 02:19 AM.


#8
esco

esco

    Newbie

  • Members
  • Pip
  • 5 posts
First let me thank everyone for participating. I figured this out. I once researched about a "How to do a dynamic search", using a TextBox and a DataGrid. And I came a across some information, the code sample was written in old VB.NET. I worked it out to the current Csharp then. Here is what my result was:
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string search;
            search = textBox1.Text;
            if (search != "")
                customersBindingSource.Filter = "City LIKE'" + search + "%'";
            else
                customersBindingSource.Filter = "City <> '123'";
        }

Now, out of this I worked out the solution for the ComboBox:
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string bds_filter = comboBox1.Text;
            customersBindingSource.Filter = "City ='" + bds_filter + "'";
        }

Wonderful !

Remember the list (collection) for the ComboBox is done manually, NOT binding the ComboBox to the datasource

#9
esco

esco

    Newbie

  • Members
  • Pip
  • 5 posts
Even this works
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            customersBindingSource.Filter = "City ='" + comboBox2.Text + "'";
        }