?
Solved

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

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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