Freelancer
Freelancer

Reputation: 9064

Querying data in gridview

I am developing windows application in c#.

I have a gridview with about 40k records in it.

In that gridview i have checkbox.

I wanted to update only those records which are checked.

I have written following code for it>>

for (int i = 0; i < gvTradeFile.Rows.Count; i++)
            {

                try
                {
                    DataGridViewCell cell = gvTradeFile.Rows[i].Cells[0];
                    bool check = (bool)cell.Value;

                    if (check)
                    {
                        try
                        {
                            con.Open();
                            cmd = new SqlCommand("update tradeFile set Party_Code='" + txtTransferPartyCode.Text + "' where Party_Code='" + gvTradeFile.Rows[i].Cells[1].Value.ToString() + "' and TradeNo='" + gvTradeFile.Rows[i].Cells[6].Value.ToString() + "' and Sauda_Date='" + gvTradeFile.Rows[i].Cells[13].Value.ToString() + "' and OrderNo='" + gvTradeFile.Rows[i].Cells[14].Value.ToString() + "'", con);
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            con.Close();
                        }
                    }



                }
                catch (Exception ex)
                {
                }
            }

In this if we look at the for loop i am dealing with each row in the grid.

Is there any specific technique through which i can directly take only Checked rows[via checkbox in grid] in for loop?

Above code works fine but because of each and every row is checked, exe is getting hanged.

Please help me.

Upvotes: 0

Views: 169

Answers (3)

Dave
Dave

Reputation: 8451

I can think of 2 options.

  1. You could stick the save onto a new thread, then either have your colleague/client assume it's saved or display a 'saving' graphic.

  2. Split the entire thing up to 1000 records at once. This way, you're only showing first 1k, edit, save. Then show next 1k, edit, save! This will reduce the overhead.

Upvotes: 1

Amit Singh
Amit Singh

Reputation: 8109

I am not very sure, but I think you can not get any gridview function that returns the checked rows.

However, you can do this manually by keeping the record the which row of the gridview checkbox is checked. Than update your database according to your list you maintain.

Upvotes: 1

Rohit Vyas
Rohit Vyas

Reputation: 1959

I think on the checkbox chek event you should maintain a datatable or a dictionary if checkbox is checked and then bulk update the database. You can google for bulk update

Upvotes: 1

Related Questions