Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi. I have a table in which I had to add the 'checkbox column'. I have a button which needs to filter only SELECTED ROWS. I did my research on the internet and found some different way (creating a new datatable, new columns and storing the data into them) but It is not how I should do it. I want the SQL querry to filter my datagridview so I came up with this code:

private void button2_Click(object sender, EventArgs e)
        {
            
            for ( int i = 0; i < grupeDataGridView.Rows.Count; i++)
            {
                DataGridViewRow row = new DataGridViewRow();
                row = grupeDataGridView.Rows[i];
                
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    label1.Text = row.Cells[2].Value.ToString();

                    listBox1.Items.Add(label1.Text);

                    try
                    {
                        this.grupeTableAdapter.FillBy12(this.dbDiplomskiDataSet.grupe, label1.Text);
                    }

                    catch (SystemException ex)
                    {
                       System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }
            }
        }


THE PROBLEM: Application filters only ONE ROW and the odd thing is that it shows FIRST SELECTED ROW. I thought if something is wrong and the code shows me only ONE ROW it should be LAST ? Anyway here's the example:

IF I HAVE 10 ROWS, AND I SELECT 2ND, 4TH AND 6TH ROW I WANT THE PROGRAM TO SHOW ME ROWS 2,4,6, AND THIS CODE THAT I'VE WRITTEN SHOWS ME ONLY ROW - 2 ....

Can someone help me how to get rest of the selected rows. Thank you
Posted
Updated 9-Jun-15 4:00am
v2
Comments
njammy 9-Jun-15 11:49am    
Hopefully can help you. Can you confirm the value you are getting in the label1.Text property in line :
try
{
this.grupeTableAdapter.FillBy12(this.dbDiplomskiDataSet.grupe, label1.Text);
}
?

Also it seems from your code logic that each time you loop over the data grid view row, the table adapter you have there will overwrite any previous rows data retrieved. Technically you should only get the last selected row data.
[no name] 9-Jun-15 15:47pm    
Yes. I can confirm I'm getting value. datagridview shows me every column but only first selected row and not last as we said technically should be ...

Can you suggest something, by that I meen how could I get that table adapter doesn't owervrite the data but how to show me what I selected.
ramyajaya 10-Jun-15 0:54am    
Check this example http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx

Hope it helps
[no name] 10-Jun-15 5:25am    
@ramyajyay thank you very much but I've seen that example. Actually it works, but It's not quite what I need. I want my datagridview to filter itselfs data and not to filter and write new (filtered) data ...
ramyajaya 10-Jun-15 8:49am    
That s also possible u r just adding rows to data table so you can bind the newly created data table to your existing grid then that's it

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