Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a grid view which include a row that contain
1. Combobox for column selection
2. Combobx for searching method (=, like, <, <>, >)
3. Text box for search (what to search)
4. Combobox for operator (AND/OR)

I am using this code to send where clause to Store procedure
C#
private void buttonX1_Click(object sender, EventArgs e)
        {
            lblRecordNo.Text = "";

            foreach (DataGridViewRow Datarow in dataGridView1.Rows)
            {
                
                if (dataGridView1.RowCount == 1) //When DataGridview Contain only one row
                {
                    if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null && Datarow.Cells[2].Value != null)//&& Datarow.Cells[3].Value != null)
                    {

                        WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString() + " ";
                        SearchQuery = WhereClause;    
                    }
                        
                    else
                    {
                        MessageBox.Show("Must Fill Column-کالم لازمی پر کریں","Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                    
                }
                else if (dataGridView1.RowCount > 1) //When DataGridview Contain MOre then 1 row
                {
                    if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null && Datarow.Cells[2].Value != null && Datarow.Cells[3].Value != null)
                    {
                       
                        WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString()+ " " + Datarow.Cells[3].Value.ToString()+ " ";
                        SearchQuery =  WhereClause;
                    }
                        
                    else
                    {
                        MessageBox.Show("Must Fill Column-کالم لازمی پر کریں", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }    
                    
                }
                
            }

           
            //for testing search query ! What is this?
            MessageBox.Show(SearchQuery);
          

            //passing value to BLL CLass
            objSearching.WhereClause = SearchQuery;
            
            //pass Searched values to Form1 (Main Form) of Application.Bind also Search's tab Combobx and textboxes 
            //this work require to do!!!!!
            FrmMain.dgvhadiths.DataSource = objSearching.HadithSearching();
            
            dgvSearchHadith.DataSource = objSearching.HadithSearching();
            //reset the searching query variable
            lblRecordNo.Text = dgvSearchHadith.Rows.Count.ToString();
            WhereClause = ""; SearchQuery = ""; BookSelection="";
        }


This fine but the poblem if user add more then one row in Datagridview then how to not add
C#
Datarow.Cells[3].Value.ToString()

this contain and/or keyword
Posted

i am not sure check this one

C#
foreach (DataGridViewRow Datarow in dataGridView1.Rows)
            {

                if (dataGridView1.RowCount == Datarow.Index +1) //check if it is the last row other wise do the else 
                {
                    if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null && Datarow.Cells[2].Value != null)//&& Datarow.Cells[3].Value != null)
                    {

                        WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString() + " ";
                        SearchQuery = WhereClause;
                    }

                    else
                    {
                        MessageBox.Show("Must Fill Column-کالم لازمی پر کریں", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                else// except the last row 
                {
                    if (Datarow.Cells[0].Value != null && Datarow.Cells[1].Value != null && Datarow.Cells[2].Value != null && Datarow.Cells[3].Value != null)
                    {

                        WhereClause += Datarow.Cells[0].Value.ToString() + "  " + Datarow.Cells[1].Value.ToString() + " " + Datarow.Cells[2].Value.ToString() + " " + Datarow.Cells[3].Value.ToString() + " ";
                    }

                    else
                    {
                        MessageBox.Show("Must Fill Column-کالم لازمی پر کریں", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }

            }

tell me if it work or not
 
Share this answer
 
You can check out the examples written by Stephen Thomas in his book Beginning WPF 4.5 by Full Examples. They are simple, clear and they helped me - http://tekkiebooks.com/Product/9db79251-4ddb-42c5-819e-873cce0f4d5f/codeproject_02[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900