Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-16
4
Medium Priority
?
471 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 1500 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

618 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