Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now