Solved

In C#, the datagridview is taking the control to top at every check on mouseup

Posted on 2013-01-16
4
468 Views
Last Modified: 2013-01-16
I have a datagridview where in i have a checkbox along with some other fields, when i click on checkbox, the color of row becomes green if i check the checkbox else becomes green.

But everytime the cursor goes to top.

The code is attached.

 private void dataGridView1_CellMouseUp_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 0)
                {
                    DataGridViewRow dgvRow = dataGridView1.Rows[e.RowIndex];
                    DataGridViewCheckBoxCell chkBox = (DataGridViewCheckBoxCell)(dgvRow.Cells[e.ColumnIndex]);

                    SqlCommand cmd = new SqlCommand("updstssts", b.con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@uid", SqlDbType.VarChar, 50).Value = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    if (Convert.ToBoolean(chkBox.Value))
                    {
                        cmd.Parameters.Add("@sts", SqlDbType.Char, 1).Value = 'U';
                        //    dataGridView1.Rows[e.RowIndex].Cells[0].Value = false ;
                    }
                    else
                    {
                        cmd.Parameters.Add("@sts", SqlDbType.Char, 1).Value = 'F';
                        //  dataGridView1.Rows[e.RowIndex].Cells[0].Value = true ;
                    }
                    if (b.con.State == ConnectionState.Closed)
                    {
                        b.con.Open();
                    }
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    b.con.Close();

                    filgrd(e.RowIndex);


                }
            }
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 38782169
What "filgrd(e.RowIndex);" is doing in your code?
0
 

Author Comment

by:searchsanjaysharma
ID: 38782322
The code is as follows

        private void filgrd(int r)
        {
            try
            {
                string cor = b.pcbcor;
                string sec = b.pcbsec;
                b.con.Close();
                SqlDataAdapter adp = new SqlDataAdapter("selstdbycorsec", b.con);
                adp.SelectCommand.Parameters.Add("@cor", SqlDbType.VarChar, 50).Value = cbcour.Text; ;
                adp.SelectCommand.Parameters.Add("@sec", SqlDbType.VarChar, 50).Value = cbsec.SelectedValue.ToString();
                adp.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                adp.Fill(ds);
                //ds.AcceptChanges();
                // dataGridView1.DataSource = null;
                dataGridView1.DataSource = ds.Tables[0];
                bndgrd();

                // dataGridView1.
            }
            catch (Exception ex) { MessageBox.Show("The Error is:" + ex.Message + ",Kindly take screen shot of this and send to sanjaysharmacgc@gmail.com"); }
        }
0
 
LVL 11

Accepted Solution

by:
Deepak Lakkad earned 500 total points
ID: 38782387
As you are rebinding your DGV, it will select first row this is default behavior.

If you want to reselect your row again. You have store current selection (row number) in a variable before doing update and all that stuff.

After update and rebinding, DGV, you have to change selection of row using variable used earlier.

I have done it in my projects. Code snippet is as below:

Storing Current Position in a variable (Before Update)

rowPosition = myDGV.CurrentRow.Index

Open in new window


Relocate Row - After Update

myDGV.CurrentCell = myDGV.Rows(idgvPosition).Cells(0)

Open in new window


This is Visual Basic version of code, you have to write code for C# accordingly

- Deepak Lakkad
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 38785695
tx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question