Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Search Data From Datagridview With Usage Of Conditional Operator

gridview grid datagridview

  • Please log in to reply
4 replies to this topic

#1 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 12 July 2012 - 07:56 PM

  private void btn_search_Click(object sender, EventArgs e)
	  {
		 OleDbConnection conn = new OleDbConnection (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\FYP_ Prgms\DatabaseConnection2\DatabaseConnection2\Database 4.accdb");
		 DataTable dt = new DataTable();
		 OleDbDataAdapter SDA = new OleDbDataAdapter("SELECT * FROM [Memo] where Hours < " + int.Parse(txt_hours.Text) , conn);
		 SDA.Fill(dt);
		 dataGridView1.DataSource = dt;
	  }

1)Now I'm able to get data from column name "Hours" which are values to be displayed in the datagridview less than what i've entered in the "txt_hours.Text" when i click the search button.

2)likewise, i ve a column named "Age" i want to get values greater to be displayed in the datagridview than what ive entered in the "txt_age.Text" when i click the the same search button.

Therefore, how to add conditional operators to display by datagridview according to the both conditions in 1) & 2) ?
how to edit the above coding?
Thanks!
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 12 July 2012 - 08:35 PM

You can combine two or more conditions in the WHERE clause of an SQL statement with AND, OR and braces. In your case I think you need to use AND to combine your conditions.
"SELECT * FROM [Memo] where Hours < " + int.Parse(txt_hours.Text) + " AND Age > " + int.Parse(txt_age.Text)

  • 1

#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 12 July 2012 - 08:50 PM

Maybe what you are looking for is the RowFilter which will allow you to do filtering operations on a dataview,
you just need to associate the datatable to a dataview, apply the rowfilter to the dataview, and pass the dataview as datasource to your grid

please, make me know if you need more hints :D
  • 1

#4 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 13 July 2012 - 01:21 AM

You can combine two or more conditions in the WHERE clause of an SQL statement with AND, OR and braces. In your case I think you need to use AND to combine your conditions.

"SELECT * FROM [Memo] where Hours < " + int.Parse(txt_hours.Text) + " AND Age > " + int.Parse(txt_age.Text)


Thanks a lot!! U have solved my pblm.

Maybe what you are looking for is the RowFilter which will allow you to do filtering operations on a dataview,
you just need to associate the datatable to a dataview, apply the rowfilter to the dataview, and pass the dataview as datasource to your grid

please, make me know if you need more hints :D


Although my my qn was solved by kernelcoder. i would like to knw more abt rowfilter to solve the same qn.could u teach me?
  • 0

#5 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 13 July 2012 - 08:17 AM

Did you try the link Black Rabbit put in his message? Your answer is there.

Here's another: http://msdn.microsof...(v=vs.400).aspx
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth





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

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