Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to select a particular cell in a datagridview using C# programmatically.
I passes a datatable as a datasource of the datagridview.

After that I want to select given cells automatically after loading data.

In the Code,
C#
//loading data to the datagrid
 dataGridView2.DataSource=dt;         //dt is the data table
 dataGridView2[2, 1].Selected = true;

But this does not work.

But if I use the same code at the click event of a button,particular cell is selected.
Can anyone tell me what is the cause for this problem??
Posted
Updated 31-Jul-12 8:32am
v3
Comments
Kenneth Haugland 31-Jul-12 14:16pm    
WPF?
Sameera Fonseka 31-Jul-12 14:19pm    
Yep..it is a windows form..tkx
Kenneth Haugland 31-Jul-12 14:22pm    
Èhh, Windows Froms and Windos Presantation Foundation (WPF) is two very different tings.... Its not clear to me what of those you use... Im guessing WinForms but Im not sure.
Sameera Fonseka 31-Jul-12 14:30pm    
sorry i m using win forms.. i will be very thankful if you can give me any hint..tkx

1 solution

Use
C#
dataGridView2.CurrentCell = dataGridView2[2, 1];


I got the multiple cell selection to work by using:
C#
dataGridView2.MultiSelect = true;
dataGridView2.SelectionMode = DataGridViewSelectionMode.CellSelect;

// set a current cell before selecting any
dataGridView2.CurrentCell = cableGrid[2, 1];
dataGridView2[2, 1].Selected = true;
// now select any others you want
dataGridView2[3, 1].Selected = true;


I found that without first setting CurrentCell then Selected = true did not work.
After adding this line though I was able to select as many cells as I wanted.
Not entirely sure why it works this way, I am curious if anyone knows the answer though.
 
Share this answer
 
v2
Comments
Sameera Fonseka 1-Aug-12 11:27am    
Thankx alot..But this didn't work either.I tried it earlier..If I place this code inside the databinding complete event of the datagrid view it also worked..
That is the point i am stuck..How can i programmatically select multiple cells of a datagridview in a separate method?
obp42 1-Aug-12 12:03pm    
Added some code that will hopefully help you, check it out.
Sameera Fonseka 1-Aug-12 13:16pm    
This didnt work either..but any setting of a cell can be done in a button click event..that is the point im stucked.
Before selecting cells i set the datasource of that datagridview to a datatable returned from another method.
Even i put above code,only the current cell is selected..tkx
obp42 2-Aug-12 16:19pm    
You said you got it to work if you put it in the DataBindingComplete event right? Is there something wrong with doing it that way?
Sameera Fonseka 3-Aug-12 5:43am    
But that does not do my requirement..Sequence of the process datatable is returned from another method.Then it is set as the datasource of the datagridview.
After that I want to select some cells of that datagrid view according to some paramateres.Problem arises with that.I can't select any cell programmatically..

But if i manually add some records to the datagridview i can select any cell programmatically.So i am sure that it is some issue with the datatable returned.Is there any specific way to create a datatable in order to set it as the datasource of the datagridview?
Any help is highly appreciated..thank you very much

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