Solved

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

Posted on 2013-01-16
4
469 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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