Click here to Skip to main content
15,942,756 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
when i bind the data in the datagrid programmatically and deselect first cell programmatically by this code .
C#
dgv.rows[0].selected= false; 

when i open the form it select the first sell as always!
i tried many and many even i tried it in VS 2012 .
still a problem ! why MDI have a bug like that !?
it worked whin i make it normal form not MDI but i want it in mdi any help ?
Posted
Updated 2-Aug-12 5:40am
v4

1 solution

Try this:
C#
dgv.CurrentCell = dgv[0, 0];
dgv.Rows[0].Selected = false; 

It seems that CurrentCell for the grid is null until it is displayed or CurrentCell is set programatically and in the past I've been unable to use Selected = true/false until first setting a CurrentCell.

I think you might want to do this in your DataBindingComplete event. So using the code you gave in the comment, it would look something like
private void ItmGroupfrm_Load(object sender, EventArgs e)
{
  using DataTable dt = itmGrp.SelectItemGroup());
  {
    dgv.DataBindingComplete += 
      new DataGridViewBindingCompleteEventHandler(dgv_DataBindingComplete);
    dgv.DataSource = dt;
  }
}

void dgv_DataBindingComplete(object sender,
  DataGridViewBindingCompleteEventArgs e)
{
  dgv.CurrentCell = dgv[0, 0];
  dgv.Rows[0].Selected = false;
}
 
Share this answer
 
v3
Comments
Ahmad Abd-Elghany 2-Aug-12 15:19pm    
thanx but its still sellected it !
the problem not in the code, its with the mdi and the datagridview !
when i make it normal form its working great !
obp42 2-Aug-12 15:48pm    
You should add at least a little more code to your question. Show us where you are calling this line, etc. Otherwise I don't know how much more anyone is going to be able to help you.
Ahmad Abd-Elghany 2-Aug-12 15:56pm    
from the parent form i call my form bt this code .
Forms.ItmGroupfrm frm = new Forms.ItmGroupfrm();
frm.MdiParent = this;
frm.Show();

private void ItmGroupfrm_Load(object sender, EventArgs e)
{
BindDgv();
}

and bind method here :
private void BindDgv()
{
using (DataTable dt = itmGrp.SelectItemGroup())
{
dgv.DataSource = dt;
if (dt.Rows.Count > 0)
{
dgv.CurrentCell = dgv[0, 0];
dgv.Rows[0].Selected = false;
}
}

}
when i call it from the main by this code :
Forms.ItmGroupfrm frm = new Forms.ItmGroupfrm();
frm.Show();
the code that deselect the data work perfectly !
obp42 2-Aug-12 16:12pm    
I updated my answer for you, take a look and let me know if it works.
ashumeerut 27-Sep-13 6:25am    
I have same sort of problem sir , which u have solved , but where is ur answer i can't see it, u have written that "I updated my answer for you, take a look and let me know if it works." but where is the updated answer. please help sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900