Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Parametized Query--Filtering a Datagrid with a combobox

combobox grid

  • Please log in to reply
8 replies to this topic

#1 esco

esco

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 14 June 2010 - 06:30 AM

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

Attached Files


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

  • 0

#2 Davide

Davide

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 484 posts

Posted 14 June 2010 - 07:13 AM

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].
  • 0
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3 esco

esco

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 14 June 2010 - 07:34 AM

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 ?
  • 0

#4 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 16 June 2010 - 04:00 AM

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

#5 Davide

Davide

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 484 posts

Posted 16 June 2010 - 04:03 AM

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.
  • 0
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#6 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 16 June 2010 - 04:07 AM

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
  • 0

#7 esco

esco

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 22 June 2010 - 01:43 AM

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.

  • 0

#8 esco

esco

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 23 June 2010 - 05:06 AM

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
  • 0

#9 esco

esco

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 23 June 2010 - 05:28 AM

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

  • 0





Also tagged with one or more of these keywords: combobox, grid

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download