Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1. I have a button on click event it show a datagridview which had number of rows

2. i Want to select the first row or second row defaultly so i can navigate the datagrid
through with up and down key.

3. The on Enter key event i want to some selected row details



C#
public frmRoomAvailability(Transactions.frmCheckINNEW objcheckIn)
       {
           objCheckIn = objcheckIn;
           InitializeComponent();
           getdgv();

       }

 public void getdgv()
        {
            DataSet ds = new DataSet();
            ds = DB.ExecuteQuery_SP("SelectAllVacantRoom");
            try
            {
                if (ds.Tables.Count > 0)
                {
                    dgvsvail.DataSource = ds.Tables[0];
                   
                    Generalclass.ROOMID = Convert.ToInt32(ds.Tables[0].Rows[0]["RoomID"].ToString());
                }
            }
            catch (Exception ex) { }
        }
Posted
Updated 9-Dec-14 19:39pm
v2

1 solution

The simplest way is:
VB
dgv.Rows[index].Selected = True;

where index for first row is equal to 0 (zero).
Note: there must be at least one row, otherwise the error occurs!
 
Share this answer
 
Comments
SamsCool 10-Dec-14 2:03am    
I tried this but not working

public frmRoomAvailability(Transactions.frmCheckINNEW objcheckIn)
{
objCheckIn = objcheckIn;
InitializeComponent();
getdgv();
dgv.Rows[0].Selected = True;

}

like that i used but it's not working.
Maciej Los 10-Dec-14 2:07am    
Your DataGridView has name: dgvsvail. So, use proper name in your code!
SamsCool 10-Dec-14 2:03am    
then the grid resides in another form which is calling on btn click event.
SamsCool 10-Dec-14 3:58am    
ya i used still not working......i think the main reason is that the form is not active on button click it is showing the form and grid only the gridreside form is not active
Maciej Los 10-Dec-14 4:48am    
No. This has nothing to do with DataGridView row selection.
How do you call that form?

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